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.

For a Wordpress Theme I know javascript files (ending in .js) must be included using wp_enqueue_script in functions.php but how do I include additional javascript code, for example say I need to add this:

jQuery(function($){
    jQuery.supersized({
        slides:
        [
             'http://google.com',
             'http://google.co.uk'
        ]
    });
});

Where would I put this? Do I put it within <script> tags in the header.php?

Thanks, Ben

share|improve this question
add comment

2 Answers

Use wp_localize_script to pass data from php to your javascript:

wp_register_script(
    'wpa_script',
    get_template_directory_uri() . '/js/script.js',
    array('jquery'),
    null,
    false
);

wp_localize_script(
    'wpa_script',
    'WPAData',
    array(
        'slides' => array( 'http://google.com', 'http://google.co.uk' )
    )
);
share|improve this answer
add comment

Just put it inside a callback, hooked into the correct action:

add_action( 'wp_print_scripts', 'wpse88383_print_scripts' );

function wpse88383_print_scripts() {
    if ( ! is_admin() ) {
        ?>
            <script type="text/javascript">
                jQuery(function($){
                    jQuery.supersized({
                        slides:
                        [
                        'http://google.com',
                        'http://google.co.uk'
                        ]
                    });
                });
            </script>
        <?php
    }
}
share|improve this answer
add comment

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.