I created a new role, and I can set a user to that role under. However, I cannot change the author of a post to that user. I'm thinking this should be simple, but I can't figure it out.
I'm guessing it's something to do with the capabilities. I set up the user like this:
add_role( 'test-author', 'Test Author', array(
'read' => true,
'delete-Posts' => true,
'edit_posts' => true,
'delete_published_posts' => true,
'publish_posts' => true,
'create_product' => true,
'upload_files' => true,
'edit_published_posts' => true
But as I said, I can't set a post to this author.
Eric P. answers:
The dropdown for authors is created in /wp-admin/includes/meta-boxes.php by the function "wp_dropdown_users" in wp-includes/user.php.
Looking at the source code for that, it appears that meta-boxes.php includes 'who'=>'authors' in the arguments array, and that triggers a filter in wp_dropdown_users for "user_level != 0" (user_level is a deprecated way of setting up permissions and capabilities).
I can't believe that the WordPress development team hasn't updated wp_dropdown_users where $args['who']=='authors' would use "user_can()" and check for "edit posts" capability.
There is an "apply_filters" hook for "wp_dropdown_users" which you could hook and generate a more appropriate dropdown which would show for authors. That's the good news.
The bad news is, writing an actual filter to fix this will require more than $5 in effort.
Have you tried setting that user's role as "author" (or even "contributor") first, saving the user information, then changing them to the "Test Author" role?
There's a function to set the "user_level" from capabilities, and it should be called when the user's profile is changed. If you bang on the database and just change a user's role, the "user_level" might not get updated.
Eric P. comments:
As a follow up, the code fragment you gave only creates a new role. If you want a user to have that role assigned, you'll need to update the user profile or include code to set the role of one or more users to your "Test Author" role.
When you actually assign that role to a user, if you do it through the profile page, the "updated_user_leve_from_caps()" function should update the user_level of that user, and since the user has the "edit_posts" capability, they should show in the author dropdown.
You were right. I just added
'user_level' => 'level_2'
and then re-saved my user and it's working now. I saw the levels on the Roles and Capabilities page of the Codex, but figured since it was deprecated it wasn't in use anymore.
Thanks for the help Eric, I appreciate it very much.