enqueue and localize script in footer

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 );
}

1 Answer
1

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' );

Leave a Comment