During the past week I was thinking about a new tutorial on how to create a custom WordPress plugin from scratch. The task is not that hard to be done, especially when you have a good idea how WordPress works and what is its files structure.
The following will be a detailed tutorial for custom WordPress plugin creation. I will provide step by step instructions on how to start developing a simple plugin. For better understanding I will add a little piece of code that will query the database and generate backups.
Custom wordpress plugin installation
Let’s start from the point that you have default WordPress installation. What you have to is login to its admin panel and navigate to plugins page. If no new plugin are installed so far you would see the following on your screen:
These are the plugins that come by default. At this point you are on plugins page and this is the place where our plugin will appear soon. Here is what to do next:
Using FTP client you should access the root folder of your WordPress instance and then open wp-content folder. Inside it you will see plugins folder, open it and create new one named dbmechanic. The name can be of course changed. If you refresh plugin page you will see no change. This is because we create the folder of our plugin, but there is no code to trigger WordPress hooks.
Custom plugin development
As a next step, we have to create a new file that will contain the code of our future plugin. The file name could be dbmechanic.php.
Again WordPress plugin page does not show any signs about a new plugin because we have a folder and main file, but there is no code. So now we have to open dbmechanic.php and add the following header to it:
<?php /* Plugin Name: Simple database mechanic Plugin URI: http://wppotion.com/ Description: A simple database mechanic plugin Version: 1.0 Author: Leonid G. Author URI: http://wppotion.com/ License: GPL */ ?>
Now go back to plugins page and refresh it. If everything is fine, you will see our custom plugin in the list.
As you can see the information from the headers corresponds correctly with WordPress hooks and the name, version, URI and author are displayed as expected on the page.
We have now the basic file and we can proceed and add some options to it. As adding the code piece by piece may break it and generate fatal errors I will paste the entire code and then explain the lines:
Now if everything is fine, you have created your first custom WordPress plugin. Just refresh the plugin page and you will see the following on your screen: