Last active
May 9, 2018 03:04
-
-
Save faishal/f71b78a5c0d02b12048975e1f01c0af5 to your computer and use it in GitHub Desktop.
Reset WordPress add term form after save.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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