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.

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

share|improve this question
add comment

4 Answers 4

up vote 6 down vote accepted

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!

share|improve this answer
    
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 '12 at 20:42
    
You're welcome mate! :) Actually I was using it for a custom role, hence the parameters... Glad I could help! –  Rutwick Gangurde May 3 '12 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 '12 at 1:59
    
Yes, you get the author for this post, and check which one of users matches the author, keep that option selected. –  Rutwick Gangurde May 5 '12 at 5:08
1  
Try printing the global $post variable... –  Rutwick Gangurde May 7 '12 at 4:20
show 7 more comments

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.

share|improve this answer
    
Thanks Stephen. I ended up using Rutwick's solution because it already works with CPTs but appreciate your answer :) –  fxfuture May 2 '12 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. –  danieldekay Dec 26 '13 at 11:20
add comment

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

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

}
share|improve this answer
    
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). –  danieldekay Dec 26 '13 at 11:22
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.