From what I can gather you wish to output some parameters and use them in you ajax call. The way most developers will do this is to create a global variable. For example, WordPress adds global variables ajaxurl
, pagenow
, adminpage
etc to every WP Admin page. Cluttering the global namespace is also bad practice but it’s certainly better than overwriting a global function.
So you could do something like:
<script>
var polylang = {
foo: 'bar',
baz: true
};
</script>
These parameters will be attached to the global window so you can now access them from within your js function, eg:
(function($) {
var data = {
action: 'ajax_action',
security: 'nonce',
foo: polylang.baz ? polylang.foo : 'default';
};
$.post( ajaxurl, data, function( resp ) {
alert( "Response: " + resp );
});
})(jQuery);
As one last illustration of why overwriting the global jquery functions is bad practice please consider this situation: Another plugin author, let’s call him Leroy, has created a Super Awesome Plugin. Leroy also wants to add parameters to every ajax call so he uses $.ajaxPrefilter. Leroy uses admin_print_footer_scripts
on every page, just like you do, except he uses priority 11, so his script executes just after yours. Now Leroy has overwritten your $.ajaxPrefilter. It’s not like WP filters where things get chained, Leroy has just stomped on your prefilter. Leroy is a c**t.