Wordpress - 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!
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;
}
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.