How to create and install moodle local plugin
Published: 2022-06-06 10:26:12

**Moodle Local Plugin:** _Local plugins are used in cases when no standard plugin fits the requirement, here are a few examples:_
1. Event consumers communicating with external systems.
2. Custom definitions of web services and external functions.
3. Applications that extend moodle at the system level (hub server, amos server, etc.).
4. New database tables used in core hacks (discouraged).
5. New capability definitions used in core hacks.
6. Custom admin settings.
7. Extending the navigation block with custom menus.

**How to create moodle local plugins?**

To create local plugin you can follow following folder structure according to Moodle documentation (https://docs.moodle.org/dev/Local_plugins)
local/
     yourplugin/
       db/
         access.php
       lang/
         en/
           yourplugin.php
       settings.php
       version.php

Now, create all the files and put some necessary code into it.

1.Open **version.php** file and put below code into it.
```
<?php 
/**
 * Version details.
 *
 * @package   local_yourplugin
 * @copyright 2014 Surya Pratap
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

defined('MOODLE_INTERNAL') || die();

$plugin->version   = 2014061101;      // The current module version (Date: YYYYMMDDXX).
$plugin->requires  = 2014050800;      // Requires this Moodle version.
$plugin->component = 'local_yourplugin';// Full name of the plugin (used for diagnostics).
$plugin->cron      = 0;
```

2. Open the **settings.php** file and put the below code into it.
```
<?php
/**
 * You may have settings in your plugin
 *
 * @package    local_yourplugin
 * @copyright  2014 Surya Pratap
 * @license    http://www.gnu.org/copyleft/gpl.html gnu gpl v3 or later
 */

defined('MOODLE_INTERNAL') || die();

if ($ADMIN->fulltree) {
}
```

3.Open **access.php** file under 'db' directory and put the below code into it.
```<?php
defined('MOODLE_INTERNAL') || die();

$capabilities = array(
);
```

4. Open **local_yourplugin.php** file under 'lang/en/' directory and put the below code into it.
```
<?php
/**
 * You may localized strings in your plugin
 *
 * @package    local_yourplugin
 * @copyright  2014 Surya Pratap
 * @license    http://www.gnu.org/copyleft/gpl.html gnu gpl v3 or later
 */

$string['pluginname'] = 'New local plugin';
```

The necessary code has been inserted into all the files, now zip these files and start the installation process.

**Installing via uploaded ZIP file:**
1. log in to your Moodle site as an admin and go to Administration > Site administration > Plugins > Install plugins.
2. Upload the ZIP file. You should only be prompted to add extra details (in the Show more section) if your plugin is not automatically detected.
3. If your target directory is not writeable, you will see a warning message as "Check the plugin validation report" then grant the writer access to the directory and reload the page and continue the installation.

 **Installing via command line:**
 1. log in to your server/machine
 2. Go to moodle root directory and place the code in the respective directory. In our case, we have to put our code in the local directory.
 3. Run **php admin/cli/upgrade.php** then a message prompt message will be printed on the terminal. Read the instruction and follow the process as suggested. In my case Here is the message:

 "== Upgrading Moodle database from version 3.4.4 (Build: 20180709) (2017111304) to 3.4.4 (Build: 20180709) (2017111304) ==

Your Moodle files have been changed, and you are about to automatically
upgrade your server to this version:

3.4.4 (BUILD: 20180709) (2017111304)

Once you do this you can not go back again. Please note that this process
can take a long time.

Are you sure you want to upgrade this server to this version?

type y (means yes) or n (means no)
"
type 'Y' and hit the 'Enter' button, this process may take some time. If any error occurred then fix it at your end.

4. Run **php admin/cli/purge_caches.php**

Now, the cache has been purged. You can make changes to the plugin file as per your requirement/assignment.


Cheers, Happy Moodleing!