16

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?

Thanks

8 Answers 8

19

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!

10
  • Thanks so much Rutwick! Exactly what I needed! I just had to change $users = get_users(); otherwise it didn't display users with other roles in there
    – fxfuture
    May 2, 2012 at 20:42
  • You're welcome mate! :) Actually I was using it for a custom role, hence the parameters... Glad I could help! May 3, 2012 at 5:22
  • I've just found a minor issue with this - when I revisit the post edit page the drop down defaults to admin, so if I make changes and save without reselecting the author this it changes the author to admin. Any idea how to fix this?
    – fxfuture
    May 5, 2012 at 1:59
  • Yes, you get the author for this post, and check which one of users matches the author, keep that option selected. May 5, 2012 at 5:08
  • 2
    Try printing the global $post variable... May 7, 2012 at 4:20
16

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;

}
1
2

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.

2
  • Thanks Stephen. I ended up using Rutwick's solution because it already works with CPTs but appreciate your answer :)
    – fxfuture
    May 2, 2012 at 20:46
  • added global $post; to the actual function since it wasn't picking up the existing author when I edited a post, but it always took me as post author. Very Annyoing. Dec 26, 2013 at 11:20
1

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>';

}
1
  • doesn't work for me: it always proposes the Admin account to become new author of the post, which makes it very difficult for users to actually update posts (and then automatically lose editing rights). Dec 26, 2013 at 11:22
1

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>';
}
1
  • 1
    Thanks brasofilo. I ended up using Rutwick's solution because it already works with CPTs but appreciate your answer :)
    – fxfuture
    May 2, 2012 at 20:46
0

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;
}
0

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' ) );
}
0

It's a bit of a mess. It seems there are 3 ways to go about it, and it depends on whether you are using the Classic Editor or Gutenberg.

If you are using the Classical Editor:

  • You can override the HTML using the filter wp_dropdown_users as other answers suggested
  • Or override the the query parameters using wp_dropdown_users_args

If you are using Gutenberg, the Authors dropdown is implemented differently.

As Thomas Levy pointed in this GitHub issue:

For the custom role to show up in the author dropdown, you need to add the level_1 capability to the role. Levels were replaced with capabilities in WordPress 2.0 back in 2005, but this behavior/bug stuck around all this time.

$role = add_role( 'mycustomrole', 'My Custom Role' );
$role->add_cap( 'level_1' );

To add this capability to existing users, run this once:

$custom_role = get_users( 'role=mycustomrole' );
foreach ( $custom_role as $role ) {
    $role->add_cap( 'level_1' );
}

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