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
Okay so let’s look at this step by step.
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).'&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).'&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).'&title='.urlencode($post->post_title).'" title="Bookmark '.$post->post_title.' on Spurl">Spurl</a></li> </ul>';
This is fairly straightforward. We just need to get the value of the option set by the options page
$attach = get_option('sboption');
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;
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;
Finally, if there is content but the option to append links is not set, then just return the content untouched.
else : return $content; endif;
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');
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;
}
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.
Author comments are in a darker gray color for you to easily identify the posts author in the comments
[...] 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 since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums
Great finish to a great series. Thanks again Sarah, these posts have really opened the door for me on plugin development.
I have a wordpress blog, and I didnt know it can be enhanced in so many ways..Thanks a lot!
It is a good article,thanks for your sharing.
It is good information!