SarahCreating a WordPress Plugin III

Written by Sarah from Stuff By Sarah

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

	$sblinks = '<ul id="social">
<li>Bookmark this page at: </li>
<li><a href="http://del.icio.us/post?url='.get_permalink($post->ID).'&amp;title='.urlencode($post->post_title).'" title="Bookmark '.$post->post_title.' on Del.icio.us">Del.icio.us</a> - </li>
<li><a href="http://www.stumbleupon.com/submit?url='.get_permalink($post->ID).'&amp;title='.urlencode($post->post_title).'" title="Submit '.$post->post_title.' to StumbleUpon">StumbleUpon</a> - </li>
<li><a href="http://www.spurl.net/spurl.php?url='.get_permalink($post->ID).'&amp;title='.urlencode($post->post_title).'" title="Bookmark '.$post->post_title.' on Spurl">Spurl</a></li>
</ul>';

2. Check the option set

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

$attach = get_option('sboption');

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

if ($attach && !empt y($content)) :
	$content .= $sblinks;
	return $content;

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.

elseif (empt y($content)) :
	echo $sblinks;
	return TRUE;

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.

else :
	return $content;
endif;

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

add_filter('the_content', 'display_sblinks');

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:

ul.socialbm { margin-left: 0; padding-left: 0; }
ul.socialbm li {
	list-style-type: none;
	float: left;
	width: auto;
	margin-right: 5px;
}

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.

Follow this blogger on Twitter!

Sarah Written by Sarah from Stuff By Sarah
Posted on March 29th, 2009 and filed under WordPress Coding & Design
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx

5 Responses to “Creating a WordPress Plugin III”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Paul says:

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

  2. Guest says:

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

  3. woodman says:

    It is a good article,thanks for your sharing.

  4. bedding sets says:

    It is good information!

Trackbacks

  1. [...] of a three part series on BloggingTips.com on how to create a basic WordPress plugin.  Part 2 and part 3 are also [...]

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Follow us on Facebook Find Out More About Our Newsletter

Sponsors

Blogging Tips Newsletter

Six Figure Blogging

 

Our Free E-Books