Answers:
这是我在类似情况下编写的简单技巧。它将Subscribers
在“ Author
编辑/添加帖子/页面” 下拉列表中显示所有内容,您可以在其中选择所需的任何内容。我认为它应该为您工作...
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;
}
其背后的技巧是,在提交提交此页面后,WP仅从$ _POST数组中的此下拉列表中读取$ user-> ID,并将其分配为帖子作者。这就是您想要的!
global $post
变量...
从WordPress 4.4.0开始,您现在可以使用wp_dropdown_users_args
过滤器。现在的代码要简单得多:
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;
}
这类似于@brasofilo。但是仅在编辑帖子屏幕中起作用,而不是在快速编辑中起作用,并且包括所有用户(不仅是作者和订户)。
/* 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
) );
}
这模仿了默认的作者metabox,但是调用wp_dropdown_users
忽略了who=>'editors'
参数。它默认为唯一的其他值,即呼叫用户。
一种更好的方法...
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>';
}
这是一个代码链接通过@Innate在评论(解决方案),以他自己的问题,我刚刚适应一点点,在WP 3.3.2(功能wpse39084)进行测试。它将在帖子编辑和快速编辑中显示订阅者。
还添加了一些操作(功能wpse50827),以将“作者”元框移动到“发布操作”元框内,以便于管理。
一切都与帖子相关,没有页面也没有CPT ...
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>';
}
我已经做了一些与此处接受的答案类似的操作,但只想向管理员和我展示一个自定义的“生产者”角色。
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;
}
这可能是避免快速编辑错误的解决方案,在这种情况下,应将“ cpt_slug”替换为自定义帖子类型的“ 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' ) );
}