Handling AJAX with custom taxonomy custom fields on add

You could achieve this with a little bit of jQuery:

Create this file within THEMEFOLDER/js/ called field-clear.js and replace #formID with the ID of the form you would like to reset:

//Code runs when an ajax request succeeds
jQuery( document ).ajaxSuccess(function( event, xhr, settings ) {
    //Check ajax action of request that succeeded
    if(settings.action == 'add_tag') {
        //Reset the form
        jQuery('#formID')[0].reset();

        //Send ajax response to console.
        console.log("Triggered ajaxSuccess handler. The ajax response was: " + xhr.responseText );
    }
});

Then in functions.php load your javascript:

wp_register_script('field-clear-script', get_stylesheet_directory_uri() . '/js/field-clear.js', array('jquery'), null, true);

function field_clear_load_scripts() {
    wp_enqueue_script('field-clear-script');
}
add_action('admin_enqueue_scripts', 'field_clear_load_scripts');

EDIT – Added code to display ajax response text. Added check for ajax action parameter. Changed enqueue function to enqueue on admin as I think this is where it’s required.

Leave a Comment

tech