Posted on Aug 10, 2009

How to Create Your First WordPress Plugin

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’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.

A WordPress plugin is simply one or more PHP files that adhere to certain rules and conventions. All you need to understand and complete this tutorial is a basic understanding of PHP programming.

Before we get started, let me explain what does the plugin we’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 “WP Most Commented”.

Plugin Folder Structure

WordPress doesn’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 plugins folder located in /wp-content/

Let’s create the plugin folder:
folder_structure

In this folder we will create the plugin main PHP file, let’s call it “wp-most-commented.php”.

Activating/Deactivating the Plugin

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.

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

As you can see that the above comments tells WordPress some meta data about the plugin like the plugin’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’ admin panel, then click “Plugins” in the left side bar and you should find that our plugin was detected by WordPress.

plugins_page

Now you can activate and deactivate the plugin, although it will make no difference since we haven’t added any logic to the plugin main file.

Most Commented SQL Query

The MySQL query we will use to retrieve the most commented 5 posts is:

SELECT ID, post_title, comment_count FROM $wpdb->posts WHERE post_status = 'publish' and comment_count > 0 ORDER BY comment_count DESC LIMIT 5

You can test the query if you like but make sure to replace $wpdb->posts with your post’s table which is probably wp_posts.

Let’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’s main file.

function wp_most_commented() {
	global $wpdb;

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

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

	$html = '';

	$html .= '<ul>';

	foreach ($posts as $post) {

		$title = $post->post_title;

		$permalink = get_permalink($post->ID);

		$comment_count = $post->comment_count;

		$html .= '<li>';

		$html .= '<a href="'.$permalink.'" title="'.$title.'">'.$title.'</a>';

		$html .= '</li>';

	}

	$html .= '</ul>';

	echo $html;

}

Testing the Plugin

Open the sidebar.php file in your current active theme and place the following snippet:

<?php if(function_exists('wp_most_commented')): ?>
	<div id="most_commented">
		<h3>Most Commented Posts</h3>
		<?php wp_most_commented() ?>
	</div>
<?php endif; ?>

In your sidebar you should see something similar to the following screenshot:

plugin_sc

You’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’ll this task to you.

4 Comments