Support » Developing with WordPress » Conditional for if request is admin-ajax

  • Resolved Beee

    (@beee)


    I would like to redirect users out of the wp-admin. For that I use the code below. It works as intended, except when an ajax request is made.

    function sd_redirect_out_of_admin()  {
        if ( is_admin() && ! current_user_can( 'manage_options' ) ) {
            if ( is_user_logged_in() ) {
                wp_redirect( get_permalink( ACCOUNT_PAGE ) );
            } else {
                wp_redirect( home_url() );
            }
            exit();
        }
    }
    add_action( 'init', 'sd_redirect_out_of_admin' );

    Admin-ajax.php is in the admin and thus the user will not reach this and thus throws an error.

    Is there a way to check if a user is logged in and requests this URL ?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Dion Designs

    (@diondesigns)

    Something like this will probably do what you want:

    if (defined('DOING_AJAX')) {
    	// URL is admin-ajax.php
    }
    else {
    	// URL is something else
    }
    
    Beee

    (@beee)

    Awesome that did it. Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Conditional for if request is admin-ajax’ is closed to new replies.