Enqueuing Script in functions.php vs on the page

Does it matter if I enqueue a script on just the page I need it to be used?As opposed to functions.php?Or, what is the

Does it matter if I enqueue a script on just the page I need it to be used? As opposed to functions.php?

Or, what is the best practice? In this example I need a rotator just on the front page of my site.

And if I do enqueue it on the page, is there a best place to put it?

Does it matter if I enqueue a script on just the page I need it to be used? As opposed to functions.php?

Or, what is the best practice? In this example I need a rotator just on the front page of my site.

And if I do enqueue it on the page, is there a best place to put it?

Share Improve this question edited Sep 22, 2016 at 4:22 Dave Romsey 17.9k11 gold badges56 silver badges70 bronze badges asked Oct 7, 2011 at 19:40 jw60660jw60660 1,5462 gold badges14 silver badges28 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

Go with functions.php.

Use the wp_enqueue_scripts action to call this function, or admin_enqueue_scripts to call it on the admin side. Calling it outside of an action can lead to problems. See #11526 for details.

Source: http://codex.wordpress/Function_Reference/wp_enqueue_script

function themename_load_js() {

    if ( is_admin() ) {
        return;
    }

    if is_page( 123) { // Replace 123 with your page's ID.
        wp_enqueue_script( 'jquery' );
        wp_enqueue_script( 'rotator-js', get_template_directory_uri() . '/js/rotator.js', array ( 'jquery' ) );
        wp_enqueue_script( 'rotator-init-js', get_template_directory_uri() . '/js/rotator-init.js', array ( 'jquery', 'rotator-js' ) );
    }
} 
add_action( 'wp_print_scripts', 'themename_load_js' );

Edit: Bonus tip: Whenever I need to do something like this, rather than hard coding the page ID in the function, I create an options page and add a call to wp_dropdown_pages() which allows the user to choose the special page. You said that you're using the front page, so you could probably leverage is_front_page() within your function rather than checking the page ID, which is even cleaner.

This is absolutely working for me right now:

add_action('get_header', function() {
    if(is_page('566')) {

        function sp_enqueue_script() {
            wp_enqueue_script(
                'sp-custom-script',
                get_stylesheet_directory_uri() . '/assets/js/sp_script.js',
                array( 'jquery' ), '1.0', true
            );
        }

        add_action( 'wp_enqueue_scripts', 'sp_enqueue_script' );
    }
});

is_page doesn't work in functions.php because the main query hasn't loaded yet, so wait for the header to load.

Last boolean in the wp_enqueue_script hook is for the script to be enqueued in the footer, so it runs faster.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745261397a4619227.html

相关推荐

  • Enqueuing Script in functions.php vs on the page

    Does it matter if I enqueue a script on just the page I need it to be used?As opposed to functions.php?Or, what is the

    3小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信