X

Block admin for all users that are not administrators

Snippets by IsItWP

This snippet will redirect all users that are not administrators back to the primary domain when they try to access the WordPress admin.

Instructions
Add this snippet to the functions.php file of your WordPress theme.


    add_action( 'init', 'blockusers_wps_init' );
    function blockusers_wps_init() {
            if ( is_admin() && ! current_user_can( 'administrator' ) ) {
                    wp_redirect( home_url() );
                    exit;
            }
    }

Comments  Leave a Reply

  1. Thanks for sharing ! Awesome snippet but it cause problem with ninja form plugin when you want to sent an email ! Any tip for that ?

  2. Wouldnt this block ajax that uses no_priv or any type of ajax?

  3. I want author to be able to access to that page instead of just an admin (I know the code, but needs help appending it). Also is it possible to only access posts and media only?

    1. if you look at current_user_can and change the capability to something else if will allow other roles to access. View this page on wordpress codex to see what can be done,
      http://codex.wordpress.org/Roles_and_Capabilities

      1. Thanks. Is there a code that can append admin Like “if admin and author can” or something like that?

        1. anything an author can an admin can so allowing authors will also allow admins.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!