WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 1 down vote accepted

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.

share|improve this answer
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. – Michael Watson Sep 24 '11 at 14:58

Your Answer


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.