Now that you know what a plugin is, let's explore what you need to create one.
The minimum requirements for a valid WordPress plugin are at least one PHP file, the main plugin file, with an opening PHP tag.
Inside the main plugin file, the first piece of code should be the plugin header, which is a PHP Comment block.
At minimum, it should contain a field for the plugin name.
To create your first plugin, navigate to the wp-content/plugins directory,
and then create a single PHP file called example-plugin.php.
Inside the file, make sure to open the PHP tags,
so that the server can execute any PHP code.
Now, add the following to the top of the file, just below the opening PHP tag.
This is known as the plugin header, and is written using a version of PHP's comment syntax called a DocBlock.
To read more about comments in PHP, take a look at the comments page in the basic syntax section of the PHP manual.
With your first plugin created, you can now browse to the plugins page in the WordPress dashboard,
and you will see your plugin available and ready to be activated.
The plugin doesn't do anything yet, but it's ready to have functionality added to it.
Once the plugin is activated, it is added to the list of active plugins stored in a serialized array in the options table.
You can find this array by running the following SQL query in phpMyAdmin.
Select all from wp options where option name like active plugins.
Notice how it stores the file name of the PHP file in the serialized array.
This is also known as the plugin slug, and it is how WordPress identifies your plugin during request execution.
If you move your main plugin file inside a directory, the plugin slug changes to include the directory name.
Create a new directory in the wp-content-plugins directory called example-plugin, and move your plugin file into that directory.
If you browse to the plugins page in the WordPress dashboard, you'll see the plugin is no longer active, because the slug has changed.
You'll also notice the warning at the top of the plugins page.
The plugin, example-plugin.php, has been deactivated due to an error.
The plugin file does not exist.
Now, activate the plugin.
Then, go back and look at the list of active plugins in the options table.
Notice how the new slug has been added to the serialized array, which includes the directory name.
While the plugin name field is the basic requirement for a valid plugin, there are additional fields available for you to add to the plugin header.
It is generally recommended to also add a description and a version to the plugin header.
This allows users to get a bit more information about your plugin, and it looks better when displayed in the plugin list.
You can read the full list of plugin header fields in the header requirements page in the plugin developer handbook.
Plugging handbook also contains a section on common best practices when developing plugins.
One of these suggestions is to include a check to ensure that the plugin code is only executed when part of a WordPress request.
It's usually a good idea to include this check right after the plugin header.
What this code does is check if the abspath constant is defined, which is a WordPress specific constant.
If it's not defined, then accept the code execution of the plugin.
This way, if someone tries to browse to the main plugin file in a browser directly,
none of the PHP code in the plugin will be executed, preventing any security risks.
You can read more about this and the other suggestions in the best practices page in the plugin developer handbook.
.