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
/*
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_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.
$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.
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.
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.
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_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.
Related posts:
- Twitter-like pagination using CodeIgniter and jQuery If you use Twitter you'll notice that there is a...
- Adding security to CodeIgniter forms with a custom library class The class that we are going to create within CI...
- Creating a basic iPad application Because of the recent announcement of the iPad more...
- Build a Twitter-like site with CodeIgniter and jQuery Part 2 Don't forget to check out part 1 if you...
- 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.













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.
nice one!!
Thanks, it was very helpful.
thank you very muchhh
Great article and informativ. I have this bookmarked. Thanks
thank you for this tutorial
wow. plugin wp. If i use this plugin, can my site loading low or fast
thank you. very good article
[...] The Tutorial Blog » Blog Archive » Creating a Wordpress plugin Comment (RSS) | Trackback [...]
a very useful plugin for wordpress.
I thank you very much.
I wish you continued working.
Let me know if you find something that works. I need something like this for another project.
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
thank you very good
its a plugin of first class thank you
good plugin thank you
thanks for your sharing
thank you webmaster
thanks good information
thank you for share
very nice gread article thanks
good information thanx
thank you. perfect plugin
thank you. good plugin
thanks good information
thank you. perfect plugin
thanks nice post
A link of where to download the MUO_AF plugin would be nice though, so people can get it to use or compare.