Register plugin scripts to be enqueued to the footer #478

Open
wants to merge 2 commits into
from

Projects

None yet

2 participants

@jedrzejchalubek
Contributor
jedrzejchalubek commented Jan 7, 2017 edited

Currently plugin scripts are enqueued to the head. This PR adds $in_footer argument to the wp_enqueue_script function, so scripts will be included before </body>.

Also makes sure that autocomplete.php file will be echo out after plugin scripts.

@rayrutjes
Member

Thanks a ton @jedrzejchalubek

Regarding enqueuing everything in the wp_footer, I have some concerns.
Indeed, some users are reporting that their theme does not use the wp_footer hook, which breaks the plugin.
If we are to inject everything in the footer, I suppose we would have to first implement #310 so that we let the user know about the missing wp_footer in their theme. Not sure it is easy to detect BTW.
Also not sure if we really want to put everything in the footer, I'm wondering if it wouldn't be easier to put everything in the header by default.

Maybe we can have the best of both world and introduce a new constant: ALGOLIA_ENQUEUE_SCRIPTS_IN_FOOTER

Regarding making sure autocomplete.php is always injected last, that is definitely something I'd like to merge, I will cherry pick it. Wondering if this resolves #467

Your feedback is highly appreciated!

@rayrutjes
Member

I just merged e06bfe8 manually, I'm happy to discuss the other commit according to my answer ;)

@jedrzejchalubek
Contributor

I don't think there is any "non-hacky" way to detect that. Especially in this situation. The wp_enqueue_scripts is always called before wp_footer action, so we can't check if wp_footer was called in wp_enqueue_scripts hook.

Let me quote some wise words from stackoverflow:

Any Theme that doesn't call wp_head() and wp_footer() has far bigger problems, though; they are essential to core functionality at this point.
http://wordpress.stackexchange.com/a/21778/93299

In my opinion, only resonable solution/compromise is to introduce new option in settings (for example checkbox: Move scripts to footer), which by default will be unchecked. If someone want to enqueue plugin scripts to footer, will have to update this setting. true/false value from this field can be directly used in wp_register_script function.

@rayrutjes
Member

@jedrzejchalubek I agree with your different statements.
I think I will probably merge this after some testing.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment