5

I'm trying to keep all non-administrators out of the Wordpress admin panel by using a wp_redirect inside of an is_admin conditional. The problem is that a side effect of this if that non-admins can no longer use the file "admin-ajax.php" for ajax calls in Wordpress. It appears that a few people on the Wordpress forums are having the exact same problem lately.

Would anyone have a solution for this?

1 Answer 1

8

Check the DOING_AJAX constant:

function my_admin_init(){
    if( !defined('DOING_AJAX') && !current_user_can('administrator') ){
        wp_redirect( home_url() );
        exit();
    }
}
add_action('admin_init','my_admin_init');
1
  • I'd switch to manage_options in case some plugin changes the name of the 'administrator' role. Anyway, I'd +10 this if i could :)
    – kaiser
    Aug 17, 2011 at 9:35

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.