• This plugin alters the global $.ajax method causing problems for any other plugins that wish to use jQuery ajax.

    Plugin authors, please be a good citizen and;

    1. Do not alter the global function with a ajaxPrefilter, instead add your parameters in the ajax call.
    2. Do not enqueue your scripts on every wp-admin page. If you want to break ajax on your settings page, fine, but don’t impose it on everyone else.

    https://wordpress.org/plugins/polylang/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Chouby

    (@chouby)

    Hi!

    This plugin alters the global $.ajax method causing problems for any other plugins that wish to use jQuery ajax

    Since this feature is included in Polylang, I am aware of only one plugin conflicting with it (raised very recently and hoping to solve it).

    Do not alter the global function with a ajaxPrefilter, instead add your parameters in the ajax call.

    ajaxPrefilter is just a hook to jQuery ajax calls. I use this possibility offered by jQuery as any other plugin authors would use WP filters to alter WP variables.

    Do not enqueue your scripts on every wp-admin page. If you want to break ajax on your settings page, fine, but don’t impose it on everyone else.

    I enqueue scripts only when needed.

    Thread Starter kilbot

    (@kilbot)

    Since this feature is included in Polylang, I am aware of only one plugin conflicting with it (raised very recently and hoping to solve it).

    The problems I describe will affect any other plugin that uses jQuery.ajax and encodes the request payload as application/json, for example, any plugin using Backbone with default settings.

    ajaxPrefilter is just a hook to jQuery ajax calls. I use this possibility offered by jQuery as any other plugin authors would use WP filters to alter WP variables.

    $.ajaxPrefilter is a global method. This means that any call to $.ajax will also have to run through your filter. ajaxPrefilter is more like pluggable functions than WP filters. It would be like changing the core code of a filter so it returns a string when the filter only expects arrays.

    Do you understand?

    I enqueue scripts only when needed.

    With your plugin installed, go to the WP Dashboard and View Source. Do a search for ‘ajaxPrefilter’ and you will find your script … is it needed on the Dashboard? Go to any other page in the WP Admin and do the same test and you will find your script.

    You should use the current_screen hook and only enqueue your scripts if the $current_screen->id matches your page id.

    Plugin Author Chouby

    (@chouby)

    I know that my script loads on every admin page. This is the intention. Currently WP does not provide any way to know if an ajax request is made on frontend or admin side. I found this way. If you have a better solution, please share.

    Thread Starter kilbot

    (@kilbot)

    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.

    It seems like Polylang is also screwing with my plugin, wp-sync-db, see issue #74 for details.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Polylang alters the global $.ajax’ is closed to new replies.