Create A Simple HelloWorld WordPress Plugin

Posted by & filed under PHP, WORDPRESS.

Every WordPress beginner’s least dream is to write their own plugin to extend the WordPress functionality. We can extend WordPress functionality by writing our own plugin using WordPress API. In this tutorial we are going to create “HelloWorld” WordPress plugin, which let you add “Hello World” text at the end of the every post. Please follow the each steps to create a simple HelloWorld WordPress Plugin.

Create A Simple HelloWorld WordPress Plugin

Setting WordPress Local Development Environment:

Before starting our plugin development, We must have local development environment. Please follow the below tutorial to setup local WordPress development environment.

Install WordPress In Localhost

 

WordPress Plugin Directory

All the plugins in WordPress will reside in the following WordPress directory (wp-content/plugins). Now I am going to create new directory called “smart-hello-world” in the WordPress plugin directory (wp-content/plugins). The plugin directory name must be unique which avoids naming collision with other plugins.

Note: Plugin may be single PHP file or list of PHP files reside in the plugins directory. Always best practice is will keep all our plugin files in a separate directory.

Simple HelloWorld WordPress Plugin Folder Structure

Create Plugin Main File

 

 

Next we are going to create main/root file that WordPress will execute first in our plugin. This main/root file must have header information (information about our plugin). WordPress will read this information and recognises as valid WordPress plugin.

Here is sample WordPress plugin skeleton structure.

<?php

/**

Header Information

*/

//define plugin related constants

//includes php files or write our function

//Hook our plugin functions to WordPress Event

 

Add Plugin Header Information

Now create index.php file in the “smart-hello-world” directory and add the following header information in it.

Here is the our plugin header information. This plugin header information which tell us name of the plugin, plugin URL, author, author URL, description and version. and save it.

<?php
/**
Plugin Name: Hello World By SmartTutorials.net
Plugin URI: http://www.smarttutorials.net/
Author: muni
Author URI: http://www.smarttutorials.net/
Description: Sample plugin which lets the user to add "hello world" text at the bottom of every post.
Version: 1.0
*/

Now login into WordPress admin dashboard and click on plugins to see list of installed plugins. If you added header information correctly, then your plugin must be listed in the WordPress admin dashboard plugin’s area like this.
activate wordpress plugin

If the plugin is not listed, then there is mistake in the header information format.

Now activate our plugin by clicking on the activate button.

Create WordPress Function

Next create a function which appends the “Hello World” text at at the bottom of every post. Function name must be unique which avoids the naming collision with other function name.

Note: If two functions has same name, then PHP interpreter will through fatal error. So be careful while naming your function name.

function smart_hello_world($content){
	return $content."<p style='text-align:center;font-size: 25px;font-weight: bold;'> &#9827; Hello World &#9827; </p>";
}

Link PHP Function With WordPress Event Through WordPress Hook

The plugin function’s must be linked to WordPress Life cycle event through WordPress hook.

1. Filter Hook
2. Action Hook

Filter Hook:

Filter hook in wordpress allow us change the data before displaying content to user.

Filter Hook:

Action hook in the wordpress allow as do some stuff. For example if I want to add some css in the head section of the page following wp_head() action hook let us add the css to the head of the page.

Here we are going to append “Hello World” text at the end of the post. So we are going to link our function to WordPress events using Filter hook.

<?php
/**
Plugin Name: Hello World By SmartTutorials.net
Plugin URI: http://www.smarttutorials.net/
Author: muni
Author URI: http://www.smarttutorials.net/
Description: Sample plugin which lets the user to add "hello world" text at the bottom of every post.
Version: 1.0
*/

//define our plugin related constants

//include PHP files

function smart_hello_world($content){
	return $content."<p style='text-align:center;font-size: 25px;font-weight: bold;'> &#9827; Hello World &#9827; </p>";
}

add_filter('the_content', 'smart_hello_world');

Now go and see the each post, you will see “Hello World” text at the end of every post like this.

Simple HelloWorld WordPress Plugin

Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts

If you want any of my script need to be customized according to your business requirement,

Please feel free to contact me [at] muni2explore[at]gmail.com

Note: But it will be charged based on your customization requirement

Get Updates, Scripts & Other Useful Resources to your Email

Join 10,000+ Happy Subscribers on feedburner. Click to Subscribe (We don't send spam)
Every Email Subsciber could have access to download 100+ demo scripts & all future scripts.

%d bloggers like this:

Get Instant Script Download Access!