$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Move Jquery.js to Footer|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Move Jquery.js to Footer

matteradmin10PV0评论

I want to move jquery.js from header to footer. I have tried following code:

//Remove jQuery Default
function replace_jquery() {
 if (!is_admin()) {
 wp_deregister_script('jquery');
 }
}
add_action('init', 'replace_jquery');

//Load on Footer
add_action('wp_footer', 'jquery_code');
function jquery_code(){
?>
<script type='text/javascript' src='.js'></script>
<?php
};

The issue is that If I run first piece of above code, js files of my theme are removed with jquery.js. Because theme's static js files is calling via array( 'jquery' ) attributes.

I just want to move jquery only from header to footer.

NOTE: I have tried other suggestions on Stackexchange but none of them didn't work on my blog. (Such us: Enqueue core jQuery in the footer?)

How can I do it?

I want to move jquery.js from header to footer. I have tried following code:

//Remove jQuery Default
function replace_jquery() {
 if (!is_admin()) {
 wp_deregister_script('jquery');
 }
}
add_action('init', 'replace_jquery');

//Load on Footer
add_action('wp_footer', 'jquery_code');
function jquery_code(){
?>
<script type='text/javascript' src='https://example/wp-includes/js/jquery/jquery.js'></script>
<?php
};

The issue is that If I run first piece of above code, js files of my theme are removed with jquery.js. Because theme's static js files is calling via array( 'jquery' ) attributes.

I just want to move jquery only from header to footer.

NOTE: I have tried other suggestions on Stackexchange but none of them didn't work on my blog. (Such us: Enqueue core jQuery in the footer?)

How can I do it?

Share Improve this question asked Jan 10, 2019 at 14:48 Serdar KoçakSerdar Koçak 522 silver badges7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

When you deregister jQuery, you have to register it again. Try this code

UPDATE: You have to deregister jQuery first (first part of your code), so the final code should be:

//Load on Footer
add_action('wp_enqueue_scripts', 'register_jquery');
function register_jquery() {
    wp_deregister_script('jquery'); // remove original jQuery
    wp_register_script( // add custom jQuery in footer
       'jquery', 
       'https://ajax.googleapis/ajax/libs/jquery/3.1.1/jquery.min.js',
       array(), // dependencies
       null, // version
       true); // load in footer?
    wp_enqueue_script('jquery');
}

But it's NOT RECOMMENDED to load custom jquery in wordpress.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far