Redirect to dashboard based on user role

Hi WP'ers,
I'm trying to hide some pages for child site admins of my multisite. I can use remove_menu_page( \'\' ); but this only hides them. I don't want them to be accessible through the url. I tried something like this, but my coding skills aren't great..:

if ( ! current_user_can( \'update_core\' ) ) {
<?php
$current_site = get_current_site();
$current_domain = $current_site->domain;
if( $current_domain ==  $current_site/wp-admin/themes ) {
<?php wp_redirect( \'/wp-admin/index.php\', 301 ); exit; ?>
}
?>

Can somebody help me with this?

Thanks, Menno

  • Ash

    Hello Menno

    For admin theme please, please try the following code:

    add_action( 'admin_init', 'disallowed_admin_pages' );
    function disallowed_admin_pages()
    {
        global $pagenow;
        if( $pagenow == 'themes.php' )
    	{
    		wp_redirect( admin_url() );
    		exit;
    	}
    }

    You can use this code in your child theme's functions.php if the theme is not changed. Otherwise mu-plugin is the best option. To create a mu-plugin, go to wp-content/mu-plugins folder. If there is no mu-plugins folder then, create one. Now, inside the mu-plugins folder create file with name anything.php (make sure file extension is .php). Now start with a <?php tag and then put the above code.

    Hope it helps! Please feel free to ask more questions if you have any.

    Have a nice day!

    Cheers,
    Ash

  • Menno

    Hi Ash,

    That's great work, it works! Thank you! I added the update_core line so only super admins of the multisite can access the pages.

    // Disallow pages for child site admins //
    add_action( 'admin_init', 'disallowed_admin_pages' );
    function disallowed_admin_pages()
    {
    	if ( ! current_user_can( 'update_core' ) ) {
    	    global $pagenow;
    	    if( $pagenow == 'themes.php' )
    		{
    			wp_redirect( admin_url() );
    			exit;
    		}
    	}
    }

    Cheers,
    Menno

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.