admin管理员组文章数量:1122832
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
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 edited May 2, 2012 at 3:17 fxfuture asked May 2, 2012 at 0:33 fxfuturefxfuture 1,3374 gold badges24 silver badges40 bronze badges9 Answers
Reset to default 19This 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.
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>';
}
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>';
}
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;
}
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' ) );
}
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:
- The block ed. function used to populate the author’s dropdown is wodpress/data/select( ‘core’ ).getAuthors()
- This calls the rest api endpoint: /wp/v2/users/?who=authors&per_page=-1
- This api endpoint passes data to WP_User_Query
- A WP_User_Query with the “who=>author” parameter passed will add a usermeta query to the mix.
- This will pull any user with a user_level not equal to 0
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' );
}
I tried all the solutions here and some others, but this one from the contributed user notes in the Wordpress Codex was the only one that worked for me.
It says that you need to give capability to subscribers to use it, but in my case, I did not want it. I just wanted to be able to select them in the select box of authors.
// Show Subscribers in the 'author' drop down on the classic post editor
function wpdocs_add_subscribers_to_dropdown( $query_args ) {
$query_args['who'] = ''; // reset the query
$query_args['capability'] = ''; // reset the query
$query_args['role__in'] = array( 'administrator', 'subscriber', 'author', 'editor' );
$query_args['capability__in'] = array( 'edit_own_posts' ); // Custom capability for subscribers
return $query_args;
}
add_filter( 'wp_dropdown_users_args', 'wpdocs_add_subscribers_to_dropdown' );
The link to the codex page: https://developer.wordpress.org/reference/hooks/wp_dropdown_users_args/#user-contributed-notes
本文标签: user rolesSelect subscriber as author of post in admin panel
版权声明:本文标题:user roles - Select subscriber as author of post in admin panel? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736296994a1929917.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论