How to Create a custom plugins in wordpress
What is a WordPress plugin:
A plugin in WordPress is a small software app that can be added to a WordPress site to extend its functionality and to add new features to it without changing its core program code.
The WordPress plugins are written in PHP and they integrate with WordPress very easily.
Creating a plugin:
Here are the steps that need to be followed while creating a custom plugin in wordpress.
1.Plugin Name
Choose a unique name for your plugin according to its purpose.
2.Plugin files and folders
Now all the plugins installed in a WordPress site are placed inside the /wp-content/plugins/ directory. So create a folder for your plugin in the directory,
check screenshot –
Suppose our plugin name is “Sample Plugin” then create a folder with this name.
3.Create the index file for your plugin
Now create its index file in the plugin folder you created in step 2 named after the plugin name that is sample-plugin.php (though you can give any name to this file it is preferable to choose after the plugin name) and hence it will be unique in the plugin directory.
This file is going to be your plugins index file so it will contain the actions required to execute on plugin activation/deactivation and database related functionality.
4.Setup the plugin Info
There should be specific information added in the header of the plugin’s index file(created in step 3).
The standard header should contain the plugin’s information, for ex.
/* Plugin Name: Sample Plugin Plugin URI: http://www.sampleplugin.com Description: This is a sample plugin Author: A. B. C. Version: 1.0 Author URI: http://www.abcsite.com */
WordPress is going to recognize your plugin using this information and this is very important.
The details you have added in header file of your plugin’s index file will reflect here.
In the plugin’s main file we can also handle the activation and deactivation of the plugin by writing our own functions on the wordpress inbuilt activation and deactivation hooks. Example –
Function on Deactivation hook
register_deactivation_hook(__FILE__, ‘my_deactivation’ ); function my_deactivation ( ) { /** Add here that you want to call on deactivation of the plugin**/ }
Function on Activation hook
register_activation_hook(__FILE__, ‘my_activation’ ); function my_activation ( ) { /** Add here that you want to call on activation of the plugin**/ }
Function to add a line below all posts on our site
add_action( 'the_content', 'my_custom_text' ); function my_custom_text ( $content ) { return $content .= 'This is my first custom plugin!
'; }
There are various other hooks also that we can use to override the default functions of the plugin. Also, we can write our custom code to fulfill the purpose of the custom plugin.
5.Readme file
If you want to host your plugin on WordPress organization then you will need to maintain a readme file in a proper format that will explain about the plugin purpose, its installation process, its version, etc.