3

Is there any hook that always goes through it, where I can see all the requests, and also the returns after the "action" that is executed?

I need to intercept all requests, including requests from WooCommerce, from what I've seen, it looks like it uses another way to do ajax requests.

I need something like this:

Ajax (js object):

{
    action: 'getSomePostsHtml',
    param1: 'some param 1',
    param2: 'some param 2'
}

Other side by PHP, on my functions.php or other file...:

function someAjaxSent() {
    // Some ajax action are sent? pass here...
    echo '<pre>';
    print_r($_REQUEST);
    echo '</pre>';

    // show something like this
    // [
    //  action: 'getSomePostsHtml',
    //  param1: 'some param 1',
    //  param2: 'some param 2',
    // ]
}

And on return, show what returns after execute action:

function someAjaxSentReturnData($dataReturnFromAction) {
    // after execute action, return some data, pass here...
    echo '<pre>';
    print_r($dataReturnFromAction);
    echo '</pre>';

    // Array (
    //  [fragment] => Array (
    //      div.something => '<div>Data of posts html...</div>',
    //      div.somehingElse => '<p>Lorem ipsum...</p>'
    //  )
    //  [hash] => ASDF123
    //  [postsFound] => 20
    //  ...
    // )
}

My idea is to know and save log of all what users are doing in the site/store via ajax, for example:

  • loading parts of the site via ajax
  • filtering products via ajax
  • adding to the cart via ajax
  • recalculating shipping via ajax
  • removing products from cart via ajax
  • adding filters and etc...

The part of save this information is already done, I already use in the pages loading with hook add_action('template_redirect', 'myInterceptFunction'); now I just need to also intercept the ajax requests.

4
  • Keep in mind a lot of newer plugins use the REST API endpoints, not admin AJAX, and that for neither of those is a template loaded, so template_redirect wouldn't work
    – Tom J Nowell
    Commented Feb 23, 2019 at 0:32
  • Yes, I use "template_redirect" only for standard navigation (not for ajax), and seemingly solved for almost every request (except for ajax), now I need some hook that does something similar but in ajax requests. Commented Feb 23, 2019 at 11:27
  • 1
    One answer available here wordpress.stackexchange.com/a/282651/119672 Commented Apr 15, 2020 at 9:26
  • @VivekAthalye Doesn't allow you to access or manipulate the response - only completely override it. Commented Mar 10, 2022 at 12:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.