I’d like to take advantage of loading my scripts while the page is loading. ie. Yepnope, head.js etc…
But I also want to be able to enqueue the script so other plugins don’t try adding their own. (a la jquery)
Does anyone have any suggestions?
Thanks in advance…
Post Views: 0
You can’t have both: you either use wp_enqueue_script() or head.js
Or, you do some wacky hacking to translate the WP script dependency tree into head.js calls.
NB: Replace head.js with whatever library you want to use.
This should be a comment, but I don’t have those privilages yet.
But if primary concern is just to prevent duplication, deregister the script, then register the same script with no source and enqueue it. Essentially, wp will run a blank script to block every other register/enqueue version of it.
Then add the script with the method of your choice. I haven’t done thorough testing, but got the tip from somewhere else, and in theory it should work fine.
Here’s what I’m doing to accomplish this. I’m posting this as a possible solution, and also to get feedback from anyone with more WP experience than me. If you see any problems with my implementation or anything I’m not considering, please let me know.
Known limitations:
wp_enqueue_script
<head>
section of the page. This might be easy to work around, but for my implementation I prefer it this way.The problem I saw with the head.js plugin was that it only seemed to work on scripts in the
<head>
section, and the fact that it didn’t allow for any easy CDN fallback functionality which is a really nice benefit of loading scripts this way in my opinion.Ok, here’s the code. This is part of a class that is in my general functionality plugin, but it could just as easily be in
functions.php
or organized in some other way. I’m using yepnope for now, but this could easily be modified to use another script loader.I just released a plugin that should be a dropin for wordpress installations. Still very early and I am sure there will be bugs, but it has worked for me on a site I am working on:
http://wordpress.org/extend/plugins/asynchronous-javascript/
Here is the easy way for adding asynchronous to all custom js files in WordPress.
Add in
functions.php
: