Simple plugin to display the number of comments in WordPress

Simple plugin to display the number of comments in WordPress
In this article, we will talk about writing a simple plugin for the already popular content management system WordPress. The main task is to show how easy it is to start writing plugins for this system, since sooner or later there is a need to add something of your own, without spending time searching for the right one among ready-made solutions.

So, let's get started, the task of the plugin is to display the total number of comments for the post. Such a solution, for example, will help users see the total number of comments, being in any part of the site where you have a link to any post.

In order to write a plugin for WordPress you need very little:
1. First, create a folder in wp-contentplugins, call it a unique – friendly name.
2. Create a file in the folder that will contain the functional content of your plugin.

So, let's create a wp-visible-comments folder in wp-contentplugins. The name of the folder corresponds to the essence of the plugin and is quite unique, so overlap with other plugins is unlikely.

Create a wp-visible-comments.php file in the folder. It will contain all the necessary functionality and description. The main part of this file is a description, in it you need to specify the name of the plugin, a link to the developer page, a description, a version number, etc. In principle, by simply creating a file with such a description, you will already have a fully working empty plugin that you can manage from the admin panel:

   Plugin Name: VisibleCommentsPages for wordpress
   Plugin URI:
   Description: A plugin that shows comments amount of posts 
   Version: 1.0
   Author URI:
   License: GPL2

But to display comments, you need to add functionality. In WordPress, comments for each post are displayed in a separate wp-comments table, that is, you just need to count the number of posts of the desired post in this table and display in a convenient form. For convenience, let's create a function that will be responsible for output, and call it viscomments_view:

	 * Shows comments amount for post
	 * Find comments number for post and show it 
	 * @global object $post Post object
	 * @return integer $post_comments Comments number
function viscomments_view() {
		global $post, $wpdb;
$sql = &quot;SELECT count(<code>w</code>.<code>comment_post_id</code>) as comm_num FROM <code>$wpdb->comments</code> w where <code>w</code>.<code>comment_post_id</code> = &quot;.$post->ID;
		$comm_num = $wpdb->get_results($sql);
if (count($comm_num[0]->comm_num)>0){		
	   		$cm = &quot;&quot;;
	   		$cm = $cm.&quot;<img src="&quot;&quot;.WP_PLUGIN_URL.&quot;/wp-visible-comments/images/comm_num.gif&quot;" width="14" height="15" alt="Comments:" title="Comments" /> &quot;;
	   		$cm = $cm.$comm_num[0]->comm_num;
	   		echo $cm;	

In this function, a regular request is formed, from which we get the number of comments for the desired post.

For better visualization, an images folder was created inside the plugin, in which a picture displaying comments is placed. Working with it is also displayed in the viscomments_view function.

Simple plugin to display the number of comments

At this point, the work with the plugin is finished. For it to work, you just need to activate it from the admin panel and insert a call to the function viscomments_view(); in the place where you need to show the number of comments.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a comment

Your email address will not be published.