Foreword:

This tutorial will focus solely on the development of a basic wordpress plugin, it is assumed that you already have a working wordpress installation along with some basic PHP & WordPress development skills. This is part 1/2 in the series and will cover the basics only. The plugin will provide a method of appending ‘hello world’, or a custom message, to the end of a blog post. In part 2 we will look at a more realistic and real world application of a wordpress plugin.

Introduction: Develop a ‘hello world’ plugin for wordpress

Wordpress plugins provide a means to extend the default capabilities of a wordpress blog. The idea being that it is possible to alter & improve the functionality of a blog without touching the core files, making it possible to install future WordPress updates without losing any of the customisation. Common uses include social sharing widgets, seo improvements, page optimisation, e-commerce functionality and the list could go on!

If you work with WordPress on a regular basis, or intend to in the future, plugin development is probably something you should consider learning.

Tutorial Overview:

Best practices

Folder Structure

Required Files & content.

End result: A very basic plugin.

The tutorial will probably take somewhere between 10-30 minutes to complete depending on your PHP/Wordpress experience.

Step 1: A quick introduction to WordPress plugin Development.

Before we dive in and start coding, let’s take a second to understand the best practises and to get a feel for how the plugin will look.

Ensure that classes & functions all have a unique name. This can be achieved through use of a prefix.

Do not hardcode the WordPress database table prefix (usually “wp_”) into your Plugins. Be sure to use the $wpdb->prefix variable instead.

Minimize the amount of writing you do to the database – prepare everything in your code first of all, then go ahead with the inserts/updates.

Use WordPress’ APIs instead of using direct SQL where possible. For example, use get_posts() or new WP_Query() instead of SELECT * FROM {$wpdb->prefix}_posts.

Where possible use the existing database tables, rather than creating new ones. The majority of plugins can be created using the existing tables.

SELECT only what you need. Even though databases fetch data blindingly fast, you should still try to reduce the load on the database by only selecting that data which you need to use. If you need to count the number of rows in a table don’t SELECT * FROM, because all the data in all the rows will be pulled, wasting memory. Likewise, if you only need the post_id and the post_author in your Plugin, then just SELECT those specific fields, to minimize database load. Remember: hundreds of other processes may be hitting the database at the same time. The database and server each have only so many resources to spread around amongst all those processes. Learning how to minimize your Plugin’s hit against the database will ensure that your Plugin isn’t the one that is blamed for abuse of resources.

Develop in debug mode so that errors can be spotted and fixed before release. You can do so by adding ‘define('WP_DEBUG', true)' to the 'wp-config.php' file.;

To prevent a plugin from becoming unwieldy it is generally a good idea to plan the structure beforehand. Take a look at the image below for a suggested file structure:

You may want to go a step further, adding a vendor, or classes subfolder to the includes directory, for example. This is dependant on the complexity of the plugin and is also down to personal preference. As long as you are using some kind of structure then you are heading in the right direction.

Required files & content

Initially 2 files are required. The first is a PHP file with a name, preferably derived from the name of your plugin. In the example above, the plugin has been named weeb-hello-world, and as such the PHP file has the same name. This is the ‘main’ plugin file and is where your custom code should be added.

If you so choose, you can add all of your plugin code to this single file. That is not recommended however as it will make updating and maintaining the plugin a nightmare. Instead you might want to logically divide your code into separate files, using the folder structure above to keep things tidy & intuitive.

The second file required is the ‘readme’ text file. I’ll refer to the wordpress documentation on this one:

As the purpose of this tutorial is to demonstrate the process of creating a plugin, the readme is of little importance, but it’s something to remember when you decide to go ahead and create plugins in future.

Step 2: Creating the folder structure.

The first thing you need to do is create the plugin folder and setup the structure . To do so, open your wordpress folder and browse to ‘wp-content > plugins’. Create the plugin folder here, making sure to give it a unique name. Inside the folder create the required files & sub-folders as shown in the image earlier.

At this stage you should have a folder that looks like the following:

With the folder structure set, and the necessary files created, you are now ready to add some code! It’s probably worth mentioning at this point that in this tutorial you will not need to make use of the folder structure. As the plugin you are developing is only going to be a few lines of code it simply isn’t worth separating it into various files.

If you intend to follow part 2 of this tutorial series the folder structure will come in handy, so keep it at hand, ready to copy and paste. Otherwise at least you have a rough idea of how you can structure a WordPress plugin in future.

Step 3: Plugin header & activation.

The very first piece of code added to any plugin should be the header information. The header tells wordpress that your plugin exists and adds it to the plugin management screen so that it can be activated. As a minimum, the plugin name is requried, but you can provide other information such as a description or version details. The header code can be seen below – this should be added to the top of the main plugin file (‘weeb-hello-world.php’ in my example).

Plugin header

1

2

3

4

5

6

7

8

9

/*

Plugin Name: Hello-World

Plugin URI: http://weebtutorials..com/

Description: A hello world plugin used to demonstrate the process of creating plugins.

Version: 1.0

Author: John Richardson

Author URI: http://weebtutorials.com

License: GPL

*/

With that added to the main plugin file, open up WordPress admin in your browser and take a look at the plugins page. The plugin should be listed, click activate to enable the plugin.

Step 3: Using WordPress hooks to manipulate page content.

Using the hooks provided by WordPress is it possible to ‘inject’ custom code into specific parts of a site. For example, it is possible to execute code before, or after, saving a post. Another example, which is relevant to this tutorial, is the ability to add some custom content to the end of all posts e.g. “Hello world!”.

There are two types of Hooks; actions and filters.

Actions: Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Your plugin can specify that one or more of its PHP functions are executed at these points, using the Action API.

Filters: Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. Your plugin can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API.

As mentioned above, the intention of this tutorial is to modify the posts in the wordpress database – adding some custom content to the end before displaying them. The two definitions above clearly point us towards the use of filters. Let’s look at how this can be done.

Add the below code to your main plugin file:

Using wordpress hooks.

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

//Hooks a function to a filter action, 'the_content' being the action, 'hello_world' the function.

add_filter('the_content','hello_world');

//Callback function

functionhello_world($content)

{

//Checking if on post page.

if(is_single()){

//Adding custom content to end of post.

return$content."<h1> Hello World </h1>";

}

else{

//else on blog page / home page etc, just return content as usual.

return$content;

}

}

The code above will append ‘Hello World’ to the end of a post using the magic of WordPress hooks. Just so the above code is clear, ‘the_content’ is a filter which can be used to access & modify posts after they have been retrieved from the database and before they are printed to the screen. When a callback function is defined, it is important to remember that the post content will be passed to it as a parameter. That is why, in the above example, the function accepts a single parameter named ‘$content’.

By adding a filter to ‘the_content’, the default behaviour is modified and the post will not be displayed unless $content is returned. That is the reasoning behind the conditional statement; if the current page is a single post page, then return the content plus the custom message, else simply return the content as normal.

Finishing up:

At this point, the plugin is complete. If you open a post page in your browser, you should see “Hello World” has been appended to the end of the content. I’ve included an image to show this below:

Congratulations – you’ve just created your first WordPress plugin. Now it’s time to move on to something slightly more challenging! In part 2 of this tutorial series, you will learn how to create a ‘Related posts’ plugin. It will work in a similar fashion to the ‘Hello World’ plugin – in that content will be appended to the end of a post – so keep your files handy.

I’ll be working on this over the next week or so, so stay tuned.

About the Author

John Richardson: 26 years old, living in the UK. Currently works as a PHP Developer in North-East of UK.
Interested in HTML,CSS, Javascript, jQuery & PHP.

Why you have attached download link as RAR Format it is not as per Wordpress Standard It should be ZIP.
If newbie wordpress trying to install your Plugin directly it will give an error "The package could not be installed. PCLZIP_ERR_BAD_FORMAT (-10) : Unable to find End of Central Dir Record signature"
remove the RAR Format plugin and re-upload ZIP Format plugin.