• Resolved blueshark

    (@blueshark)


    Hi there,

    for my blog I am trying to add a custom role which is called ‘custom_editor’. This role should in addition to a normal editor also allow to edit theme options. But this role should only be created or added, if my theme is activated.

    So at the beginning in the functions hooked to ‘after_setup_theme’ In put there

    add_role(	'custom_editor',
    		'Erweiterter Redakteuer',
    		array(
    			// Additional Capabilities
    			'edit_theme_options' => true,
    			// Editor Capabilities
    			'delete_others_pages' => true,
    			'delete_others_posts' => true,
    			'delete_pages' => true,
    			'delete_posts' => true,
    			'delete_private_pages' => true,
    			'delete_private_posts' => true,
    			'delete_published_pages' => true,
    			'delete_published_posts' => true,
    			'edit_others_pages' => true,
    			'edit_others_posts' => true,
    			'edit_pages' => true,
    			'edit_posts' => true,
    			'edit_private_pages' => true,
    			'edit_private_posts' => true,
    			'edit_published_pages' => true,
    			'edit_published_posts' => true,
    			'manage_categories' => true,
    			'manage_links' => true,
    			'moderate_comments' => true,
    			'publish_pages' => true,
    			'publish_posts' => true,
    			'read' => true,
    			'read_private_pages' => true,
    			'read_private_posts' => true,
    			'unfiltered_html' => true,
    			'upload_files' => true
    		)
    	);

    The capability exists and I can apply it to users. But a user with this capability cannot edit the theme options.

    If I edit the capability of editor instead by doing

    $editor = get_role( 'editor' );
    $editor->add_cap( 'edit_theme_options' );

    all users with the capability ‘editor’ can then edit theme options.

    What I am doing wrong?

    • This topic was modified 7 years, 2 months ago by blueshark.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I tried your add_role() code on my site and a user with that role could edit theme options. This user used the customizer to change twentyfifteen theme options without any issue. Could it be how you’ve setup your theme options? With the customizer, you can add settings that require any capability you want, so having ‘edit_theme_options’ may not be adequate for your settings. Similar for adding admin pages.

    Thread Starter blueshark

    (@blueshark)

    Thanks for your reply.

    I was guessing, that something according to the order of calling my functions is not correct. I would totally agree with you, if both ways would not work. Since extending an existing role with ‘add_cap’ works fine, some doubts are remaining.

    Can you please provide the whole function, where you placed ‘add_role’. I want to doulbe check if I put the call for ‘add_role’ at the correct position.

    Moderator bcworkz

    (@bcworkz)

    I added your code to a callback added to the “init” action as a quick test. This would be the wrong way to actually add a new role. Roles are persistent, there’s no point in re-adding the same role on every request, which is the case with the “init” hook. It’s best to add roles from code that runs once and is done, such as the activation hook for a plugin or theme.

    The order ultimately doesn’t matter too much. Even if it’s “wrong” the first time through, because it persists, it would still be applied on subsequent requests. There is one important caveat though. If you add too early, before the roles are loaded from the DB, calling add_role() would have no effect. If you called add_role() directly from plugin code without adding the call to a later hook, that would be too early. “init” would be late enough, but as mentioned, it’s the wrong hook. The recommended hooks are plugin or theme activation hooks.

    Thread Starter blueshark

    (@blueshark)

    I followed your suggestions and created an init function, which is hooked to ‘load-themes.php’. Now the function is called only when you switch themes. So I guess this is the correct place, since add_role is not always called.

    But still I had the same issue, so I printed the privileges of the new role with

    	$custom_editor = get_role( 'custom_editor' );
    	$custom_editor->add_cap( 'edit_theme_options' );
    	print_r($custom_editor);

    The capbability ‘edit_theme_options’ was not included. Then I realised, that a comma was missing at the end of my initial code. So instead of 'upload_files' => true, there was 'upload_files' => true. And now after this hassle it works as expected. ??

    Thank you for your help.

    • This reply was modified 7 years, 1 month ago by blueshark.
    • This reply was modified 7 years, 1 month ago by blueshark.
    Moderator bcworkz

    (@bcworkz)

    You’re welcome. If upload_files is the last item in an array declaration, the terminal comma is optional, so that doesn’t really explain your observations. But if you have your custom role working, who cares? It works! ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom role with add_role’ is closed to new replies.