• Hi, i’m refactoring the javascript of my plugin to use requirejs.
    This will allow me to avoid exposing any global javascript variable, and i will be able to include my version of the libraries so that i will not depend on wordpress version anymore (yes, there is a little overhead, but in this way i’m sure that my plugin will work exactly the same on each wordpress version). The only global variables i’m using are those created by wp_localize_script(), and that’ something i would like to get rid of.

    So i would like to create a new function wp_localize_script_define() which, instead of creating a global variable, creates a module. something like

    <script type="text/javascript">
    define( "my/module", { property : value } );
    </script>

    that seems to be possible because after all wp_localize_script() uses function of the globally available $wp_scripts class, the only thing that would be very welcome would be the ability to add the script AFTER and not before another script, is there a way to do this?But things could work even without that, apart from that do you spot any potential problem?

Viewing 1 replies (of 1 total)
  • You only need wp_localize_script if you’re using ajax with WordPress, although in practice all that function does in my experience is creates a global with a URL to the wp-admin/admin-ajax.php file. There’s no reason why you can’t go and create this global privately in a requirejs script and remove wp_localize_script from your php file.

Viewing 1 replies (of 1 total)
  • The topic ‘Use requirejs modules to localize scripts instead of global variables’ is closed to new replies.