X

Restrict admin access by user capability

This snippet will restrict admin access by user capability. Users will be redirected back to the home page if they do not have the manage_options capability. We added && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) to ensure the request is not an ajax request. Keep in mind you can always change the manage_options capability to something else to suit your needs. You can find a llist of capabilities within the WordPress Codex.

function wps_restrict_admin() {
    if ( ! current_user_can( 'manage_options' )  && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX )  ) {
        wp_redirect( home_url() );
        exit;
    }
}
add_action( 'admin_init', 'wps_restrict_admin', 1 );

Comments  Leave a Reply

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!