Get the Official WPSaviour App Now!

Blog

How to Create a Comments Central Page Template in WordPress

One approach to enhance customer engagements is to reward their feedback by showcasing them in your web site. Moreover, you can too function the highest commenters as nicely, linking again to their web site within the course of. Right here we’ll create a devoted Web page Template to show these feedback and commenters in a single place.

In brief, this tutorial will train you find out how to:

  1. create a Web page Template,
  2. use SQL queries in your code to fetch feedback with various parameters,
  3. create a piece of the Web page that’s solely viewable by Admin,
  4. add assist for a comment-related plugin.

Making a Web page Template

The best approach to create a Web page Template is to open the web page.php file in your theme, which can roughly appear like this:

<?php get_header(); ?>
  <div id="content material">
    <?php if (have_posts()) : whereas (have_posts()) : the_post(); ?>
    <div class="submit" id="post-<?php the_ID(); ?>">

    <h2 class="page_title"><?php the_title(); ?></h2>
      <?php the_content(); ?>
    </div>
    <?php comments_template(); ?>
    <?php endwhile; endif; ?>
  </div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Copy and paste web page.php’s content material and add this to the very high:

<?php
/*
Template Identify: Feedback Central
 */
?>

And put it aside. There’s no actual guidelines on naming a Web page Template file, nevertheless it’s a good suggestion to go along with a prefix to make it recognizable, say “pt-comment-central.php”. We haven’t added something into this Web page Template, nevertheless it’s up and working and selectable on the write new Web page dashboard space.

Fetching Feedback

For this Web page Template, we’ll function 4 completely different points of feedback:

  • Latest Feedback,
  • Latest Trackbacks / Pingbacks,
  • Prime Commenters,
  • Most Commented Posts,

First, we’ll do Latest Feedback:

<h3>Latest Feedback</h3>  
<ul id="cc-recent-comments">      
<?php
  $max = 7; // quantity merchandise to get
  international $wpdb;
  $sql = "SELECT c.*, p.post_title FROM $wpdb->feedback c INNER JOIN $wpdb->posts p ON (c.comment_post_id=p.ID) WHERE comment_approved = '1' AND comment_type not in ('trackback','pingback') ORDER BY comment_date DESC LIMIT $max";
  $outcomes = $wpdb->get_results($sql);
  
  $template = '%g <a href="%au">%an</a> on <a href="%pu#comment-%cid">%pt</a>';
  
  $echoed = 0;
  foreach ($outcomes as $row) {
    $tags = array('%ct','%cd','%g','%pt','%pu','%au','%an','%cid');
    $replacements = array($row->comment_title,$row->comment_date,get_avatar($row->comment_author_email,'32'),$row->post_title,get_permalink($row->comment_post_ID),$row->comment_author_url,$row->comment_author,$row->comment_ID);
    echo '<li>' . str_replace($tags,$replacements,$template) . '</li>';
    $echoed = 1;
  }
  if ($echoed==0)
      echo '<li>No remark discovered.</li>';        
?>
</ul>

The SQL question asks for all authorised feedback sorted by date (newest first). $max is the place we set the quantity of feedback to get, 7 in our case. The output of the code above will likely be an unordered checklist of latest feedback:

List of Recent Comments

With a little bit CSS we will straighten that to look higher:

#cc-recent-comments li {
  width: 100%;
  float: left;
  list-style-type: none;
}

#cc-recent-comments li img {
  float: left;
  margin-top: -5px;
}

List of Recent Comments with proper CSS

$template determines how the precise textual content will likely be written; that is based mostly on the format made by WP Remark Remix, and you may observe that hyperlink to be taught extra on customizing it (search for ‘tokens’).

Subsequent is Latest Pingbacks / Trackbacks:

<h3>Latest Pingbacks / Trackbacks </h3>
<ul id="cc-recent-trackbacks">        
<?php
  $sql = "SELECT c.*, p.post_title FROM $wpdb->feedback c INNER JOIN $wpdb->posts p ON (c.comment_post_id=p.ID) WHERE comment_approved = '1' AND comment_type not in ('trackback','pingback') ORDER BY comment_date DESC LIMIT $max";
  $outcomes = $wpdb->get_results($sql);
  
  $template = '%g <a href="%au">%an</a> on <a href="%pu#comment-%cid">%pt</a>';
  
  $echoed = 0;
  foreach ($outcomes as $row) {
    $tags = array('%ct','%cd','%g','%pt','%pu','%au','%an','%cid');
    $replacements = array($row->comment_title,$row->comment_date,get_avatar($row->comment_author_email,'32'),$row->post_title,get_permalink($row->comment_post_ID),$row->comment_author_url,$row->comment_author,$row->comment_ID);
    echo '<li>' . str_replace($tags,$replacements,$template) . '</li>';
    $echoed=1;
  }
  if ($echoed==0)
    echo '<li>No remark discovered.</li>';          
?>
</ul>

The code above is similar to the one we now have for Latest Feedback, the one variations being that we’re now asking for feedback with ‘comment_type’ beneath ‘pingback’ / ‘trackback’, and the template is a bit completely different as nicely. Outcome:

List of Recent Pingbacks and Trackbacks

Right here’s the code for Prime Commenters:

<h3>Prime Commenters</h3>
<ul id="cc-top-commenters">
<?php
$sql = "SELECT comment_author, comment_author_url, comment_author_email, depend(comment_ID) as comment_count FROM $wpdb->feedback WHERE comment_approved = '1' AND comment_type not in ('trackback','pingback') GROUP BY comment_author, comment_author_url, comment_author_email ORDER BY comment_count DESC LIMIT $max";
$outcomes = $wpdb->get_results($sql);

$template = '<a href="%au">%g %an</a> (%c feedback)';

$echoed = 0;
foreach ($outcomes as $row) {
    $tags = array('%g','%au','%an','%c');
    $replacements = array(get_avatar($row->comment_author_email,'32'),$row->comment_author_url,$row->comment_author,$row->comment_count);
    echo '<li>' . str_replace($tags,$replacements,$template) . '</li>';
    $echoed = 1;
}
if ($echoed==0)
    echo '<li>No commenter discovered.</li>';  
?>
</ul>

Nothing too mind-blowing there. Do discover the cool get_avatar() operate, although, which will provide you with the Gravatar for anybody whose e-mail handle you specify. On this case, we fetch the avatar picture utilizing the commenter’s e-mail handle. With the CSS much like the one we now have for latest feedback, we will have this outcome:

#cc-top-commenters li {
  width: 100%;
  float: left;
  list-style-type: none;
}

#cc-top-commenters li img {
  float: left;
  margin-top: -5px;
}

List of Top Commenters

Final is Most Commented Posts:

<h3>Most Commented Posts</h3>
<ul id="cc-most-comments">
$sql = "SELECT p.*, c.comment_count FROM $wpdb->posts p INNER JOIN (SELECT comment_post_id, depend(comment_ID) as comment_count from $wpdb->feedback WHERE comment_approved='1' GROUP BY comment_post_id) c ON (c.comment_post_id=p.ID) ORDER BY c.comment_count DESC LIMIT $max";
$outcomes = $wpdb->get_results($sql);

$template = '<a href="%pu">%pt</a> (%c feedback)';

$echoed = 0;
foreach ($outcomes as $row) {
  $tags = array('%pd','%pt','%pu','%c');
  $replacements = array($row->post_date,$row->post_title,get_permalink($row->ID),$row->comment_count);
  echo '<li>' . str_replace($tags,$replacements,$template) . '</li>';
  $echoed = 1;
}
if ($echoed==0)
    echo '<li>No commenter discovered.</li>';
?>
</ul>

List of Most Commented Post

And that’s it. Subsequent, we’ll add some further coolness by including some stuff that solely the admin can see.

Admin-only Data

To point out stuff just for the admins, we will use this code snippet from WPCandy:

<?php 
international $user_ID; 
if( $user_ID ) :
  if( current_user_can('level_10') ) :
  // admin-only stuff right here.
  endif;
endif; ?>   

Now on the Dashboard, we get a fast look of a web site’s whole, authorised, pending overview and spam feedback. Let’s replicate this for our Web page Template for simpler, admin-only entry:

<?php 
  $num_comm = wp_count_comments();
?>
Complete Feedback: <a href="<?php bloginfo('wpurl'); ?>/wp-admin/edit-comments.php?"><?php echo $num_comm->total_comments; ?></a>
Accepted: <a href="<?php bloginfo('wpurl'); ?>/wp-admin/edit-comments.php?comment_status=authorised"><?php echo $num_comm->authorised; ?></a> 
Moderated: <a href="<?php bloginfo('wpurl'); ?>/wp-admin/edit-comments.php?comment_status=moderated"><?php echo $num_comm->moderated; ?></a>
Spam: <a href="<?php bloginfo('wpurl'); ?>/wp-admin/edit-comments.php?comment_status=spam"><?php echo $num_comm->spam; ?> </a>

Admin

wp_count_comments() is a neat operate that returns an array of assorted remark stat numbers. We’re including hyperlinks to the respective remark administration space too.

Including Some Sparks

Final, say you discover a cool comment-related plugins you wish to incorporate into this Web page Template. As a substitute of including extra codes, let’s simply add assist for it. For this instance, I’ll go along with Exercise Sparksplugin, which might “show a ‘sparkline’ model graph in your sidebar point out submit and/or remark exercise. ” Sounds nice to me.

Often, a plugin’s readme.txt file will train you find out how to add it into your theme recordsdata. In our case, the code may be like this:

<?php 
  if(function_exists('activitysparks')) {
    activitysparks(array('dataset'=>'legend','height_px'=>100,'width_px'=>600,'interval'=>30, 'ticks'=>24));	
  }
?>     

ActivitySparks plugin

function_exists() checks whether or not a selected operate is obtainable; in our case, the activitysparksoperate, which will likely be obtainable if the plugin has been uploaded and activated. If it’s there, we present the graph. If not, then our Web page Template gained’t present something (however it’s going to nonetheless run simply effective, no errors).

Outcome and Instance

An instance of this Web page Template is obtainable right here. It makes use of the codes you see right here with a number of modifications, largely to maintain the HTML construction in step with the remainder of the web site. The entire code for that Web page Template is obtainable at Pastebin.

Credit and Additional Readings

  • The codes used to show the varied latest and high feedback are taken from WP Remark Remix plugin. Check out its code to be taught extra issues to do with feedback, like excluding admin feedback from high commenters or find out how to consider latest feedback solely. The $template we use to format the output may be discovered extra from that web page, too.
  • Creating Your Personal Web page Template on the WordPress Codex.
  • Displaying admin-only content material tutorial on WPCandy.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBlogShowcase

7 Best Shopify Alternatives in 2020 (Cheaper and More Powerful)

Beginner’s GuideBest WordPress PluginsBlog

14 Best AI Chatbots Software for Your Website (Comparison)

Beginner’s GuideBlogTips

Magento vs WooCommerce – Which one is Better? (Comparison)

Beginner’s GuideBlogTips

12 Best Calculator Plugins for Your WordPress Site

Sign up for our Newsletter and
stay informed

Leave a Reply

Your email address will not be published.

Fear Of Missing Out?

Sign up with your email address to receive WordPress tips and updates
SUBSCRIBE NOW
Terms and Conditions apply
close-link
Click Me