Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a script which must run in my footer, after some variables are declared. It works if I just put the code directly in my footer file, but I think best practices dictate I should do this via functions.php and wp_localize_script.

Unfortunately that doesn't work; the script is always output in the header. Any ideas on where I'm going wrong? Thanks in advance for your help!

add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_front_page_scripts' );

function mytheme_enqueue_front_page_scripts() {
     wp_register_script('flowplayer_object',                    
        get_bloginfo('stylesheet_directory') . '/_/js/flowplayer/flowplayer-object-creator.js' );

     // last 'true' in wp_enqueue_script should force this into footer, right?
     wp_enqueue_script( 'flowplayer_object','','',true, true );

     $data = array( 'my_stylesheet_path' => __( get_bloginfo('stylesheet_directory') ) );
     wp_localize_script( 'flowplayer_object', 'my_data', $data );
}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

You should be setting it to show in the footer with the register, so your code should look like this:

wp_register_script(
    'flowplayer_object',
    get_bloginfo( 'stylesheet_directory' ) . '/_/js/flowplayer/flowplayer-object-creator.js',
    array(), // these are your dependencies, if you need jQuery or something, it needs to go in that array
    false, // set a version if you want
    true // this will put it in the footer
);

wp_enqueue_script( 'flowplayer_object' );
share|improve this answer
    
Thank you! Good to know. I'm assuming this means wp_register_script options override wp_enqueue_script options, since you can set it either place? –  Michelle Feb 4 '12 at 21:04
    
I think wp_enquque_script() has the final say...but if you set it in wp_register_script(), you don't have to set it in the enqueue –  m0r7if3r Feb 4 '12 at 21:26
    
Hmm, I was setting it in wp_enqueue_script and it wasn't taking (see above). Only worked when I moved it to wp_register_script. Ah well, just glad it's working. :) –  Michelle Feb 4 '12 at 23:09
    
Except that when you set it in the enqueue, you were clearing all the other properties. –  m0r7if3r Feb 4 '12 at 23:38
    
In this context, you should probably just get rid of the wp_register_script(), and put everything in wp_enqueue_script(), since it will handle the script-registering itself. –  Chip Bennett Feb 5 '12 at 2:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.