Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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