So, you have decided you want to improve your WordPress blog but can’t find the exact plugin that will match you needs. WordPress allows developers to really easily build a plugin and have it do whatever you want with your blog. In this tutorial I am going to walk you through the basics of a WordPress plugin and we will build a simple plugin that puts a link to your Twitter profile at the bottom of every post.

The files

A WordPress plugin is made up of at least one PHP file. It can contain many other files including other php, HTML, CSS and JavaScript. The plugin we are going to build will just contain a single PHP file called myprofilelinks.php.

The main file for a WordPress plugin must contain a header with contains all of the information about our plugin. Here we can say who wrote the plugin, give it a name and other information. The header is included at the top of the main file and is contained within standard PHP /* */ comment tags. The header is very simple and more information can be found here.

Hooks – actions and filters

WordPress allows developers to tap into a blog’s functionality through hooks. A hook allows a developer to execute a section of code when a specific action happens or to filter the output or input to the system (a list of all hooks can be found here). In our plugin, to add an action we use the add_action() method, and to add a filter we use the add_filter() method. What we are going to need in our filter is to add a filter that is displayed when the_content is output:

<?php
add_filter('the_content', 'addFooter'); //Add a filter that will run our addFooter function when the_content is displayed
?>

Our filter

The function we have ‘hooked’ into our blog using the add_filter() method needs to accept one parameter. The function accepts the single parameter of the content we wish to filter and then returns our filtered output. The the_content filter is run just before every post or page is output to the screen and allows us to make changes to the content. Let’s take a look at what we will do with our function:

<?php
function addFooter($content) {
if(is_page()) { //If our content is a page, do nothing
return $content;
} else { //This is a blog post so let's add on our links
return $content . '<div class="myProfileLinks"><a href="http://www.twitter.com/yourprofile">Twitter</a></div>';
}
}
?>

We now have a plugin which will add on a div to all of our blog entries that will contain a link to our Twitter profile. This can be modified using CSS to make it look better, we could change the link to an image etc. The basics of this post will allow you to create your first WordPress plugin and start modifying your blog in your own way and do whatever you want. The important thing to remember with these plugins is that you can run whatever code you want and just ‘hook’ it into the blog where necessary to give the desired output.

Installing your plugin

To install your plugin you can either upload it directly to your wp-content/plugins directory or zip it up and then upload it through the plugins menu in your admin area.

Full source code

Finally, here is the full plugin source code (excluding the header):

<?php
add_filter('the_content', 'addFooter'); //Add a filter that will run our addFooter function when the_content is displayed
function addFooter($content) {
if(is_page()) { //If our content is a page, do nothing
return $content;
} else { //This is a blog post so let's add on our links
return $content . '<div class="myProfileLinks"><a href="http://www.twitter.com/yourprofile">Twitter</a></div>';
}
}
?>

Murray Picton is an experienced PHP web developer and has worked in the industry for many years as well as producing a few WordPress plugins along the way. He has a blog available at http://www.murraypicton.com containing many posts on PHP, MySQL, Javascript and many other technical subjects. If you have liked this post, please go over to his blog and take a look at his other posts.

Tags: , , , | Comments: 1 Comment

 

Related Posts


    Fatal error: Call to undefined function related_posts() in /home/gospelrh/public_html/wp-content/themes/mergerofjuneandjuly/single.php on line 28