Select subscriber as author of post in admin panel?

I want to be able to select a subscriber an author of a post in the admin so it displays their name as having written the post, but I do not want to give them any additional privileges (if they login the only thing they can access is their profile).

Is there a simple way to do this without having to change roles and capabilities?

Read More

Thanks

Related posts

Leave a Reply

7 comments

  1. 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!

  2. As of WordPress 4.4.0 you can now use the wp_dropdown_users_args filter. The code is much simpler now:

    add_filter( 'wp_dropdown_users_args', 'add_subscribers_to_dropdown', 10, 2 );
    function add_subscribers_to_dropdown( $query_args, $r ) {
    
        $query_args['who'] = '';
        return $query_args;
    
    }
    
  3. This is similar approach to @brasofilo. But only works in the edit post screen, rather than quick edit, and includes all users (not just authors and subscribers).

    /* Remove Author meta box from post editing */
    function wpse50827_author_metabox_remove() {
        remove_meta_box('authordiv', 'post', 'normal');
    }
    add_action('admin_menu', 'wpse50827_author_metabox_remove');
    
    
    /* Replace with custom Author meta box */
    function wpse39084_custom_author_metabox() {  
        add_meta_box( 'authordiv', __('Author'), 'wpse39084_custom_author_metabox_insdes','post');  
     } 
    add_action( 'add_meta_boxes', 'wpse39084_custom_author_metabox');  
    
    
    /* Include all users in post author dropdown*/
    /* Mimics the default metabox http://core.trac.wordpress.org/browser/trunk/wp-admin/includes/meta-boxes.php#L514 */
    function wpse39084_custom_author_metabox_insdes() {
          global $user_ID;
          global $post;
          ?>
          <label class="screen-reader-text" for="post_author_override"><?php _e('Author'); ?></label>
    
          <?php
            wp_dropdown_users( array(
                 'name' => 'post_author_override',
                 'selected' => empty($post->ID) ? $user_ID : $post->post_author,
                 'include_selected' => true
            ) );
    }
    

    This mimics the default author metabox but the call wp_dropdown_users omits the who=>'editors' argument. It defaults to the only other value which is call users.

  4. A better way to do it…

    add_filter('wp_dropdown_users', 'MySwitchUser');
    function MySwitchUser()
    {
        global $post; // remove if not needed
        //global $post is available here, hence you can check for the post type here
        $users = get_users('role=subscriber');
    
        echo'<select id="post_author_override" name="post_author_override" class="">';
    
        echo'<option value="1">Admin</option>';
    
        foreach($users as $user)
        {
            echo '<option value="'.$user->ID.'"';
    
            if ($post->post_author == $user->ID){ echo 'selected="selected"'; }
    
            echo'>';
            echo $user->user_login.'</option>';     
        }
        echo'</select>';
    
    }
    
  5. This is a code linked by @Innate in a comment (solution) to his own question, I’ve just adapted a little bit and tested in WP 3.3.2 (function wpse39084).
    It will show the subscribers in posts Edit and Quick Edit.

    Also added a couple of actions (functions wpse50827) to move the Author meta box inside the Publish Actions meta box, for easier management.

    Everything is post related, no pages nor CPTs…

    foreach( array( 'edit.php', 'post.php' ) as $hook )
        add_action( "load-$hook", 'wpse39084_replace_post_meta_author' );       
    
    /* Show Subscribers in post author dropdowns - edit and quickEdit */
    function wpse39084_replace_post_meta_author()
    {
        global $typenow;
        if( 'post' != $typenow )
            return;
    
        add_action( 'admin_menu', 'wpse50827_author_metabox_remove' );
        add_action( 'post_submitbox_misc_actions', 'wpse50827_author_metabox_move' );
        add_filter( 'wp_dropdown_users', 'wpse39084_showme_dropdown_users' );
    }
    
    /* Modify authors dropdown */
    function wpse39084_showme_dropdown_users( $args = '' )
    {
        $post = get_post();
        $selected = $post->post_author;
        $siteusers = get_users( 'orderby=nicename&order=ASC' ); // you can pass filters and option
        $re = '';
        if( count( $siteusers ) > 0 )
        {
            $re = '<select name="post_author_override" id="post_author_override">';
            foreach( $siteusers as $user )
            {
                $re .= '<option value="' . $user->ID . '">' . $user->user_nicename . '</option>';
            }
            $re .= '</select>';
            $re = str_replace( 'value="' . $selected . '"', 'value="' . $selected . '" selected="selected"', $re );
        }
        echo $re;
    }
    
    /* Remove Author meta box from post editing */
    function wpse50827_author_metabox_remove()
    {
        remove_meta_box( 'authordiv', 'post', 'normal' );
    }
    
    
    /* Move Author meta box inside Publish Actions meta box */
    function wpse50827_author_metabox_move()
    {
        global $post;
    
        echo '<div id="author" class="misc-pub-section" style="border-top-style:solid; border-top-width:1px; border-top-color:#EEEEEE; border-bottom-width:0px;">Author: ';
        post_author_meta_box( $post );
        echo '</div>';
    }
    
  6. I’ve done something similar to the accepted answer here but only wanted to show admins and in my case, a custom ‘producers’ role, together.

    add_filter('wp_dropdown_users', 'custom_author_select');
    function custom_author_select($output){
    
        //global $post is available here, hence you can check for the post type here
        $admins = get_users('role=administrator');
        $producers = get_users('role=producer');
        $users = array_merge($admins, $producers);
    
        $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;
    }
    
  7. This could be a solution to avoid the error in quick-editing, where “cpt_slug” should be replaced with your custom post type slug

    add_filter('wp_dropdown_users', 'MySwitchUser');
    
    function MySwitchUser($output)
    {
        global $typenow;
    if ((is_edit_page('edit') && "cpt_slug" == $typenow)||(is_edit_page('new') && "cpt_slug" == $typenow)){
        global $post;
        $users = get_users();
        $output = "<select id="post_author_override" name="post_author_override" class="">";   
        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;
    }
    
    
    
    function is_edit_page($new_edit = null){
        global $pagenow;
        if (!is_admin()) return false;
        if($new_edit == "edit")
            return in_array( $pagenow, array( 'post.php',  ) );
        elseif($new_edit == "new") 
            return in_array( $pagenow, array( 'post-new.php' ) );
        else 
            return in_array( $pagenow, array( 'post.php', 'post-new.php' ) );
    }