最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can't enqueue scripts in the footer?

programmeradmin0浏览0评论

The WordPress codex says:

(old codex page)

$in_footer

(boolean) (optional) Normally scripts are placed in the section. If this parameter is true the script is placed at the bottom of the . This requires the theme to have the wp_footer() hook in the appropriate place. Note that you have to enqueue your script before wp_head is run, even if it will be placed in the footer. (New in WordPress 2.8) Default: false

So I added true after each script's src path:

/**
     * JavaScript
     */

    function my_scripts_method() {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', '.6/jquery.min.js', true );
        wp_enqueue_script( 'jquery' );
    }

    add_action('wp_enqueue_scripts', 'my_scripts_method');

    function media_queries_script() {
        wp_register_script( 'mediaqueries', get_template_directory_uri() . '/js/css3-mediaqueries.js', true );
        wp_enqueue_script( 'mediaqueries' );
    }

    add_action('wp_enqueue_scripts', 'media_queries_script');

    function custom_script() {
        wp_register_script( 'custom', get_template_directory_uri() . '/js/custom.js', true );
        wp_enqueue_script( 'custom' );
    }

    add_action('wp_enqueue_scripts', 'custom_script');

    function replace_script() {
        wp_register_script( 'replace', get_template_directory_uri() . '/js/jquery.ba-replacetext.min.js', true );
        wp_enqueue_script( 'replace' );
    }

    add_action('wp_enqueue_scripts', 'replace_script');

But the scripts are still being included in the header.

Any suggestions to fix that?

The WordPress codex says:

(old codex page)

$in_footer

(boolean) (optional) Normally scripts are placed in the section. If this parameter is true the script is placed at the bottom of the . This requires the theme to have the wp_footer() hook in the appropriate place. Note that you have to enqueue your script before wp_head is run, even if it will be placed in the footer. (New in WordPress 2.8) Default: false

So I added true after each script's src path:

/**
     * JavaScript
     */

    function my_scripts_method() {
        wp_deregister_script( 'jquery' );
        wp_register_script( 'jquery', 'http://ajax.googleapis/ajax/libs/jquery/1.6/jquery.min.js', true );
        wp_enqueue_script( 'jquery' );
    }

    add_action('wp_enqueue_scripts', 'my_scripts_method');

    function media_queries_script() {
        wp_register_script( 'mediaqueries', get_template_directory_uri() . '/js/css3-mediaqueries.js', true );
        wp_enqueue_script( 'mediaqueries' );
    }

    add_action('wp_enqueue_scripts', 'media_queries_script');

    function custom_script() {
        wp_register_script( 'custom', get_template_directory_uri() . '/js/custom.js', true );
        wp_enqueue_script( 'custom' );
    }

    add_action('wp_enqueue_scripts', 'custom_script');

    function replace_script() {
        wp_register_script( 'replace', get_template_directory_uri() . '/js/jquery.ba-replacetext.min.js', true );
        wp_enqueue_script( 'replace' );
    }

    add_action('wp_enqueue_scripts', 'replace_script');

But the scripts are still being included in the header.

Any suggestions to fix that?

Share Improve this question edited Sep 6, 2019 at 1:53 gregn3 1767 bronze badges asked Jan 5, 2012 at 4:58 wycwyc 3,90719 gold badges61 silver badges97 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 21

Where did you put your code ?

  1. the "true" arguments goes in the wp_register_script();, not the wp_enqueue_script() ;

the functions is:

<?php wp_enqueue_script('handle', 'src', 'deps', 'ver', 'in_footer'); ?>

meaning

    <?php wp_enqueue_script('NameMySccript', 'path/to/MyScript', 
'dependencies_MyScript', 'VersionMyScript', 'InfooterTrueorFalse'); ?>

E.G.

<?php wp_enqueue_script('my_script', WP_CONTENT_URL . 'plugins/my_plugin/my_script.js', array('jquery', 'another_script'), '1.0.0', true); ?>

  1. does your theme have <?php wp_footer(); ?> at the end of the page ?

3.add the action with add_action('wp_print_scripts', 'your function');

That being said , your best practice would be :

<?php  
if (function_exists('load_my_scripts')) {  
    function load_my_scripts() {  
        if (!is_admin()) {  
        wp_deregister_script( 'jquery' );  
        wp_register_script('jquery', 'http://ajax.googleapis/ajax/libs/jquery/1.6.4/jquery.min.js');  
        wp_enqueue_script('jquery');  
        wp_register_script('myscript', bloginfo('template_url').'/js/myScript.js'__FILE__), array('jquery'), '1.0', true );  
        wp_enqueue_script('myscript');  
        }  
    }  
}  
add_action('init', 'load_my_scripts');  
?>  

or add_action('wp_print_scripts', 'dl_register_js');

You are passing a bool value to the dependency param. Try this:

 wp_enqueue_script(
      'replace', 
       get_template_directory_uri().'/js/jquery.ba-replacetext.min.js',
       array('jquery'), 
       '', 
       true
);

I had the same problem and after doing some search I was missing this function in my custom theme:

 <?php wp_footer();?>

I'd like to simplify the answer here.. When using wp_enqueue_script or wp_register_script to load a script in the footer, you need to make sure you include the other params too, even if they are blank.

I was trying..

wp_register_script( 'rrssb', get_stylesheet_directory_uri() . '/includes/rrssb.min.js',  true);

Which didn't work as @janoChen has noticed. Wordpress calls for

wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );

So you need to include '', for the parameters you are not using if you want to declare the function inline. Therefore the code below works as planned.

wp_register_script( 'rrssb', get_stylesheet_directory_uri() . '/includes/rrssb.min.js', '', '', true);

Subsequently you can declare the function in an array like format like @Brian Fegter shows.

If you enqueue a script to the header but declare a dependency to a script enqueued to the footer, WordPress will move the footer script to the header to make it available to the dependent script/s.

According to the official WordPress documentation (Last Checked - May 2018):

https://developer.wordpress/reference/functions/wp_enqueue_script/

and personal experience of 7+ years.

  1. You should try always use 'wp_enqueue_scripts' function for better compatibility with installed plugins/themes.

     add_action('wp_enqueue_scripts','your_plugin_enqueue');
    
  2. You can find more about using official docs Usage here

       /**** wp_register_script, wp_enqueue_script, wp_dequeue_script ***
    
             *** Personal recommendation
             *  - I strongly recommend registering all scripts before enqueueing,
             *  again -> for better compatibility with other developers. 
             *  :::: For example, so others could manipulate the position 
             *       of your scripts using the wp_dequeue_*type* method. 
             *       (wp_dequeue_script, wp_dequeue_style,...)
    
             *** Official Docs
             *   $in_footer
             *      - (bool) (Optional) Whether to enqueue the script before </body> instead of in the <head>. Default 'false'.
             *      - Default value: false
             *
             ***/
    
             // examples:
    
      function your_plugin_enqueue(){
    
    
    
             wp_dequeue_script('some_script(s)'); // remove conflicting script
    
             wp_register_script( $handle, $src, $deps, $ver, $in_footer=true ); 
             wp_enqueue_script($handle);
             wp_register_script('some_script(s)', $src, $deps, $ver, $in_footer=true);  // re-enqueue scripts after yours for example to elimination of conflicts
    
             wp_enqueue_script('some_script(s)');
    
      }
    
    
      add_action('wp_enqueue_scripts','your_plugin_enqueue');
    

P.S. If you find this answer useful, please give it an '▲' (ArrowUP), if you think that it can be improved, please comment below.

发布评论

评论列表(0)

  1. 暂无评论