How to put my javascript in the footer

后端 未结 3 1668
慢半拍i
慢半拍i 2020-12-05 02:21

I just want to ask on how to print script \'javascript\' at the footer using simple plugin. I\'m using WordPress 3.0 any ideas?

相关标签:
3条回答
  • 2020-12-05 02:36

    For an external javascript file to be linked in the footer, use this (>= WP2.8)

    function my_javascripts() {
        wp_enqueue_script( 'the-script-handle', 
                           'path/to/file.js', 
                           array( 'jquery','other_script_that_we_depend_on' ), 
                           'scriptversion eg. 1.0', 
                           true);
    }
    add_action( 'wp_enqueue_scripts', 'my_javascripts' );
    

    That last true means that the script should be put at the wp_footer() hook.

    0 讨论(0)
  • 2020-12-05 02:37

    Use a functions.php file inside your theme template add this :

    <?php
    
    function add_this_script_footer(){ ?>
    
    [YOUR JS CODE HERE]
    
    <?php } 
    
    add_action('wp_footer', 'add_this_script_footer'); ?>
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-05 02:46

    Hum may be it's too late to answer, but if anyone else comes here with the same problem :

    There is a plugin to do this : http://wordpress.org/extend/plugins/footer-javascript/

    Or you can doing this manually by adding this short code in your functions.php :

    /**
     * Automatically move JavaScript code to page footer, speeding up page loading time.
     */
    remove_action('wp_head', 'wp_print_scripts');
    remove_action('wp_head', 'wp_print_head_scripts', 9);
    remove_action('wp_head', 'wp_enqueue_scripts', 1);
    add_action('wp_footer', 'wp_print_scripts', 5);
    add_action('wp_footer', 'wp_enqueue_scripts', 5);
    add_action('wp_footer', 'wp_print_head_scripts', 5);
    
    0 讨论(0)
提交回复
热议问题