1

This is a follow-up on this question.

I have now added the code from this page and it helped hiding comments pertaining to the other authors.

Now I only need to have the notification removed, as it shows up for other authors than the ones to whom the comment is addressed: enter image description here enter image description here

Is that even possible?

1 Answer 1

2

Based off of the code from another question that you referenced, I've added onto it by changing the total number of comment using jQuery:

add_filter( 'the_comments', 'wpse_236055_filter_comments' );
add_action( 'admin_head', 'wpse_236055_comment_notification' );

function wpse_236055_filter_comments( $comments ){ // Display comments related to the author
    global $pagenow, $user_ID, $comment_count;

    wp_get_current_user();
    if ( $pagenow == 'edit-comments.php' && current_user_can( 'author' ) ) {
        foreach( $comments as $i => $comment ) {
            $the_post = get_post( $comment -> comment_post_ID );
            if ( $comment -> user_id != $user_ID  && $the_post -> post_author != $user_ID ) {
                unset( $comments[$i] );
            }
        }
    }
    $comment_count = count( $comments );
    // echo '<!-- DEBUG PRINT --> <pre>'; print_r( $comment_count ); echo '</pre>';
    return $comments;
}

function wpse_236055_comment_notification( $comments ) { // Only show total count of comment related to the author
    global $pagenow, $comment_count;
    $site_name = 'Comments (' . count( $comments ) . ') ‹ ' . get_bloginfo( 'name' ) .  ' — WordPress';

    if ( !current_user_can( 'administrator' ) ) {
        ?>
        <script type="text/javascript">
            jQuery( document ).ready( function( $ ) {
                $( '.pending-count' ).html( '<?php echo $comment_count; ?>' );
                $( '.comment-count-pending' ).html( '<?php echo $comment_count; ?>' );
            } );
        </script>
        <?php
        }
    if ( $pagenow == 'edit-comments.php' && !current_user_can( 'administrator' ) ) {
        ?>
        <script type="text/javascript">
            jQuery( document ).ready( function( $ ) {
                $( document.title = '<?php echo $site_name; ?>' );
            } );
        </script>
        <?php
    }
}

As a result, the Comments menu item will only show the number of comments related to the author instead of the total number of comments. This has been tested on my end and it works.

11
  • Thanks - but won't it hide the notification for the author to whom the comment is directed also?
    – Aztrid
    Aug 16, 2016 at 10:52
  • That's correct. This function hides the notification for everyone. May you provide me an example on how a comment is directed to an author? This way I can determine the best way to approach it. Aug 16, 2016 at 13:56
  • 1
    I have a multi user blog with several authors on it. They each write blog posts under a unique name - for instance, Melissa and Camilla. I have found out how to hide Melissas blog posts from Camilla and vice versa in the backend. Also, Melissa can't see the comments directed to Camillas blog posts. But there is still a red notification marker, when a new blog post has been added to Camillas post - and Melissa can see it when she logs into her account. When she clicks it, there are no comments to show, because the comment is directed at Camillas blog post and therefore hidden for Melissa.
    – Aztrid
    Aug 17, 2016 at 14:55
  • I see. Hm... very interesting request. What have you tried on your end? Aug 19, 2016 at 2:34
  • @Aztrid, updated my code it's been tested and it works. Please give me your feedback. Aug 22, 2016 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.