Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dwanjuki/2eefed8ec7fca65740822a4a9928e4ea to your computer and use it in GitHub Desktop.
Save dwanjuki/2eefed8ec7fca65740822a4a9928e4ea to your computer and use it in GitHub Desktop.
Display comment count on restricted posts for visitors without access
<?php
/**
* Display comment count on restricted posts for visitors without access
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_show_comment_count_after_restricted_post( $no_access_message_html ) {
// get the post ID
$post_id = get_the_ID();
// posts only
if ( false === $post_id || 'post' !== get_post_type( $post_id ) ) {
return $no_access_message_html;
}
// get approved comment count for post
$comments = wp_count_comments( $post_id );
$approved_comment_count = $comments->approved;
// Create and Add comment count HTML after restricted content message
$comment_count_html = "<p><strong>$approved_comment_count " . ( 1 === $approved_comment_count ? 'Comment' : 'Comments' ) . "</strong></p>";
$no_access_message_html .= $comment_count_html;
return $no_access_message_html;
}
add_filter( 'pmpro_no_access_message_html', 'my_pmpro_show_comment_count_after_restricted_post' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment