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

Aaron Jorbin aaron at jorb.in
Wed Dec 16 21:38:25 UTC 2009


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.

http://aaron.jorb.in
twitter: twitter.com/aaronjorbin


On Wed, Dec 16, 2009 at 12:49 PM, TobiasBg <wp-hackers at tobias.baethge.com>wrote:

> Hi,
>
> for a plugin, I'm trying to achieve something really simple (I would
> think):
> I have a shortcode that gets replaced with some string, as usual.
> Now I want to enqueue a JavaScript file *to the footer* while parsing the
> Shortcode.
> That way, the JS file will only be included on pages where the Shortcode is
> actually shown.
> And by using wp_enqueue_script I can make sure that it is still only loaded
> once in case there is more than one of the same Shortcodes on the page.
>
> Now the problem: It doesn't work. It seems that I can not enqueue a script
> to the footer, while I'm already in the Loop, although there's nothing that
> should prohibit that.
>
> Can someone kindly point me to the right direction? Am I doing something
> wrong or is it just not possible to add scripts to the footer like that? (If
> not, it should.) Thanks!
>
> This is a short demo plugin, illustrating what I'm trying to do. It works
> except for the inclusion of the JS.
> To try it, just put a shortcode like [demo foo="my-string" /] into a post.
>
> ##################################################
> <?php
> /*
> Plugin Name: Demo Enqueue Script
> */
>
> function demo_shortcode_handler( $atts ) {
>    $default_atts = array( 'foo' => 'bar' );
>    $atts = shortcode_atts( $default_atts, $atts );
>
>    // why does the JS not show up?
>    wp_enqueue_script( 'demo-js', 'path/to/demo.js', false, '1.0', true );
>
>    return 'Your foo argument was: ' . $atts['foo'];
> }
>
> add_shortcode( 'demo', 'demo_shortcode_handler' );
>
> ##################################################
>
> Thanks a lot!
> 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