[wp-hackers] wp _ enqueue _ script() not working while in the Loop
TobiasBg
wp-hackers at tobias.baethge.com
Wed Dec 16 20:49:39 UTC 2009
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
More information about the wp-hackers
mailing list