[wp-hackers] [ wp-hackers ] wp _ enqueue _ script() not working while in the Loop

Otto otto at ottodestruct.com
Wed Dec 16 22:02:04 UTC 2009


Efficiency is biting you here.

The footer output works, but scripts still need to be enqueued before
the wp_head call. Even footer included ones.

The reason has to do with the way the print_scripts calls work. The
first one called from wp_head processes all the enqueued scripts. If
it see a footer one, it saves it in an array called "in_footer". The
print_footer_scripts then prints those out later.

So enqueueing in the middle of the page won't do you any good. After
wp_head, the scripts are fixed. What is known to be included has
already been decided, you can't add anything later.

If you want to determine whether a shortcode is used or not to include
a script, you have to scan ahead to the content. Write your own Loop
to look for the shortcode, then do a rewind to reset the loop for
another go-through by the main page.


-Otto
Sent from Memphis, TN, United States


On Wed, Dec 16, 2009 at 3:58 PM, TobiasBg <wp-hackers at tobias.baethge.com> wrote:
> Hi,
>
>> The wp_enqueue_script action happens just after wp_head, so unless your loop
>> happens before then(which would be very odd)  , it won't function.
>
> If you are refering to wp_enqueue_scripts (did you miss the s for a typo?), I can't really see that.
>
> "wp_enqueue_scripts" is attached to wp_head, correct.
> But it is nothing more than a wrapper for another action hook (with the same name). Basically to give developers a more logical place to use for enqueuing then pure wp_head.
> By default there's nothing attached to it (I couldn't grep anything).
>
> The actual printing of the scripts is done by "wp_print_head_scripts" and "wp_print_footer_scripts" (as far as I understand the code), which are attached to "wp_head" and "wp_footer" respectively.
>
> And while I'm in the Loop, I clearly enqueue the script before wp_footer. So I still don't get it, why it won't work... :-(
>
> Regards,
> Tobias
>
>
>
> _______________________________________________
> 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