1

I have a theme being used by a sub-folder multisite and a domain-mapped multi site.

E.g.

mywpsite.com/site1

mywpsite.com/site2

www.site2.com (leads to the above)

I have this snippet of code in my functions.php file

function my_wp_admin_ban(){
if( !current_user_can('activate_plugins') ){
wp_redirect( get_bloginfo('url') );
exit;
}
}

add_action('admin_init','my_wp_admin_ban');

This successfully prevents members of site1 from access the admin (wp-admin) dashboard area.

This code does not, however, seem to work for site2.

Instead, those users are shown a "You do not have sufficient permissions to access this page." error message.

Tried everything I can think of including different ways of doing the same thing I.e. How to redirect non admins to homepage if trying to view mysite.com/wp-admin/? and How do I remove dashboard access from specific user roles?

What can I do to ensure all users (non admin) of the multisite are redirected away from the dashboard area?

1 Answer 1

1

The file functions.php is not the place to run this kind of code. See Where do I put the code snippets I found here or somewhere else on the web?

You can use a Must Use plugin or make a simple one and Network Activate.

It would go like this:

<?php
/* Plugin Name: Only admins on dashboard */

add_action( 'admin_init', 'admin_ban_wpse_105863' );

function admin_ban_wpse_105863()
{
    if( !current_user_can( 'activate_plugins' ) ){
        wp_redirect( site_url() );
        exit;
    }
}

The documentation for the function get_bloginfo has recommended alternatives for many cases. The site_url function takes care of using the current blog URL.


[update]
The only place where the error message ...permissions to access... appears is in the file wp-admin/includes/menu.php. And it has an useful hook:

add_action( 'admin_page_access_denied', 'denied_ban_wpse_105863' );

function denied_ban_wpse_105863()
{
    wp_redirect( site_url() );
    exit;
}
5
  • Thanks for that. Created a plugin instead and removed the offending code from functions.php Still gives the same message for site2 "You do not have sufficient permissions to access this page."
    – Chris
    Jul 9, 2013 at 18:58
  • Did you tried as a mu-plugin? I tested in my two local MS installations (subdomain and subdirectory) and it works ok.
    – brasofilo
    Jul 9, 2013 at 19:22
  • I did. It doesnt work. Are you using sunrise on any of them? Im sure that has something to do with it.
    – Chris
    Jul 9, 2013 at 20:00
  • I just tested in a live site with sunrise, and it worked as well :/ If possible, try to disable the other plugins. Or try another capability. I'll add an alternative in the Answer.
    – brasofilo
    Jul 9, 2013 at 21:22
  • Would you believe, for reasons unknown, I need both snippets of code. The 'admin_init' for sites like site1 and the 'admin_page_access_denied' for sites like site2. Thanks to you brasofilo!
    – Chris
    Jul 10, 2013 at 7:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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