0

I have ajax method, user can trigger the same from either dashboard or from front-end. I just want to get from where the ajax is triggered. Is there any way to find?

I tried is_admin method but it always returns true

add_action( 'wp_ajax_get_something', 'get_something');
add_action( 'wp_ajax_nopriv_get_something', 'get_something');

function get_something(){
   $data = array(
       'current_time' => time(),
       'is_admin' => is_admin()
   );

   die(json_encode($data));
}

Javascript which is used for both dashboard and front-end

     jQuery.ajax({
        url: admin_url+'admin-ajax.php?action=get_something',
        type: 'post',
        success: function(response){
            //need to check whether request sent from dashboard or front-end
        }
    });
1

Ajax request in Wordpress is always performed in admin side, so is_admin() returns always true. If you need to know where the ajax request come from you need to check where the script is executed, not where the script send the ajax request.

For example:

add_action('wp_enqueue_scripts', 'cyb_enqueue_scripts');
function cyb_enqueue_scripts() {

    //Register the script.
    wp_register_script('my-script', get_stylesheet_directory_uri(). '/js/my-script.js', array( 'jquery' ) );

    //Enqueue the script and dependencies
    wp_enqueue_script('jquery');
    wp_enqueue_script('my-script');

    //Set up the vars data for the script
    $scriptvars = array(
                    'ajaxurl' => admin_url( 'admin-ajax.php' ),
                    'data'    => array(
                                  'action'   => 'get_something',
                                  'is_admin' => is_admin()
                             )
                   );
    wp_localize_script('my-script', 'script_vars', $scriptvars);
}


add_action( 'wp_ajax_get_something', 'get_something');
add_action( 'wp_ajax_nopriv_get_something', 'get_something');

function get_something(){

    if( isset($_POST['is_admin']) && $_POST['is_admin'] == true ) {
        //The ajax request comes from admin area
        $is_admin = true;
    } else {
        $is_admin = false;
    }

    $data = array( 'is_admin' => $is_admin );
    wp_send_json_success( $data );

}

Then, the js code (for my-script.js file in this example):

(function($){
    $(document).ready(function(){
        jQuery.ajax({
            url: script_vars.ajaxurl,
            data: script_vars.data,
            type: 'post',
            success: function(response){
                alert(response.data.is_admin);
            }
         });
    });
})(jQuery);
| improve this answer | |
1

It is a bad idea IMO to have one request that behave differently depending if the are executed from page A or page B. Think of AJAX as a formal API and treat it like that and your life will be easier in the long run.

If you need two different behaviors on the front end and backend then just use two different requests.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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