
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: https://webproggi.com Description: A plugin that shows comments amount of posts Version: 1.0 Author: Webproggi.ru Author URI: https://webproggi.com 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 = "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> = ".$post->ID;
$comm_num = $wpdb->get_results($sql);
if (count($comm_num[0]->comm_num)>0){
$cm = "";
$cm = $cm."<img src=""".WP_PLUGIN_URL."/wp-visible-comments/images/comm_num.gif"" width="14" height="15" alt="Comments:" title="Comments" /> ";
$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.

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.