0

I've created a new role within a WordPress multisite named 'Site Owner'. The intention is for it to be very close to admin role within the child sites except it will not be able to access plugins, settings, tools and some specific options under Appearance section. However, when I have registered the role following the proper convention outlined https://codex.wordpress.org/Function_Reference/add_role it does not seem to allow the custom 'Site Owner' role to see any of the Appearance settings, no matter what I do. It seems as though any custom role created, will never be able to access to Appearance. Here is the code:

<?php

$result = add_role(
    'org_site_owner',
    __( 'Site Owner' ),
    array(
        'activate_plugins'      => false,
        'create_users'          => true,
        'customize'             => true,
        'delete_others_pages'   => true,
        'delete_others_posts'   => true,
        'delete_pages'          => true,
        'delete_plugins'        => false,
        'delete_posts'          => true,
        'delete_private_pages'  => true,
        'delete_private_posts'  => true,
        'delete_published_pages'    => true,
        'delete_published_posts'    => true,
        'delete_site'           => false,
        'delete_themes'         => false,
        'delete_users'          => true,
        'edit_dashboard'        => false,
        'edit_files'            => true,
        'edit_others_pages'     => true,
        'edit_others_posts'     => true,
        'edit_pages'            => true,
        'edit_plugins'          => false,
        'edit_posts'            => true,
        'edit_private_pages'    => true,
        'edit_private_posts'    => true,
        'edit_published_pages'  => true,
        'edit_published_posts'  => true,
        'edit_theme_options'    => true,
        'edit_themes'           => false,
        'edit_users'            => true,
        'export'                => false,
        'import'                => false,
        'install_plugins'       => false,
        'install_themes'        => false,
        'list_users'            => true,
        'manage_categories'     => true,
        'manage_links'          => true,
        'manage_options'        => true,
        'moderate_comments'     => true,
        'promote_users'         => false,
        'publish_pages'         => true,
        'publish_posts'         => true,
        'read'                  => true,
        'read_private_pages'    => true,
        'read_private_posts'    => true,
        'remove_users'          => true,
        'switch_themes'         => false,
        'unfiltered_html'       => false,
        'update_core'           => false,
        'update_plugins'        => false,
        'update_themes'         => false,
        'upload_files'          => true,
        'upload_plugins'        => false,
        'upload_themes'         => false,
    )
);

add_action( 'admin_init' , 'org_site_owner_menu_pages' );
function org_site_owner_menu_pages()
{
    if ( current_user_can( 'qut_site_owner' ) )
    {
        remove_menu_page('plugins.php'); // Plugins
        remove_menu_page('tools.php'); // Tools
        remove_menu_page('edit.php?post_type=acf'); // ACF Fields
        remove_menu_page('admin.php?page=searchandfilter-settings'); // Search and filter plugin
        //remove_menu_page( 'themes.php' ); //Appearance
        remove_submenu_page('themes.php', 'themes.php'); // hide theme selection

    }
}

It doesn't affect it at all if I set all the array options to 'true'. The only thing that seems to work is the remove_menu_page() options at the bottom. Is this a core embedded WordPress feature that I can't get around or something?

  • Where and how do you use this code? – Max Yudin May 31 '17 at 7:25
  • I created a plugin to handle it. Will post my solution now – Ryan Coolwebs Jun 2 '17 at 1:48
0

WP Custom Site Roles appear to be a bit 'flaky' at best but this particular solution worked for me (created a custom plugin for it).

$result = add_role(
    'org_site_owner',
    __( 'Site Owner' ),
    array(
        'activate_plugins'      => false,
        'create_users'          => true,
        'customize'             => true,
        'delete_others_pages'   => true,
        'delete_others_posts'   => true,
        'delete_pages'          => true,
        'delete_plugins'        => false,
        'delete_posts'          => true,
        'delete_private_pages'  => true,
        'delete_private_posts'  => true,
        'delete_published_pages'    => true,
        'delete_published_posts'    => true,
        'delete_site'           => false,
        'delete_themes'         => false,
        'delete_users'          => true,
        'edit_dashboard'        => false,
        'edit_files'            => true,
        'edit_others_pages'     => true,
        'edit_others_posts'     => true,
        'edit_pages'            => true,
        'edit_plugins'          => false,
        'edit_posts'            => true,
        'edit_private_pages'    => true,
        'edit_private_posts'    => true,
        'edit_published_pages'  => true,
        'edit_published_posts'  => true,
        'edit_theme_options'    => true,
        'edit_themes'           => false,
        'edit_users'            => true,
        'export'                => false,
        'import'                => false,
        'install_plugins'       => false,
        'install_themes'        => false,
        'list_users'            => true,
        'manage_categories'     => true,
        'manage_links'          => true,
        'manage_options'        => true,
        'moderate_comments'     => true,
        'promote_users'         => false,
        'publish_pages'         => true,
        'publish_posts'         => true,
        'read'                  => true,
        'read_private_pages'    => true,
        'read_private_posts'    => true,
        'remove_users'          => true,
        'switch_themes'         => false,
        'unfiltered_html'       => false,
        'update_core'           => false,
        'update_plugins'        => false,
        'update_themes'         => false,
        'upload_files'          => true,
        'upload_plugins'        => false,
        'upload_themes'         => false,
    )
);

// Allow site owners to see Appearance and Settings menus
$role_object = get_role( 'org_site_owner' );
$role_object->add_cap( 'edit_theme_options' );
$role_object->add_cap( 'manage_options' );

function hide_appearance_options() {
    // Hide theme selection page
    remove_submenu_page( 'themes.php', 'themes.php' );
    // Hide media settings page
    remove_submenu_page( 'options-general.php', 'options-media.php' );
    // Hide permalinks
    remove_submenu_page( 'options-general.php', 'options-permalink.php' );
    // Hide featured image column
    remove_submenu_page( 'options-general.php', 'featured-image-column' );
    //Hide SSO/SAML configuration settings
    remove_submenu_page( 'options-general.php', 'onelogin_saml_configuration' );
}

function org_site_owner_menu_pages()
{
    $user_role = wp_get_current_user();
    $allowed_roles = array('org_site_owner');
    if( array_intersect($allowed_roles, $user_role->roles ) )
        {
        remove_menu_page('plugins.php'); // Plugins
        remove_menu_page('tools.php'); // Tools
        remove_menu_page('edit.php?post_type=acf'); // ACF Fields
        remove_menu_page('admin.php?page=searchandfilter-settings'); // Search and filter plugin
        add_action('admin_head', 'hide_appearance_options');
    }
}
add_action( 'admin_init' , 'org_site_owner_menu_pages' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.