Creating a WordPress plugin Comments (32)
by Wez Pyke - November 21, 2008 in PHP,Text Tutorials

The plugin that I'm going to show you how to create within this post is a plugin that I created for MakeUseOf.

Creating plugins for WP is relatively easy and uses actions to call functions that are included in pages within the main WP files. To learn more about creating plugins for WP go here.

Ok so lets get started with creating the plugin.

When creating a plugin for WP make sure that you include your own unique word or acronym to your functions so that they don't conflict with any other plugins that may be installed.

Because I created this for MakeUseOf the acronym I use is MUO and then AF because I named the plugin author footer so every function within this plugin begins with MUO_AF.

So to start our plugin we add this

<?php
/*
Plugin Name: MUO Author Footer
Description: Authors type in the footer that will be attached to the end of their articles with modifications
Author: Wez
Version: 1.0
*/

Here is where we add the name of our plugin, a description, your name and the version number of your plugin.

The next bit of code that we add is the register_activation_hook and register_deactivation_hook so that when the plugin is activated and deactivated the code within functions that we will create in our next step will be executed.

register_activation_hook(__FILE__, 'MUO_AF_activate');
register_deactivation_hook(__FILE__, 'MUO_AF_deactivate');

We're now going to create two functions called MUO_AF_activate and MUO_AF_deactivate so that the register hooks work correctly.

function MUO_AF_activate() {
$query = mysql_query("CREATE TABLE `muo_author_footer` (
`author_id` int(11) NOT NULL,
`author_name` varchar(50) NOT NULL,
`author_signature` text NOT NULL,
PRIMARY KEY (`author_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;");
}

function MUO_AF_deactivate(){
$query = mysql_query("DROP TABLE `muo_author_footer`");
}


What this code does is when the plugin is activated it will create a table within the MySQL database that the plugin will use to store and retrieve information. When the plugin is deactivated it will delete the table that was created in the database.

In the next step we will write code that will be called and displayed within the Your Profile page in the admin-cp.

function MUO_AF_profile() {
global $userdata;
get_currentuserinfo();
$query = mysql_query("SELECT * FROM muo_author_footer WHERE author_id='$userdata->ID'");
$row = mysql_fetch_array($query);
$author_signature = $row['author_signature'];
echo '<table class="form-table">
<tr><th>Author Post Signature

<td><p>
<textarea name="MUO_Author_Sig" cols="30" rows="5">'.$author_signature.'</textarea>
</p>
<h2>Preview:</h2><p><em>';
echo $author_signature;
echo '</em></p>
</td></tr>
</table>';
}

When the Update Profile button is clicked the information from the previous bit of code will be retrieved in the next bit of code and the database will be updated.

function MUO_AF_edit(){
global $userdata, $current_user;
get_currentuserinfo();

$MUO_Author_Sig = $_POST['MUO_Author_Sig'];

$query = mysql_query("SELECT * FROM muo_author_footer WHERE author_id='$userdata->ID'");

if( mysql_num_rows($query) > 0 ) {
$query = mysql_query("UPDATE muo_author_footer SET author_signature='$MUO_Author_Sig' WHERE author_id='$userdata->ID'");
} else {
$query = mysql_query("INSERT INTO muo_author_footer VALUES ('$userdata->ID','$userdata->user_login','$MUO_Author_Sig')");
}

}

The code we will write now is the code that will grab the contents of the post and then attach the authors signature to the end of that post.

function MUO_AF_publish_post( $content ) {
global $wpdb;
$num = 0;
if (get_the_ID() > $num) {
$the_author_id = get_the_author_id();
$author_query = mysql_query("SELECT * FROM muo_author_footer WHERE author_id='". $the_author_id ."'");
$author_row = mysql_fetch_array($author_query);
$author_signature = $author_row['author_signature'];

$content .= '<style>
.muo_af_style { background-color: #B4D0E1; border: 2px solid #4275B8; }
</style>';

if(strstr($author_signature, 'by') == false) :

$content .= '<p class="muo_af_style"><em>(By) '.$author_signature.'</em></p>';

else :

$content .= '<p class="muo_af_style"><em>'.$author_signature.'</em></p>';

endif;

return $content;
}
else {
return $content;
}
}

We will now add the functions to actions, this is the most important part of the code because without this none of the functions would be called and nothing would happen.

add_action('show_user_profile', 'MUO_AF_profile');
add_filter('profile_update', 'MUO_AF_edit');
add_action('the_content', 'MUO_AF_publish_post');

Then at the end of the file close the php tags with

?>

I have not been very descriptive in this tutorial of what the actual code within the plugin is doing because I created this tutorial wanting to show people the basics bit of code that are needed to call functions and make a plugin work.

I hope that you have learnt something by reading this tutorial and feel free to post a comment with your thoughts on this post.

Bookmark and Share
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • FriendFeed
  • LinkedIn
  • Ping.fm
  • Tumblr
  • Twitter

Related posts:

  1. Twitter-like pagination using CodeIgniter and jQuery If you use Twitter you'll notice that there is a...
  2. Adding security to CodeIgniter forms with a custom library class The class that we are going to create within CI...
  3. Creating a basic iPad application Because of the recent announcement of the iPad more people...
  4. Build a Twitter-like site with CodeIgniter and jQuery Part 2 Don't forget to check out part 1 if you have...
  5. Twitter-like pagination using CakePHP and jQuery I have already done this tutorial with CodeIgniter and jQuery...

Related posts brought to you by Yet Another Related Posts Plugin.

Your Ad Here

32 Responses to “Creating a WordPress plugin”

  1. Quick says:

    Thanks for simplifying it.

    A link of where to download the MUO_AF plugin would be nice though, so people can get it to use or compare.

  2. andrei says:

    Thanks, it was very helpful.

  3. kurye says:

    thank you very muchhh

  4. Istoselidon says:

    Great article and informativ. I have this bookmarked. Thanks

  5. mikel says:

    thank you for this tutorial

  6. mecozz says:

    wow. plugin wp. If i use this plugin, can my site loading low or fast

  7. zayıflama says:

    thank you. very good article

  8. [...] The Tutorial Blog » Blog Archive » Creating a WordPress plugin Comment (RSS)  |  Trackback [...]

  9. DAnTEL says:

    a very useful plugin for wordpress.
    I thank you very much.
    I wish you continued working.

  10. Steffan says:

    Let me know if you find something that works. I need something like this for another project.

  11. I happened upon this site while following the links from another site. Your site is wonderful and i bookmarked it. Thank your for the hard work you must have put in to create this wonderful facility. Keep up the excellent work

  12. kurye says:

    thank you very good

  13. çeşme otel says:

    its a plugin of first class thank you

  14. kurye says:

    good plugin thank you

  15. thanks for your sharing

  16. promosyon says:

    thank you webmaster

  17. thanks good information

  18. kurye says:

    thank you for share

  19. kurye says:

    very nice gread article thanks

  20. good information thanx

  21. thank you. perfect plugin

  22. kurye says:

    thank you. good plugin

  23. indir says:

    thanks good information ;)

  24. thank you. perfect plugin

  25. promosyon says:

    thanks nice post

  26. horlama says:

    A link of where to download the MUO_AF plugin would be nice though, so people can get it to use or compare.

  27. huzurevi says:

    a very useful plugin for wordpress.
    I thank you very much.

  28. estetik says:

    a very useful plugin for wordpress.

  29. A link of where to download the MUO_AF plugin would be nice though, so people can get it to use or compare.

  30. check up says:

    thank you. perfect plugin

  31. aspirinc says:

    A link of where to download the MUO_AF plugin would be nice though, so people can get it to use or compare.

Leave a Reply