4

When I reply to a comment in the WP backend in the "Comments" section (edit-comments.php) I can only post the reply as the logged in user. Is it possible to select a certain user like in the "Add new post" section (post-new.php) where I have a dropdown with all users that are allowed to write posts?

2

Yes, add an user-select field in the comment form:

add_action('comment_form_after_fields', function(){

  // allow only users who can moderate comments to do this
  if(!current_user_can('moderate_comments'))
    return;

  $user = wp_get_current_user();

  wp_dropdown_users(array(
    'name'     => 'alt_comment_user',
    'selected' => $user->ID,
  ));

});

When the form gets processed, check if an user has been selected and change the comment data before it gets inserted in the database:

add_filter('preprocess_comment', function($input){

  if(current_user_can('moderate_comments') && isset($_POST['alt_comment_user'])){    

    $user = get_user_by('id', (int)$_POST['alt_comment_user']);

    $my_fields = array(
      'comment_author'       => empty($user->display_name) ? $user->user_login : $user->display_name,
      'comment_author_email' => $user->user_email,
      'comment_author_url'   => $user->user_url,
      'user_ID'              => $user->ID,
    );

    // escape for db input
    foreach($my_fields as &$field)
      $field = $GLOBALS['wpdb']->escape($field);

    $input = $my_fields + $input;   
  }  

  return $input;
});

ref: wp_dropdown_users

  • Thank you very much, this works for me! Is it also possible to include this function to the WP backend in the comments section when I reply to a comment? Example – user1706680 Oct 4 '12 at 14:10
0

I couldn't get the method by @onetrickpony to work within 20 minutes or so, and granted it's been a while since his post, so things may have changed.

I ended going free plugin shopping and found the Switch Users plugin, which just worked straight out of the box. I have my main user disabled in WordFence and only login as my [renamed] admin user with MFA. Anyone who tries to login as my main user will instantly get blocked.

With the Switch Users plugin, when I want to comment as my main user, I can easily switch to that user from within the WP Admin Console, reply to comments, and switch back to my admin user.

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.