Is it possible to intercept all ajax requests and get the parameters and the returns?

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.

Read more here:: Is it possible to intercept all ajax requests and get the parameters and the returns?

Leave a Reply

Your email address will not be published. Required fields are marked *