Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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');
share|improve this answer
    
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 '11 at 9:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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