Skip to content

Instantly share code, notes, and snippets.

@jstnbr
Forked from adeel-raza/functions.php
Created January 1, 2023 15:43
Show Gist options
  • Save jstnbr/7a873c8b713933ecf631212ad8f24487 to your computer and use it in GitHub Desktop.
Save jstnbr/7a873c8b713933ecf631212ad8f24487 to your computer and use it in GitHub Desktop.
A very simple function to execute custom JS after an ajax request with a specific WordPress action is triggered
<?php
add_action( 'wp_enqueue_scripts', 'myprefix_add_custom_js');
function myprefix_add_custom_js() {
wp_add_inline_script( 'jquery',
'
jQuery(function($) {
/**
* catch AJAX complete events, to catch wordpress actions
* @param {jQuery.Event} event
* @param {jqXHR} xhr XmlHttpRequest object
* @param {Object} ajaxOpts options for the AJAX request
*/
$(document).ajaxComplete(function(event, xhr, ajaxOpts) {
// Specify the AJAX action after which you want to execute your JS
if ("data" in ajaxOpts && ajaxOpts.data.indexOf("action=wp_pro_quiz_load_quiz_data") != -1) {
// JS Code that runs after the specified WP action
alert(1)
}
});
});
'
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment