• Resolved David Gard

    (@duck_boy)


    Hey all,

    Does anybody know of a way of adding a comment as a user other than the one that is currently logged in? I.e. as an admin, you would be able to select a user from the Users Dropdown list, type the comment and submit it as that user.

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter David Gard

    (@duck_boy)

    Ok, so it appears that there is no built in way to do this, so I had to add a custom filter as a hack.

    In www\wp-comments-post.php add this on line 89 (right above $comment_id = wp_new_comment( $commentdata );) –
    $commentdata = apply_filters('pre_override_comment_data', $commentdata);

    Add this to your functions.php file –

    /**
     * Adds a User dropdown to allow admins to assign comments to users
     */
    add_action('comment_form_top', 'set_comment_author_id');
    function set_comment_author_id(){
    	if(current_user_can('manage_options')){
    		echo '<label for="priority_menu">Add comment for user - </label>';
    		wp_dropdown_users(array('selected' => 1, 'tab_index' => 1, 'name' => 'comment_author_selection', 'class' => 'dropdown'));
    	}
    }
    
    /**
     * Gathers the information for a user chosed to assign a comment to
     * @param array $commentdata The current comment data
     * @return array The commentdata The updated comment data
     */
    add_filter('pre_override_comment_data', 'set_comment_data');
    function set_comment_data($commentdata){
    	$userdata = get_userdata((int)$_POST['comment_author_selection']); 
    
    	$commentdata['user_ID'] = $userdata->ID;
    	$commentdata['comment_author'] = $userdata->user_login;
    	$commentdata['comment_author_email'] = $userdata->user_email;
    	$commentdata['comment_author_url'] = $userdata->user_url;
    
    	return $commentdata;
    }

    And hey presto, admins (who can ‘manage_options’ in this example) can now assign a comment to a user of their choice.

    I love your script. Having a small problem with it though. It allows me to assign comments to different authors, but when a user tries to leave a normal comment (non-admin) — it’s appearing under the name “Anonymous” — any ideas?

    We actually found a temporary solution. If you assign the script to only attribute the comment to an author if a particular username is logged in. It works.

    In our case, the username “Admin” is responsible for 7-8 different authors. Just change the “ID == 9” to the correct userid that you need.

    global $current_user;
    $current_user = wp_get_current_user();
    if ($current_user->ID == 9) {
    	$commentdata = apply_filters('pre_override_comment_data', $commentdata);
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add comment on behalf of another user’ is closed to new replies.