PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

Creating a WordPress Plugin III

Posted by on 29th Mar 2009 WordPress Coding & Design 6 comments

This is the final part on the subject of creating a WordPress plugin (read part 1 and part 2). We’ll be modifying our original function, that outputs social bookmark links after each post, so that it works with our new options page.

So first we need to modify our display function. At present it just echos out the markup, however we need it to do the following

  1. Put the markup into a variable
  2. Check which option is set
  3. If you should attach the links to the content, and the content isn’t empty, append the links variable created in point 1
  4. Else, if the content is empty (therefore the function has been called directly), echo the links variable
  5. Else, just return the original content without any links attached

Okay so let’s look at this step by step.

1. Put the markup in a variable

Before we were just echoing the markup, now it needs to go into a variable. This also means we need to alter our template tags for the permalink and post title, as the ones in use before would echo the content out, but we want to just return it so that it’s in the variable. So to do this we can use

[sourcecode language="php"] $sblinks = ‘

‘;[/sourcecode]

2. Check the option set

This is fairly straightforward. We just need to get the value of the option set by the options page

[sourcecode language="php"]$attach = get_option(‘sboption’);[/sourcecode]

3. Check on attachment and content, append links

This is where our if statement starts. We need to check that
a: We have some content passed through the action hook to the function
b: We have a setting to append the links to the content

[sourcecode language="php"]if ($attach && !empt y($content)) :
$content .= $sblinks;
return $content;[/sourcecode]

4. Else if the content is empty

This is triggered if someone calls the function directly, as they won’t be passing content through to the function as a parameter, so we can just echo the links variable out instead.

[sourcecode language="php"]elseif (empt y($content)) :
echo $sblinks;
return TRUE;[/sourcecode]

5. Else just return original content

Finally, if there is content but the option to append links is not set, then just return the content untouched.

[sourcecode language="php"]else :
return $content;
endif;[/sourcecode]

The action hook

Finally, we just need to add the action hook to get the function to run when ever post content is displayed using the_content() tag as the hook. We do this using

[sourcecode language="php"]add_filter(‘the_content’, ‘display_sblinks’);[/sourcecode]

The Final Code

So that gives us our final code for the full plugin which you can view/copy at Social Bookmark Links.

This file can just be added to your plugins directory and activated in your admin.

To target the list of links I recommend the following CSS:

[sourcecode language="css"]ul.socialbm { margin-left: 0; padding-left: 0; }
ul.socialbm li {
list-style-type: none;
float: left;
width: auto;
margin-right: 5px;
}[/sourcecode]

Plugin Enhancements

This is only a very simple plugin with just one option. You could extend this by allowing the user to select which links / networking sites to display, whether to append the links to posts only or pages as well. You could add a class to each list item to allow you to insert the social bookmark icon next to each link. The plugin is very extendable and should allow you to experiment further with the code already there.

A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

6 comments - Leave a reply
  • Posted by Paul on 29th Mar 2009

    Great finish to a great series. Thanks again Sarah, these posts have really opened the door for me on plugin development.

  • Posted by Guest on 29th Mar 2009

    I have a wordpress blog, and I didnt know it can be enhanced in so many ways..Thanks a lot!

  • Posted by woodman on 1st Apr 2009

    It is a good article,thanks for your sharing.

  • Posted by bedding sets on 5th Apr 2009

    It is good information!

  • Posted by Übersetzungsbüro NL on 22nd Apr 2011

    thanx sarah!
    very clear and informative.
    will try it on my site
    does it also work with WP 3.1 ?