Select subscriber as author of post in admin panel?

This is a simple hack I wrote in a similar situation. It will display all the Subscribers in the Author dropdown on edit/add post/page, from where you can select any one you want. I think it should work for you…

add_filter('wp_dropdown_users', 'MySwitchUser');
function MySwitchUser($output)
{

    //global $post is available here, hence you can check for the post type here
    $users = get_users('role=subscriber');

    $output = "<select id=\"post_author_override\" name=\"post_author_override\" class=\"\">";

    //Leave the admin in the list
    $output .= "<option value=\"1\">Admin</option>";
    foreach($users as $user)
    {
        $sel = ($post->post_author == $user->ID)?"selected='selected'":'';
        $output .= '<option value="'.$user->ID.'"'.$sel.'>'.$user->user_login.'</option>';
    }
    $output .= "</select>";

    return $output;
}

The trick behind this is, after you submit submit this page, WP only reads the $user->ID from this drop down in the $_POST array, and assigns it as the posts author. And that’s what you want!

Leave a Comment

tech