Plugin in WordPress is a component to add or extend functionality that can be enabled or disabled as required and makes no interference with the core program/software and its code. WordPress is designed to be lightweight and smooth to increase the flexibility and reduce code that might not be important for basic website functions. Plugins then add custom features and functionalities that allow users to design the site as their specific requirements.
In this guide, you will learn how to develop your first WordPress plugin.
Prerequisites:
- A WordPress Installation
- Basic Knowledge of PHP
Steps to make a plugin –
- Goto the WordPress Installation -> WordPress/wp-content/plugins.
- Make a new folder here and name it as the “Plugin Name” in this case First-Plugin
- Next, make a new PHP file in this folder and name it same as the folder in this case First-Plugin.php
- Now, we add the name of our plugin. Add Plugin Name enclosed in a PHP comment in the file.
Code of PHP file First-Plugin.php –<?php
/**
* Plugin Name: First Plugin
**/
?>
- Now, Goto the WordPress Dashboard->Plugins, Here you can see our newly created plugin and its name.
- We should not leave the description blank as it helps the user know what is plugin meant to do. so let’s add the description of our plugin by adding a new comment line after “Plugin Name:” with the title “Description” and save it. Code of PHP file First-Plugin.php –
<?php
/**
* Plugin Name: First Plugin
* Description: This is my first Plugin.
**/
?>
- We Successfully added Description of our plugin now go to Dashboard->plugin to see how it looks like.
- Next, we add some functionality to our first Plugin. We will add a basic function named first_plugin() which prints “HELLO This is my first Plugin.” and its shortcode which allows this to be placed on a WordPress post or a Page.
Code –<?php
/**
* Plugin Name: First Plugin
* Description: This is my first Plugin.
**/
function
First_Plugin()
{
$content
=
"HELLO This is my first Plugin."
;
return
$content
;
}
add_shortcode(
'myplugin'
,
'First_Plugin'
)
?>
- To display this function output value we are using the shortcode feature of WordPress. Go to Posts->edit or create a new post. There add the shortcode for the Plugin we just made.
Shortcode – [myplugin]
- To see the plugin output, browse the website and open the post in which we added the shortcode of our plugin.
Here is how it looks like-
- Now, To have this plugin functionality used multiple times we can use this shortcode multiple times. Just add the shortcode wherever we need this output.
- Check the post for updated output here we get two same sentences because we used the shortcode two times in this post.
Now we have our Plugin which returns a value and this can be used wherever we want by the use of Shortcode.