SarahCreating a WordPress Plugin

Written by Sarah from Stuff By Sarah

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:

< ?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
*/
?>

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() ?>&amp;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() ?>&amp;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() ?>&amp;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.

Follow this blogger on Twitter!

Sarah Written by Sarah from Stuff By Sarah
Posted on March 15th, 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

7 Responses to “Creating a WordPress Plugin”

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

  1. Jeet says:

    @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”?

  2. Sarah says:

    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.

  3. John says:

    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

  4. If someone creates tons of these simple, but effective plugins, it is a VERY great way to gain backlinks.

    -Mike

Trackbacks

  1. [...] Creating a Wordpress Plugin. Ніколи не задумувалися про створення плагінів для вордпрес? В цій статті знайдете інформацію про те, які правильно оформлювати плагіни та що писати в інформації про плагін. [...]

  2. [...] week I explained how to take a function and turn it into a plugin for your WordPress site. The plugin was fairly basic, just a function that created a list of social [...]

  3. [...] 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 [...]

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