I'm using this function to prevent users apart from admin from getting to the backend of wordpress. (Note, that addition to allow ajax requests too):

add_action( 'init', 'my_block_users' );

function my_block_users() {
      $isAjax = (defined('DOING_AJAX') && true === DOING_AJAX) ? true : false;

    if(!$isAjax) {

        if ( is_admin() && ! current_user_can( 'administrator' ) ) {
            wp_redirect( home_url() );


Trouble is, it causes an http error when uploading images etc. Does anyone know a work around for this? Thanks!

1 Answer 1


Works on my side. Try disabling your plugins and test again.

Btw. You could use admin_init instead of the init action hook. This way, the snippet will only be executed on admin requests. This way, you can remove the is_admin() check in the function.

  • It totally does work. It was red herring, not quite sure what was temporarily causing the http error. Anyway, working now, so yey! Also, good point about admin_init. Can't believe I missed that. Thanks. Commented Sep 24, 2011 at 14:58

Your Answer

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

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