开发者

WordPress wp_register_script function not loading script in footer

I am trying to load JQuery hosted on Google in my blog's footer just before the body finishes. This is the function I am using in my functions.php file.

function load_jquery() {
if (!is_admin()) {
    wp_deregister_script('jquery');
    // load the Google API copy in the footer
    wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js', array(), '1.5.1', 1);
    wp_enqueue_script('jquery');
}
}
add_action('init', 'load_jquery');

However it keeps being loaded in the header. I can't figure out why.

I also added the following function in the functions.php file and it works fine;

function your_funct开发者_开发百科ion() {
    echo '<p>This is inserted at the bottom</p>';
}
add_action('wp_footer', 'your_function');

Appreciate the help.


I suspect that one of the other scripts that is dependent on jquery is not set to load in the footer. Since the script is dependent on jquery, jquery will be loaded in the head.


This works for me

function add_scripts() {
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', false, '1.4.2', true);
        wp_enqueue_script( 'jquery' );
}
add_action('wp_enqueue_scripts', 'add_scripts');


There is flag in wp_enqueue_script function:

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

You can specify that if you want to load the script in footer. take a look at the documentation: http://codex.wordpress.org/Function_Reference/wp_enqueue_script


First, you should use

add_action('wp_enqueue_scripts', 'add_scripts');

As noted by Unknown_Guy.

Secondly, to load it only for pages that NEED the jQuery (non-admin pages in your case) then move the wp_enqueue_script('jquery') line to a function that is only run when rendering user pages.

In our plugins we only call wp_enqueue_script('jquery') in the function that renders the shortcode. That way jQuery is only loaded when shortcode rendering happens, which is when we need it. This will not load on admin pages or pages where our shortcodes are not in use.


There may be an undefined function that getting triggered before wp_footer() function.

For my situation:

I have buddypress functions in my template files but I have deactivated buddypress plugin so functions were not working. To solve problem I added if function exists before all buddypress functions.

Like so; Before:

<li class="show_notification_number"><?php
               bp_core_get_notifications_for_user();
                ?></li>
.
.
.
wp_footer();
.
.
.

After:

<li class="show_notification_number"><?php 
               if( function_exists( 'bp_core_get_notifications_for_user' ) ) {
                  bp_core_get_notifications_for_user();
                  } ?>
</li>
.
.
.
wp_footer();
.
.
.

I did adding function exists for all undefined (even for all) functions.


your mistake is that in the last parameter of wp_register_script() you are passing 1.

If you read the documentation on wordpress the last parameter is a boolean, so you should pass either true or false and not 1 or 0

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜