How to Add Pending Comments Link in Post Footer

If you manage an active blog where lots of comments are posted each day, you will find it easier to jump to pending comments page for a post from your homepage.

Add the following code in your theme functions file (functions.php) which will add a link to pending comments page with number of pending comments directly below the post content.

add_filter('the_content', array($this, 'pendingCommentsQuickLink'));

function pendingCommentsQuickLink($content)
{
	if(current_user_can('moderate_comments')) {
		global $wpdb;

		/*
		 * Fetch Pending Comments from Database
		 */
		$pendingComments = $wpdb->get_var('SELECT COUNT(*) AS pendingComments FROM '.$wpdb->comments.' WHERE comment_post_ID = '.get_the_id().' AND comment_approved = 0 AND comment_approved != "spam"');

		$content .= '<p>';

		/*
		 * Parse Pending Comments
		 */
		if($pendingComments):
		$content .= ' <a class="pendingCommentsLink" href="'.get_bloginfo('wpurl').'/wp-admin/edit-comments.php?comment_status=moderated&p='.get_the_id().'">Pending Comments ('.$pendingComments.')</a>';
		endif;

		$content .= '</p>';
	}
	return $content;
}

How does it look like?

How to Add Pending Comments Link in Post Footer

2 Comments
  1. Looks pretty neat. Thanks for the idea.

  2. is it compatible with wordpress 2.9.2?
    because i am getting errors… thanks

Leave a Reply