Wordpress: Load only one jquery script on website

2019-04-15 01:03发布

Just about to launch a WordPress site but have noticed that it's currently loading in two jquery files, one in wp-includes and one from my header.php, is there a way to make wordpress load the wp-include one on the front end? Done quite a bit of search and have the only mention of this seems to include the following code, but I can't find any documentation about it, any ideas?

<?php wp_enqueue_script("jquery"); ?>

4条回答
在下西门庆
2楼-- · 2019-04-15 01:08

As of WordPress 3.3, this is the best way to do it, using the proper hook:

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
    wp_deregister_script('jquery');
    wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
    wp_enqueue_script('jquery');
}
查看更多
Luminary・发光体
3楼-- · 2019-04-15 01:13

Actually, you need to use admin_init hook to make it work in admin section:

function jquery_for_admin() {
  wp_enqueue_script('jquery');
  return;
}

add_action('admin_init', 'jquery_for_admin');
查看更多
Summer. ? 凉城
4楼-- · 2019-04-15 01:21

you need to include the following code before <?php wp_head(); ?> in your header.php

<?php wp_enqueue_script("jquery"); ?>

and you can remove other jquery includes from header.php

查看更多
欢心
5楼-- · 2019-04-15 01:23

In addition to what Aram Mkrtchyan said, you can enqueue your scripts also using wp_enqueue_script().

<?php
    wp_enqueue_script('jquery');
    wp_enqueue_script('your_script', "path/to/your/script.js" , array('jquery'));
?>

The third argument to wp_enqueue_script() tells WordPress that your_script is dependent on jquery, so load it only after jquery has loaded.

查看更多
登录 后发表回答