0

apologies I have searched either for a recently updated plugin or article and cannot find anything. I am using the standard Recent Comments widget in my sidebar. I like to respond to all comments but don't want my comments to appear in recent comments. For me the filter could work by role or user id (I am the only admin). Is this possible within say functions.php or is there a better way to achieve this - for an amateur :-)

Would very much appreciate any guidance.

2

You could try this to skip comments made by user with user_id = 1 in the Recent Comments widget:

add_action( 'widgets_init', 'custom_recent_comments' );
function custom_recent_comments(){
    add_filter( 'comments_clauses', 'custom_comments_clauses' );
}

where

function custom_comments_clauses( $clauses ){
    $clauses['where'] .= " AND user_id != 1 "; // EDIT
    return $clauses;
}

In a similar way you can target the user email with:

$clauses['where'] .= " AND comment_author_email != 'admin@example.com' "; // EDIT

This will affect the where part of all the comments queries made after the widgets_init hook.

PS: If you only want to run the where-filter once after the widgets_init hook, you can use this modification of the custom_comments_clauses() function:

function custom_comments_clauses( $clauses ){
    remove_filter( 'comments_clauses', 'custom_comments_clauses' );
    $clauses['where'] .= " AND user_id != 1 "; // EDIT
    return $clauses;
}
  • you'd better target user email instead user_id because user ID 1 might not exist at all. – JMau May 19 '13 at 17:26
  • Thank you very much. I'll give this a try but will need to get familiar with the files first. Appreciate your guidance. Really kind. – Paul Brown May 19 '13 at 18:34
  • I have tested this on my install. You can add this into your functions.php file in the current theme directory or create a special plugin file with this code snippet. If you write comments when you are logged in, the user_id field in the wp_comments table is populated. If you write a comment when you are logged out, you should go for the email solution. You should edit the part where I have marked with //EDIT. – birgire May 19 '13 at 18:46
  • @birgire thank you. I have marked as the accepted answer. Really grateful for your input and also for testing fully. Really generous. – Paul Brown May 20 '13 at 9:25
  • Hi, I don't understand what you mean by browser dependency since this is PHP code! Yes, this will exclude the user you define by user_id or email. You should consider using the remove_filter part to run this only once after the widget init hook. – birgire Jun 25 '13 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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