WordPress Plugin Development for Beginners

Recently I started a project in WordPress in which I needed to create few plugins. I had no idea where to start from. But searching the web for few minutes gave me plenty of information to boost me up. So here I will show you how to create a wordpress plugin from scratch.

First of all you must decide a unique name for your plugin beacause WordPress’s plugin directory contains a number of plugins and your name may conflict with that of others. So find a unique name:)

Lets name our plugin ‘AwesomeWP‘.
Here is the directory structure for our plugin: rootwp-contentpluginsAwesomeWPstart.php
Note that we have created a new directory ‘AwesomeWP’ for our plugin. Although you can place your plugin file directly under wp-contentplugins directory, but making a separate directory helps you managing your files in an elegant way.
Now open wp-contentpluginsAwesomeWPstart.php and write following lines in the file:

<?php
/*
Plugin Name: AwesomeWP
Plugin URI: http://webspeaks.in/
Description: My first Awesome wordpress plugin
Version: 1.0
Author: Arvind Bhardwaj
Author URI: http://webspeaks.in/
License: GPL
*/
?>

Go to wordpress admin > plugins and you will see the AwesomeWP listed there. Happy to see it? Hmmmm! Its the comment part in the header portion of file that helps WordPress finding you plugin.
Now lets add some functionality to our plugin as till now it does nothing. Add following lines to our start.php

add_action('init','awesome');
function awesome()
{
echo "WordPress is just Awesome!!";
}

The ‘init‘ is called when WordPress initializes. So we have called a function ‘awesome’ on initialization of our blog. Note that no braces are required during this function call. After activating the plugin you will see ‘WordPress is just Awesome!!’ printed on top of your page, means it works!

Modifying the Post:
WordPress provides us very powerful means to modify literally any part of the wordpress blog. Like we can change the outcome of the posts as to our desire by just adding following lines of code in our plugin file:

add_filter('the_content', 'modifyPost');

function modifyPost($content)
{
$signature = '<br />Have fun with plugins!';
return $content.$signature;
}

What actually happened here is that we applied a WordPress filter ‘the_content‘ in our plugin. This hook is called when post is diplayed on frontend. So at the time of displaying post, we added our custom text below the post. Note that the content of our post is automatically made available in the callback function ‘modifyPost‘ through the wordpress filter. This is the beauty of WordPress.
So we have learnt how to write our first WordPress plugin adn modify our posts through plugin. Happy coding:)

Complete Plugin File:

<?php
/*
Plugin Name: AwesomeWP
Plugin URI: http://webspeaks.in/
Description: My first Awesome wordpress plugin
Version: 1.0
Author: Arvind Bhardwaj
Author URI: http://webspeaks.in/
License: GPL
*/
?>
<?php
add_action('init','awesome');/*Called when WordPress Initializes*/
function awesome()
{
echo "WordPress is just Awesome!!";
}

add_filter('the_content', 'modifyPost'); /*This tells WordPress that every time it's going to display the content of a post or page, it
should run it through our modifyPost() function.*/
function modifyPost($content)
{
$signature = '<br />Have fun with plugins!';
return $content.$signature;
}

?>

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

5 thoughts on “WordPress Plugin Development for Beginners

  1. This is my first time i visit here. I found so many entertaining stuff in your blog, I guess I am not the only one having all the leisure here! Keep up the good work.

Comments are closed.