Support » Developing with WordPress » How do I remove comments entered by a specific user?

  • Resolved lobbit

    (@lobbit)


    If I use the simple css I cannot create an automatism. So I created a plugin trying to hide that user’s comments using a script. The code is as follows:

        <?php
        function get_comment_ID() {
            $comment = get_comment();
    
            /**
            * Filters the returned comment ID.
            *
            * @since 1.5.0
            * @since 4.1.0 The <code>$comment_ID</code> parameter was added.
            *
            * @param int        $comment_ID The current comment ID.
            * @param WP_Comment $comment    The comment object.
            */
            return apply_filters( 'get_comment_ID', $comment->comment_ID, $comment );
        }
    
        if ($user_id=='9') {
            function hide_comment () {
                // Separates classes with a single space, collates classes for comment DIV        
            }?>
           <script >
                $comment_ID.css("display", "none");
           </script>
           <?php
        }
        ?>

    This code don’t work. So I have search another metod on wordpress support, but I found only code for remove the comment date. Someone can Help me? Thanx

    • This topic was modified 2 months, 2 weeks ago by Steve Stern.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Do you want to REMOVE the comments or just HIDE them?

    If you want to remove them from the database, you could connect to your database and DELETE all the rows… or change its status…

    If you want to remove them from every query, you can use the pre_get_comments action and modify the query made to select the comments.

    It would be something like (untested code)

    
    add_action('pre_get_comments', function($query) {
        if (is_admin()) return $query; // don't remove from admin    
        $query->query_vars['author__not_in'] = 9;
    });
    
    

    Good luck

    Thanx for your reply, but I need only to hide the comments a specific user.

    Sorry, I don’t understand what you are trying to do.

    Maybe you could clarify your question.

    We hypothesize that I now insert a comment in this forum… I would need the comments entered by my user to be hidden (only mine) using a script. This because I need the wordpress core to count the comments made by that user, but I don’t want them to be seen

    The way I suggested, using pre_get_comments is a good way to do it.

    The comments would be still in the database, you would still see it in the admin, but it would be hidden from the site.

    Hiding it using CSS is not a good idea because it could potentially break all your pagination… imagine this user makes a lot of comments, and you have a full page of them. A visitor would see a broken page…

    BUT, if you want to go that way, you don’t need anything fancy. Just add the user ID to a class in the comments div and later use a CSS rule to hide them.

    Something like:

    In your comments template:

    
    
    <article id="div-comment-<?php comment_ID(); ?>" class="comment-body comment-user-id-<?php echo $comment->user_id; ?>">
       ....
    

    And later, in your CSS

    
    .comment-user-id-9 {
        display: none;
    }
    
    

    Again, not a good idea. But here it is if you want.

    Hope that helps

    Thanx very much. I changed your code like this:

    <article id=”div-comment-<?php echo comment_ID() ?>” class=”comment-body comment-user-id-<?php echo $post->post_author ?>”>

    because using user_id it took me the id of the current user and not the one related to the user I wanted.

    Thank you again

    Hmmmm

    $post->post_author is getting you the ID of the POST author, not the comment author… So, in a post page it will print the same ID for all comments. It’s wrong.

    I know, but the method you referred to did not take the id of the user I want, but none at all. So, I solved it by hiding all the comments posted by that user anywhere, entering the code in the comment template. Do you have another solution?

    It did. The user id in this example is 9. Just change it for whatever you want

    
    add_action('pre_get_comments', function($query) {
        if (is_admin()) return $query; // don't remove from admin    
        $query->query_vars['author__not_in'] = 9;
    });
    

    This code, where I to add it? In function.php

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.