[wp-hackers] Including javascript in the footer

Otto otto at ottodestruct.com
Wed May 11 06:37:30 UTC 2011


Enqueueing scripts and styles should always be done on either the
wp_enqueue_scripts hook or the admin_enqueue_scripts hook, depending
on which side of things you want to enqueue them on.

This applies to both header and footer scripts. wp_print_scripts is
really too late to enqueue, you should only dequeue there.

If you're not deciding until midway through the page, then
you're-doing-it-wrong (tm). You can't enqueue once scripts have
printed in the header, due to possible dependencies. For example, if a
script enqueued in the head depends on a script enqueued in the
footer, then the footer script gets promoted to the header. This is
part of the purpose of the script dependency system.

Redo your methodology to make the decision earlier. If this means you
need to look ahead at the content, then do so.

-Otto



On Wed, May 11, 2011 at 1:32 AM, Philip Walton <philip at philipwalton.com> wrote:
> I've tested wp_enqueue_script() and it doesn't work if called from any
> action after 'wp_print_scripts' in a page request.
>
> The Codex recommends: "Use the wp_enqueue_scripts action to call this
> function, or admin_enqueue_scripts to call it on the admin side. Calling it
> outside of an action can lead to troubles."
>
> In my case I'm making a programmatic decision partway through the page
> request whether or not I want to include a script in the footer, and
> unfortunately, that decision is being made after the 'wp_print_scripts' hook
> is fired.
>
> So what should I do? I don't want to just echo the script markup in
> 'admin_print_footer_scripts' because that just seems wrong. However, I can't
> really think of any other good way.
>
> Any ideas?
>
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list