Skip to content

Instantly share code, notes, and snippets.

@faishal
Last active May 9, 2018 03:04
Show Gist options
  • Save faishal/f71b78a5c0d02b12048975e1f01c0af5 to your computer and use it in GitHub Desktop.
Save faishal/f71b78a5c0d02b12048975e1f01c0af5 to your computer and use it in GitHub Desktop.
Reset WordPress add term form after save.
/**
* Add ajaxprefilter to reset the form
*/
function initAjaxPreFilter() {
// Add callback
$( document ).on( 'term:added', function() {
// Do reset form related stuff here
} );
// Register ajaxprefilter
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
// Convert query string to object
if ( 'string' === typeof( originalOptions.data ) ) {
originalOptions.data = parseQuery( originalOptions.data );
}
// Modify options, control originalOptions, store jqXHR, etc
if ( null === originalOptions.data || 'undefined' === typeof ( originalOptions.data ) || 'undefined' === typeof ( originalOptions.data.action ) ) {
return true;
}
if ( 'add-tag' === originalOptions.data.action ) {
var orignalSuccess = originalOptions.success;
options.success = function( response ) {
orignalSuccess( response );
// Reset image field
$( document ).trigger( 'term:added', response );
};
}
});
}
/**
* Convert query string to object
* @param qstr
* @returns {{}}
*/
function parseQuery( qstr ) {
var query = {};
var a = qstr.substr( 0 ).split( '&' );
for ( var i = 0; i < a.length; i++ ) {
var b = a[i].split( '=' );
query[decodeURIComponent( b[0] )] = decodeURIComponent( b[1] ||
'' );
}
return query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment