In this tutorial we are going to make a very simple basic WordPress plugin. To do so we have to go to our WP installation folder, to the folder “wp-content”. Underneath to the folder “plugins” and within this folder we are going to make our plugin folder, which in this case is called “some-basic-plugin”. Underneath we make a PHP file which is called the same “some-basic-plugin.php”.
———————————————-
Create plugin folder and file
———————————————-
When we open de PHP file, we start with the opening PHP tag. We make some comments, which is called the “file header”. The file header contains some information which WP needs to know that our plugin exists. In this case we added the plugin name, version, description and author.
This is a very basic file header. If we take a look at the admin part of WordPress, we can see that the plugin is already added to the list. And as you can see the name is “Some Basic Plugin”. It has a description, the version and the author name, which is exactly the information we have put in the file header.
———————————————-
Add admin notice
———————————————-
So now we already created a plugin. But the plugin doesn’t do anything. So now we are going to add some functionality. In this case we are only going to add an admin notice when the plugin is activated. To do so we need to use the built-in action of WP. So we are going to use “add_action” and the action we want to use is “admin_notices”. And we add a function to it. In this case we call the function “some_basic_plugin_show_activated”, separated by underscores, because that’s the naming convention of WordPress.
It’s a very long function name, but it’s a good practice to use prefixes for your functions, at least for your global functions. In this case we used the prefix “some_basic_plugin_”. We use this prefix to prevent conflicts with other functions. So in this case we know our name is unique, because we used our own prefix.
Now we have to create the function itself. It’s a global function with the same name “some_basic_plugin_show_activated”. This function has to show the notice we want to add. So we added some HTML and then we added our notice description.
———————————————-
Test it
———————————————-
Now let’s have a look if it works. When we go to the admin part and click on the “activate” link of our plugin, then you can see that our notice is added on top of the page. In this case the notice will be shown on every admin page as long as the plugin is activated. So when we deactivate our plugin the notice is gone. And that’s our basic plugin.
———————————————-
More File Header information
———————————————-
You can also add some additional information to the plugin file header. It could look like this, also containing a plugin URL, an URL of the author, license, text domain (used for localization of the plugin), the domain path (the directory of the translation files) and network (used for plugins that need only to be activated on WP multi sites).
———————————————-
PHP DocBlocks
———————————————-
For those who use PHP docblocks, WP can also fetch the plugin information from DocBlocks like this.
Well this was the short demo on how to create a plugin in WordPress.
.
WordPress - Create your first Plugin in 3 minutes#createwordpressplugin
Comments
Post a Comment