mattheu / MinQueue

WordPress Plugin: Minify and concatenate enqueued scripts and styles
60 stars 18 forks source link

Option to enqueue minified scripts in the footer #21

Open einkoro opened 10 years ago

einkoro commented 10 years ago

Could we get an option to force the minified js to be enqueued in the footer? A lot of plugins do not enqueue in the footer by default and dequeueing and enqueuing them from the theme is a less than desirable work around.

djmadeira commented 10 years ago

Yes, this would be really helpful because right now the only two options are 1. don't use this plugin and manually deregister & register JS, or 2. minify & concat, but don't get the benefits of footer JS.

mattheu commented 10 years ago

Thanks for your feedback.

The plugin loads the script in the location specified when enqueueing the script. My concern with forcing it to load in the footer, is that it could easily break stuff. I think the first thing people would do is move jQuery to the footer, and it would cause lots of support requests!

My suggestion would be to de-register, and re-register the JS, but continue to use the plugin to minify and concat. But watch out for JS errors.

I'll think about this, but perhaps there should be another plugin that allows you to modify where scripts are loaded?

djmadeira commented 10 years ago

So it works to de-register and re-register, then have the plugin pick them up and concatenate? That's not so bad. Thanks!

What happens when some JS in a concat group is registered for the footer, and some for the header?

einkoro commented 10 years ago

Deregistering and registering works well enough until you find yourself in a child theme with a pile of dependancies. If the ability to add async/defer ever gains traction via additional parameters or a filter there really isn't any need to consider this. Then again its been four years running now: https://core.trac.wordpress.org/ticket/12009 https://core.trac.wordpress.org/ticket/22249

I suppose you could hack this into minqueue itself but its awfully dirty, subject to break whenever core gets around to handling it and probably better left to another plugin or the theme's functions.php for now. I'm using a mu-plugin with this at the moment:

function async_scripts($url) {
    if ( FALSE === strpos($url, '.js') ) { 
        return $url;
    }
    return "$url' async='async";
}
add_filter('clean_url', 'async_scripts', 11, 1);