Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Tried to make a simple redirect for some users I don't want to access the wp-admin/, so I did this code:

function no_admin_access() {
    if ( !current_user_can( 'delete_posts' ) ) {
        wp_redirect( site_url( '/' ) ); exit;
add_action('admin_init', 'no_admin_access');

But when I then try to make a ajax request with those users, the that is also redirected so I never get to admin-ajax.php

Anybody who has a good work around for this ?



    type: 'POST',
    url: MyAjax.ajaxurl,
    data: {
        action: 'mux_ajax_delete_pix',
        pid: $this.attr('data-id')
    success: function(){
share|improve this question
Please show your complete AJAX related code - I'm pretty sure that you're simply locking yourself out. (Happens to the best). – kaiser Oct 23 '12 at 15:50
I have added the Ajax code, but Bainternet answer below worked. – Kristian Primdal Oct 24 '12 at 11:58

1 Answer 1

up vote 2 down vote accepted

You can and a check for the DOING_AJAX constant which is defined on an Ajax in your conditional check:

function no_admin_access()
    if (
        // Don't do this for AJAX calls
        ! defined( 'DOING_AJAX' ) 
        // Capability check
        && ! current_user_can( 'delete_posts' ) 
        // Redirect to home/front page
        wp_redirect( site_url( '/' ) );
        // Never ever(!) forget to exit(); or die(); after a redirect
add_action( 'admin_init', 'no_admin_access' );
share|improve this answer
Thanks, it worked – Kristian Primdal Oct 24 '12 at 11:56
Works great on the backend, but doesn't work for front end posting. Authors can't upload images with this solution on the front end. Still can't find a way around that... – Jake Jun 17 '13 at 17:38

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.