is there a way to limit administrators access

Hi, is there a way to limit administrators access to the backend?
For example, what I need is to assign an user admin privilegies, but limited just to access Coursepress and user management.
Thanks!

  • aristath

    Hello there @Esthetic, I hope you're well today!

    This process can be usually divided to 2 steps:

    Remove unnecessary admin menus
    Restrict access to some pages.

    Step 1: Remove unnecessary pages.
    The below function actually contains 2 completely different methods, you can use whichever you want. Sometimes some menus are not removed using the first method and the 2nd one has to be used instead. Just try it on your own.

    <?php
    
    /*
     * Hide admin menus for non Network Admins
     */
    function custom_remove_admin_theme_remove_menus () {
        if( !current_user_can( 'manage_network' ) ) {
            global $menu;
            $restricted = array(_
                __( 'Posts' ),
                __( 'Media' ),
                __( 'Links' ),
                __( 'Pages' ),
                __( 'Tools' ),
                __( 'Users' ),
                __( 'Settings' ),
                __( 'Comments' ),
                __( 'Plugins' ),
                __( 'QRcode'),
            );
            end ( $menu );
    
            while ( prev( $menu ) ) {
                $value = explode( ' ',$menu[key( $menu )][0] );
                if ( in_array( $value[0] != NULL ? $value[0]: '', $restricted ) ) {
                    unset( $menu[key( $menu )] );
                }
            }
    
            remove_menu_page( 'edit-comments.php' );
            remove_menu_page( 'themes.php' );
            remove_menu_page( 'plugins.php' );
            remove_menu_page( 'admin.php?page=mp_st' );
            remove_menu_page( 'admin.php?page=cp_main' );
            remove_submenu_page( 'edit.php?post_type=product', 'edit-tags.php?taxonomy=product_category&post_type=product' );
            remove_submenu_page( 'edit.php?post_type=product', 'edit-tags.php?taxonomy=brand&post_type=product' );
            remove_submenu_page( 'edit.php?post_type=product', 'edit-tags.php?taxonomy=model&post_type=product' );
            remove_submenu_page( 'edit.php?post_type=product', 'edit-tags.php?taxonomy=product_tag&post_type=product' );
        }
    }
    add_action('admin_menu', 'custom_remove_admin_theme_remove_menus', 10);

    Step 2: Disallow access to certain pages
    This is quite simple... First we build an array of URLs we don't want our users to be able to access. Once we do that, then we simply build our function that will redirect them to their dashboard page instead:

    <?php
    
    /*
     * If user is not a SuperAdmin, when they try to access the below URLs they are redirected back to the dashboard.
     */
    function restrict_admin_with_redirect() {
    
        $restrictions = array(
            '/wp-admin/widgets.php'
            '/wp-admin/widgets.php'
            '/wp-admin/user-new.php'
            '/wp-admin/upgrade-functions.php'
            '/wp-admin/upgrade.php'
            '/wp-admin/themes.php'
            '/wp-admin/theme-install.php'
            '/wp-admin/theme-editor.php'
            '/wp-admin/setup-config.php'
            '/wp-admin/plugins.php'
            '/wp-admin/plugin-install.php'
            '/wp-admin/options-writing.php'
            '/wp-admin/options-reading.php'
            '/wp-admin/options-privacy.php'
            '/wp-admin/options-permalink.php'
            '/wp-admin/options-media.php'
            '/wp-admin/options-head.php'
            '/wp-admin/options-general.php.php'
            '/wp-admin/options-discussion.php'
            '/wp-admin/options.php'
            '/wp-admin/network.php'
            '/wp-admin/ms-users.php'
            '/wp-admin/ms-upgrade-network.php'
            '/wp-admin/ms-themes.php'
            '/wp-admin/ms-sites.php'
            '/wp-admin/ms-options.php'
            '/wp-admin/ms-edit.php'
            '/wp-admin/ms-delete-site.php'
            '/wp-admin/ms-admin.php'
            '/wp-admin/moderation.php'
            '/wp-admin/menu-header.php'
            '/wp-admin/menu.php'
            '/wp-admin/edit-tags.php'
            '/wp-admin/edit-tag-form.php'
            '/wp-admin/edit-link-form.php'
            '/wp-admin/edit-comments.php'
            '/wp-admin/credits.php'
            '/wp-admin/about.php'
        );
    
        foreach ( $restrictions as $restriction ) {
    
            if ( ! current_user_can( 'manage_network' ) && $_SERVER['PHP_SELF'] == $restriction ) {
                wp_redirect( admin_url() );
                exit;
            }
    
        }
    
    }
    add_action( 'admin_init', 'restrict_admin_with_redirect' );

    I hope that helps!

    Cheers,
    Ari.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.