How to create first WordPress plugin

Spread the Knowledge
  •  
  •  
  •  
  •  

WordPress plugins is a code which adds extra functionality to your existing WordPress website. if you know about WordPress, then you must be heard about WordPress plugins. Read this article to Install your first WordPress plugin.

The WordPress plugin Code may contain PHP, Jquery, javascript CSS, and HTML. But one PHP file is required.

To write a WordPress plugin, You must be familiar with PHP. If you are not familiar with PHP, then it may not be possible for you to create a plugin.

Very basic example of the plugin

For example, you want to add some specific text at the end of every post. There are two ways to achieve this. One way is to go to the theme folder and add your text to the single.php file. But there is an issue with this method. If you update or change the WordPress theme, Then all the changes you have done in the theme will be automatically removed.

The second method is to create a small plugin for this purpose.

creating first WordPress plugin

Go to wp-content/plugins inside the Installed WordPress directory, and create a New Folder with a specific name related to the plugin functionality. As here we are creating a plugin to add specific text after every post. So we will rename the folder to text-after-postcontent. Inside this folder create an empty PHP file. You can name the file as you want. it can be plugin.php or text-after-postcontent.php or anything. but it is good coding standard to name it plugin.php or name it on the folder name.

Here we will name it as plugin.php. So copy the below code and paste it in the PHP file.

<?php
/*
Plugin Name: Text after Post Content
Plugin URI: https://www.w3beginner.com Description: This plugin will add Specific Text after every post Content
Version: 1.0
Author: w3beginner
Author URI: https://www.w3beginner.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: w3beginner
Domain Path: /languages

*/
?>

After pasting the above code in your plugin.php, Save the file. The above commented code is called Plugin header, and this code will tell WordPress the Plugin Name, Plugin Description etc.

In the plugin header, only Plugin Name is required, All the other things are optional.  After saving the file, go to your WordPress admin area and click plugins, You will see that our newly created plugin is also listed in the installed plugins.

Basic WordPress plugin

WordPress will take it as a fully functional plugin, even there is no single line PHP code. Now we will write a function to print a specific text at the end of every post.


<?php
function w3b_after_post_content($content) {
// this if condition is to check that if it is a post, not page or any other post type.
// if you want to show this code on pages and all other post types then simply remove the condition
if (is_single()) {  
    $content .= '&amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;You specific text goes here...&amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;';
}
    return $content;
}
?>

This function can add a text to post content to print. But the Question is how to call it. For this purpose, we need to use hooks. To learn more about WordPress Hooks read this.

Here we will shortly describe hooks. In WordPress, there are two types of hooks. Actions and Filter

Action

Actions are triggered by specific events that take place in WordPress, such as activating a plugin, activating a theme, publishing post etc. As we are not allowed to do any changes inside the core WordPress, But they give us the opportunity to inject our own code through actions.

Filters

Filters are the function through which WordPress passes data, So we can change that data before sending it to the database or sending to the browser. In our plugin we are going to use filters, so you will better understand.

Add the below code to the file

<?php
add_filter( "the_content", "w3b_after_post_content");
?>

In this line of code the_content is the filter name which is defined inside WordPress core, While w3b_after_post_content is the function name that we created before.

Now putting all code into one.

<?php
/*
Plugin Name: Text after Post Content
Plugin URI: https://www.w3beginner.com Description: This plugin will add Specific Text after every post Content
Version: 1.0
Author: w3beginner
Author URI: https://www.w3beginner.com
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: w3beginner
Domain Path: /languages

*/

function w3b_after_post_content($content) {
// this if condition if to check that it is a post, not page or ahy other post type.
// if you want to show this code on pages and all other post type then simply remove the condition
if (is_single()) {
$content .= '&amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;You specific text goes here...&amp;amp;amp;amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;amp;amp;amp;gt;';
}
return $content;
}

add_filter( "the_content", "w3b_after_post_content");

Save the file, and go to admin=>Plugins and activate this plugin.

Now visite any of the post inside your WordPress, you will see that specific text at the end of every post.

Hope this article will help you in creating your very first simple plugin. If got any issues while creating the plugin please let us know in the below comment section. We will try our best to help you.

 

Spread the Knowledge
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *