Over the past couple of months we’ve started to look at creating functions and using actions and filters in our functions.php theme file to help manipulate the content output by WordPress. If you create something and feel it’s worthy of releasing for others to use then rather than just posting the code up for others to copy, it’s usually better to write a plugin.
To start a plugin file off you use a PHP comment to contain the information about the plugin. This is:
[sourcecode language="php"]< ?php
/*
Plugin Name: Put your plugin name here
Plugin URI: http://www.yourdomain.com/path-to-plugin-page/
Description: Plugin description here
Version: 1.0
Author: Your Name
Author URI: http://www.yourdomain.com
*/
?>[/sourcecode]
The above tells WordPress information about your plugin and this is the information that gets displayed on the plugins page in the admin section.
After this you can then add your functions and hooks/filters if needed. If we take the simple function we first looked at, to output social bookmarking links, then we would simply add the function below the comments eg.
<?php
/*
Plugin Name: Output Social Bookmarks
Plugin URI: http://www.bloggingtips.com/wordpress-plugins/social-bookmarks/
Description: Displays Social Bookmarks for the current post.
Version: 1.0
Author: SarahG
Author URI: http://www.bloggingtips.com
*/
function display_sblinks($cssid = "social") {
global $post;
?>
<ul id="<?php echo $cssid ?>">
<li>Bookmark this page at: </li>
<li><a href="http://del.icio.us/post?url=<?php the_permalink() ?>&title=<?php echo urlencode(the_title('','',FALSE)); ?>" title="Bookmark <?php the_title() ?> on Del.icio.us">Del.icio.us</a> - </li>
<li><a href="http://www.stumbleupon.com/submit?url=<?php the_permalink() ?>&title=<?php echo urlencode(the_title('','',FALSE)); ?>" title="Submit <?php the_title() ?> to StumbleUpon">StumbleUpon</a> - </li>
<li><a href="http://www.spurl.net/spurl.php?url=<?php the_permalink() ?>&title=<?php echo urlencode(the_title('','',FALSE)); ?>" title="Bookmark <?php the_title() ?> on Spurl">Spurl</a></li>
</ul>
<?php
}
?>
You would save this as a standard PHP file and then put it in your plugins directory. When you then go into the plugins page you’ll see it as an unactivated plugin ie. just like any other plugin you’ve added to your site.
Next week I’ll explain how to give more flexibility and control over your plugins with an options page.








@Sarah: You make things look super easy as usual
I don’t see any hooks (filter) etc. in the code. I wonder how / when will the display_sblinks will be executed. Do you want to put it after “comment_text”?
Hi Jeet, thanks for the comment. This example was just a simple example of how to load a function into WordPress for your use. There's no hooks or filters involved as the function automatically outputs the markup. If you look at the original post on the function (here) then you'll see how to call it by just calling the function's name.
Next week I'll show you how you can alter the above function to then work with a filter, along with the options page.
Great tip! I never knew that WP used simple PHP commands for Plugins. I always expected it to be much more complex. Excellent representation of the method though.
Thanks,
~John
If someone creates tons of these simple, but effective plugins, it is a VERY great way to gain backlinks.
-Mike
I never knew that,.,.,thanks for pointing that out,im have been thinking of an idea for a WP plugin just for fun but i did not know it can gain backlinks, this is a good thing.