<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sandbox Web Solutions &#187; WordPress</title>
	<atom:link href="http://sandbox-ws.com/category/wordpress/feed" rel="self" type="application/rss+xml" />
	<link>http://sandbox-ws.com</link>
	<description>Personal Blog of Ahmed El.Hussaini</description>
	<lastBuildDate>Mon, 23 Aug 2010 11:33:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>How to Create Your First WordPress Plugin</title>
		<link>http://sandbox-ws.com/wordpress/how-to-create-your-first-wordpress-plugin</link>
		<comments>http://sandbox-ws.com/wordpress/how-to-create-your-first-wordpress-plugin#comments</comments>
		<pubDate>Mon, 10 Aug 2009 21:49:17 +0000</pubDate>
		<dc:creator>Ahmed El.Hussaini</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://sandbox-ws.com/?p=399</guid>
		<description><![CDATA[WordPress is a very powerful and popular blogging platform. Once you start using it you will fall in love with it and sooner or later you&#8217;d want to create your own plugin to extend its functionality. So if you want to learn how to create your own WordPress plugin then you have come to the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fsandbox-ws.com%2Fwordpress%2Fhow-to-create-your-first-wordpress-plugin"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fsandbox-ws.com%2Fwordpress%2Fhow-to-create-your-first-wordpress-plugin&amp;source=sandboxws&amp;style=normal&amp;service=bit.ly" height="61" width="50" /><br />
			</a>
		</div>
<p>WordPress is a very powerful and popular blogging platform. Once you start using it you will fall in love with it and sooner or later you&#8217;d want to create your own plugin to extend its functionality. So if you want to learn how to create your own WordPress plugin then you have come to the right place.</p>
<p>A WordPress plugin is simply one or more <strong>PHP</strong> files that adhere to certain rules and conventions. All you need to understand and complete this tutorial is a basic understanding of PHP programming.</p>
<p>Before we get started, let me explain what does the plugin we&#8217;re going to write will do. It simply displays the most commented 5 posts. Pretty simple and straight forward plugin which makes it an ideal example to explore the concept behind a WordPress plugin. We will call the plugin <strong>&#8220;WP Most Commented&#8221;</strong>.</p>
<p><span id="more-399"></span></p>
<h2>Plugin Folder Structure</h2>
<p>WordPress doesn&#8217;t enforce a strict folder structure unlike most frameworks available, basically all you have to do is create a folder with the desired name (wp_most_commented in our case) under the <em><strong>plugins</strong></em> folder located in <em><strong>/wp-content/</strong></em></p>
<p>Let&#8217;s create the plugin folder:<br />
<img src="http://sandbox-ws.com/wp-content/uploads/2009/08/folder_structure.png" alt="folder_structure" title="folder_structure" width="286" height="554" class="aligncenter size-full wp-image-403" /></p>
<p>In this folder we will create the plugin main PHP file, let&#8217;s call it <em><strong>&#8220;wp-most-commented.php&#8221;</strong></em>.</p>
<h2>Activating/Deactivating the Plugin</h2>
<p>In order for WordPress to be able to activate/deactivate a plugin, we need to add PHP comments to the main file that tells WordPress about our plugin. Just copy and paste the following in the main file.</p>
<pre class="brush: php;">
&lt;?php
/*
Plugin Name: WP Most Commented
Plugin URI: http://sandbox-ws.com
Description: Displays Most Commented Posts
Version: 1.0
Author: Ahmed El.Hussaini
Author URI: http://sandbox-ws.com
*/
?&gt;
</pre>
<p>As you can see that the above comments tells WordPress some meta data about the plugin like the plugin&#8217;s name, author, version, and so on. Those meta data are used to display some info about the plugin in the Admin panel of WordPress. After saving the main file goto WordPress&#8217; admin panel, then click &#8220;Plugins&#8221; in the left side bar and you should find that our plugin was detected by WordPress.</p>
<p><img src="http://sandbox-ws.com/wp-content/uploads/2009/08/plugins_page.png" alt="plugins_page" title="plugins_page" width="616" height="48" class="aligncenter size-full wp-image-405" /></p>
<p>Now you can activate and deactivate the plugin, although it will make no difference since we haven&#8217;t added any logic to the plugin main file.</p>
<h2>Most Commented SQL Query</h2>
<p>The MySQL query we will use to retrieve the most commented 5 posts is:</p>
<pre class="brush: sql;">
SELECT ID, post_title, comment_count FROM $wpdb-&gt;posts WHERE post_status = 'publish' and comment_count &gt; 0 ORDER BY comment_count DESC LIMIT 5
</pre>
<p>You can test the query if you like but make sure to replace <em><strong>$wpdb->posts</strong></em> with your post&#8217;s table which is probably <em><strong>wp_posts</strong></em>.</p>
<p>Let&#8217;s add the function that will retrieve the most commented posts and displays them in a list. Just copy and paste the following to the plugin&#8217;s main file.</p>
<pre class="brush: php;">
function wp_most_commented() {
	global $wpdb;

	// select top most commented posts that have comment count &gt; 0 and are published
	$sql = &quot;SELECT ID, post_title, comment_count FROM $wpdb-&gt;posts WHERE post_status = 'publish' and comment_count &gt; 0 ORDER BY comment_count DESC LIMIT 5&quot;;

	// execute query
	$posts = $wpdb-&gt;get_results($sql);

	$html = '';

	$html .= '&lt;ul&gt;';

	foreach ($posts as $post) {

		$title = $post-&gt;post_title;

		$permalink = get_permalink($post-&gt;ID);

		$comment_count = $post-&gt;comment_count;

		$html .= '&lt;li&gt;';

		$html .= '&lt;a href=&quot;'.$permalink.'&quot; title=&quot;'.$title.'&quot;&gt;'.$title.'&lt;/a&gt;';

		$html .= '&lt;/li&gt;';

	}

	$html .= '&lt;/ul&gt;';

	echo $html;

}
</pre>
<h2>Testing the Plugin</h2>
<p>Open the sidebar.php file in your current active theme and place the following snippet:</p>
<pre class="brush: php;">
&lt;?php if(function_exists('wp_most_commented')): ?&gt;
	&lt;div id=&quot;most_commented&quot;&gt;
		&lt;h3&gt;Most Commented Posts&lt;/h3&gt;
		&lt;?php wp_most_commented() ?&gt;
	&lt;/div&gt;
&lt;?php endif; ?&gt;
</pre>
<p>In your sidebar you should see something similar to the following screenshot:</p>
<p><img src="http://sandbox-ws.com/wp-content/uploads/2009/08/plugin_sc.png" alt="plugin_sc" title="plugin_sc" width="314" height="139" class="aligncenter size-full wp-image-406" /></p>
<p>You&#8217;d probably want to customize the plugin main function to allow users to specify the number of posts to be displayed instead of the hard coded 5 limit, also you can add the ability to pass css classes, but I&#8217;ll this task to you.</p>
]]></content:encoded>
			<wfw:commentRss>http://sandbox-ws.com/wordpress/how-to-create-your-first-wordpress-plugin/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
