Create A Very Simple HelloWorld WordPress Plugin Example

11

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.

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.

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.

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.

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.

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.

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.

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.

 

Leave A Reply

Your email address will not be published.