Skip Navigation

[Resolved] Can’t Add New User WIth New Custom Role as Author

This thread is resolved. Here is a description of the problem and solution.

Problem: I have created a custom role with Access. I would like to choose a User in that custom role, when selecting the author of a post. It turns out that none of the Users in this custom role appear in the author dropdown options. What should I do to add and enable those author select options?

Solution: Please add a custom code snippet to your site in order to add these Users to the select author dropdown field:

function wpdocs_add_members_to_dropdown( $query_args, $r ) {
   
    $query_args['role'] = array('member');
   
    // Unset the 'who' as this defaults to the 'author' role
    unset( $query_args['who'] );
   
    return $query_args;
}
add_filter( 'wp_dropdown_users_args', 'wpdocs_add_members_to_dropdown', 10, 2 );

Replace member with the slug of your custom role.

Relevant Documentation:
https://developer.wordpress.org/reference/hooks/wp_dropdown_users_args/

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 6 replies, has 2 voices.

Last updated by AlimB3245 5 months ago.

Assigned support staff: Christian Cox.

Author
Posts
#1638213

Tell us what you are trying to do? I have a new user and I have a new custom role for this user.. I tried to assign a custom post to this user but in the dropdown where I have to select the Author I can view all other users except this user. However the user exists in the ALL USERS view but is not visible in the Authors dropdown view.

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site? hidden link

#1638715

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

Hi, you can use a small custom code snippet to allow Users from a custom role to be shown in the author dropdown. Another Toolset user had a similar question in the forum post here: https://toolset.com/forums/topic/author-dropdown-and-custom-roles/

Here is the code snippet - you must replace member with the slug of the custom role, but you probably should not make any other changes.

function wpdocs_add_members_to_dropdown( $query_args, $r ) {
  
    $query_args['role'] = array('member');
  
    // Unset the 'who' as this defaults to the 'author' role
    unset( $query_args['who'] );
  
    return $query_args;
}
add_filter( 'wp_dropdown_users_args', 'wpdocs_add_members_to_dropdown', 10, 2 );

You can add this code in a child theme's functions.php file, or you can create a new code snippet in wp-admin > Toolset > Settings, in the Custom Code tab. Let me know if I misunderstood the request or if you have problems implementing this code.

#1639243

Hi Christian

Thanks for the accurate assessment and solution..:-).. you were absolutely right about what I wanted..

There is one issue related to adding the code in the Custom Code Tab where it gave me this message below

Please advise what I need to put in the wp-config for the future

for the current code I added it manually.

Coming back to the AUTHOR dropdown, it's interesting to note that the user who had a similar query (The link that you sent to me.. https://toolset.com/forums/topic/author-dropdown-and-custom-roles/) ... She seemed to add all the permissions that are normally assigned to AUTHOR to the new custom role she had created. I have copied all permissions from SUBSCRIBER Role so basically not a lot of permissions were provided. I had 2 options.. Either follow what she did and update the custom role permission to mimic those of the AUTHORs or add multiple roles to the same user.. so the user had the custom role permissions as well as the AUTHOR role permissions.. it seemed to work fine for me..

Could you please advise which of the two (Set the right permission of the custom role OR Add multiple roles to user based on a combination of permissions ) is a better method and why?

Now I have another thread with Shane here.. https://toolset.com/forums/topic/user-permission-to-view-and-edit-custom-posts/ .. so I will ask him the other questions on that post.. and not mix the two...;-)..

But I had one query more for you.. I didn't know till Shane told me that I could have a new front-end option to edit posts and it's a whole new thing for me.. I always assumed that the only way to edit would be to have the backend admin access and I'd have to limit the menu permissions.. but I prefer the frontend as it's a whole lot non-intrusive.

Is there a link or some documentation for me to know which is a better option (backend editing or frontend editing for custom posts)? Or if you can let me know, I'll just take your word for it..;-)..

Regards,
Alim

#1640667

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

There is one issue related to adding the code in the Custom Code Tab where it gave me this message below

Please advise what I need to put in the wp-config for the future

for the current code I added it manually.
I'm sorry but I do not understand what you are saying. Can you provide a bit more information? Perhaps you could share a screenshot of the error?

Could you please advise which of the two (Set the right permission of the custom role OR Add multiple roles to user based on a combination of permissions ) is a better method and why?
Some pieces of custom code you'll find on the site here were written when Access only supported one role per User. Now that multiple roles are supported, we have noticed that some of those custom code snippets are no longer reliable. For that reason, I prefer to use one role and manage all capabilities in that role as needed. See this ticket: https://toolset.com/forums/topic/block-completely-backend-access-to-a-user-role/

// Hide the admin area from all Users
// with the User role "hide-from-role-slug"
$user = wp_get_current_user();
$roles = $user->roles; // now you have an array of all the User's roles 
$role = array_shift($roles); // this picks the first role in an array of roles
if ( $role == 'hide-from-role-slug' && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) {
  wp_redirect( site_url() );
  exit;
}

Instead, the code should loop over all the roles in the array and test each role, not just assume that only one role will exist.

Is there a link or some documentation for me to know which is a better option (backend editing or frontend editing for custom posts)? Or if you can let me know, I'll just take your word for it..;-)..
Let's discuss this in a separate ticket.

#1640883

Hi Christian...:-)..

I'm sorry but I do not understand what you are saying. Can you provide a bit more information? Perhaps you could share a screenshot of the error?

Am attaching a screenshot to show you the error.. Please check the text with the yellow highlight border on the left.. you would notice that I can't add or edit anything in the field...

Regards,
Alim

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/cannot-save-custom-code-snippet/

#1642179

Christian Cox
Supporter

Languages: English (English )

Timezone: America/New_York (GMT-04:00)

I have split the custom code snippet issue into a separate ticket so we can discuss the problem.

#1642395

My issue is resolved now. Thank you!

Thanks again Christian.. In my opinion should be a default feature rather than having to add a code to customize...:-).. and maybe if someone doesn't want to provide the option of adding custom role users as authors you could have a checkbox option to disable it.. just a suggestion...:-)..

Have a nice day..:-)..

Regards,
Alim