$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'); ?>Adding the jQuery to my theme that already exists in Wordpress?|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)

Adding the jQuery to my theme that already exists in Wordpress?

matteradmin9PV0评论

I'm working on a custom theme in which it uses a custom script loaded like so in the functions.php file:

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

add_action('wp_enqueue_scripts', 'custom_scripts_method');

I need jQuery to get it to run properly, I tried to enqueue the already existing jQuery with:

function theme_scripts() {
  wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'theme_scripts');

As answered in this question here: Is jQuery included in WordPress by default?

However, this does nothing. So I added my own jQuery like so:

function jquery_scripts_method()
{
        wp_register_script('jquery_script', get_template_directory_uri() . '/js/jquery.min.js', array('jquery'), '3.3.1', true);
        wp_enqueue_script('jquery_script');
}

add_action('wp_enqueue_scripts', 'jquery_scripts_method');

That worked, but then it also loaded the already existing jQuery along with it. So now there are two files being called, version 1.12 and 3.3.

How do I just get the one that is already there to load?

I'm working on a custom theme in which it uses a custom script loaded like so in the functions.php file:

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

add_action('wp_enqueue_scripts', 'custom_scripts_method');

I need jQuery to get it to run properly, I tried to enqueue the already existing jQuery with:

function theme_scripts() {
  wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'theme_scripts');

As answered in this question here: Is jQuery included in WordPress by default?

However, this does nothing. So I added my own jQuery like so:

function jquery_scripts_method()
{
        wp_register_script('jquery_script', get_template_directory_uri() . '/js/jquery.min.js', array('jquery'), '3.3.1', true);
        wp_enqueue_script('jquery_script');
}

add_action('wp_enqueue_scripts', 'jquery_scripts_method');

That worked, but then it also loaded the already existing jQuery along with it. So now there are two files being called, version 1.12 and 3.3.

How do I just get the one that is already there to load?

Share Improve this question asked Jan 18, 2019 at 13:23 XarcellXarcell 1035 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

The first block of code, from your question, is all you need. 'jquery' is specified in wp_register_script function call as dependency. When you enqueue your 'custom_script', 'jquery' will be enqueued also.

Post a comment

comment list (0)

  1. No comments so far