I use the following to stop Subscribers (and Customers) from accessing the admin area of my site:

 * Redirect back to homepage and not allow access to WP admin for Subscribers.
function xcsn_redirect_from_admin(){
    if ( ( ! current_user_can( 'edit_posts' ) ) && ( ! is_checkout() ) ){
        wp_redirect( site_url() );
add_action( 'admin_init', 'xcsn_redirect_from_admin' );

It works great except that it interferes with an ajax call that my checkout page is making (WooCommerce). The call stalls and the checkout can't proceed unless you have 'edit-posts' privileges, which defeats the purpose, obviously.

My question: is it possible to set a conditional that will work with an early call like admin_init that will allow the ajax calls to get through but won't allow the admin backend to be loaded as a page?

(Sorry if the title is unclear - it was hard to fit into a single sentence!)

1 Answer 1


Check the constant DOING_AJAX. It is always defined when someone uses the WordPress AJAX API.

if ( defined( 'DOING_AJAX' ) && DOING_AJAX )

if ( is_admin() )
    add_action( 'admin_init', 'xcsn_redirect_from_admin' );

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.