Create a Simple Plugin for Wordpress


In my previous post I created a plugin which was intended to allow  a widget to be created.  Now I am going to expand on this to give it more functionality.

Already it allows for a custom widget to be added, now I also want it to be able to insert other additional code to various areas.  The purpose is to make additions without editing the theme files themselves.

The previous widget only plugin I have altered to the following which registers the widget and takes the contents of phpfiles/widget.php as the content.


<?php add_action("plugins_loaded", "init_technixa");


function init_technixa(){     register_sidebar_widget("Technixa", "technixa_widget"); }
function technixa_widget($args) {      include "phpfiles/widget.php"; } 

The first thing I want to add is the ability to add to the site header, this would allow additional meta data or javascript to be added.  As long as the theme has <php wp_head(); ?> it is possible to easily add further information at this point, usually this if just before the </head> in the template.  This can be done by adding the following  lines.

<?phpadd_action('wp_head', 'technixa_header');

function technixa_header() {     include "phpfiles/header.php";}?>

This uses add_action to execute the function technixa_header whenever the wp_head hook is encountered. In order for this to work the theme should have <php wp_head(); ?> in the header.  If it doesn’t this can easily be added.  I am currently using this method to add the meta tag for googles webmaster tools.

Similarly the following two lines can be used to do the same for the footer, the contents of the file being added where <php wp_footer() ?>


<?phpadd_action('wp_footer', 'technixa_footer');

function technixa_footer() {     include "phpfiles/footer.php"; }?>

Currently my footer.php contains my Google Analytics code.

The Final thing I wanted to achieve was to add content before or after the post content itself.  Again I managed this with a few lines.

<?phpadd_action('the_content', 'technixa_content'); function technixa_content($content) { include "phpfiles/content_top.php"; echo $content;      include "phpfiles/content_bottom.php"; }?>

This will show the post content with the two files before and after.  At the moment I am using it to show the addthis button at the bottom and the google ad at the top.  I was worried that this method would interfere with other addons, however, when I installed YARPP which adds related posts at the end of the post there didn’t seem to be any problem.

I am not sure that the method I have used to add content to the post is the best way,  if I discover a better way I will update this post.

So there we have my first ever Wordpress Addon, this is a simple yet powerful addon which can be used to make additions to themes without the need to change the actual theme file.

You can download the full commented file here.  To make changes change the php files contained within the phpfiles folder with whatever content you want.

Bookmark and Share

    Trackbacks/Pingbacks

    1. Creating a Widget for Wordpress. | Technixa
    2. Adding Options to Wordpress Widget | Technixa
    3. Making an options page for Wordpress Plugin | Technixa

    Leave a Reply

    Powered by WP Hashcash