Creating a WordPress plugin

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

No related posts.

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

, , , , , , , , , ,

32 Responses to Creating a WordPress plugin

  1. Quick November 24, 2008 at 6:06 pm #

    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. majestic November 30, 2008 at 6:31 pm #

    nice one!!

  3. andrei December 6, 2008 at 8:16 am #

    Thanks, it was very helpful.

  4. kurye December 7, 2008 at 9:45 am #

    thank you very muchhh

  5. Istoselidon December 11, 2008 at 8:21 am #

    Great article and informativ. I have this bookmarked. Thanks

  6. mikel December 28, 2008 at 8:38 pm #

    thank you for this tutorial

  7. mecozz January 16, 2009 at 9:10 pm #

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

  8. zayıflama January 19, 2009 at 6:59 am #

    thank you. very good article

  9. DAnTEL February 15, 2009 at 11:50 am #

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

  10. Steffan February 17, 2009 at 9:29 am #

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

  11. Inchirieri masini February 25, 2009 at 1:35 pm #

    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 March 11, 2009 at 11:54 am #

    thank you very good

  13. çeşme otel May 20, 2009 at 2:54 am #

    its a plugin of first class thank you

  14. kurye July 21, 2009 at 3:20 am #

    good plugin thank you

  15. china wholesale August 26, 2009 at 4:01 am #

    thanks for your sharing

  16. promosyon September 8, 2009 at 9:02 am #

    thank you webmaster

  17. çeşme oteller September 25, 2009 at 5:25 am #

    thanks good information

  18. kurye September 25, 2009 at 12:02 pm #

    thank you for share

  19. kurye September 28, 2009 at 11:38 am #

    very nice gread article thanks

  20. çeşme oteller September 29, 2009 at 12:20 pm #

    good information thanx

  21. düğün salonu November 1, 2009 at 5:20 pm #

    thank you. perfect plugin

  22. kurye November 2, 2009 at 4:50 am #

    thank you. good plugin

  23. indir November 8, 2009 at 9:01 am #

    thanks good information ;)

  24. cheap True Religion November 9, 2009 at 2:01 am #

    thank you. perfect plugin

  25. promosyon December 9, 2009 at 6:48 pm #

    thanks nice post

  26. horlama January 10, 2010 at 9:00 am #

    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 March 24, 2010 at 12:13 pm #

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

  28. estetik May 14, 2010 at 6:52 pm #

    a very useful plugin for wordpress.

  29. Aşk Büyüsü Nasıl Yapılır May 20, 2010 at 8:09 am #

    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 May 22, 2010 at 2:07 pm #

    thank you. perfect plugin

  31. aspirinc May 28, 2010 at 3:59 pm #

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

Trackbacks/Pingbacks

  1. S1 Team Blog » Blog Archive » Creating WordPress Plugins Tuts - January 21, 2009

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

Leave a Reply