0

I am enqueuing javascript files with functions.php, one of the is a external link and another is not.

They are both loading all right, but it's loading the internal file before the external, so there're some bits of the code that are not working.

This is the code:

function _tandt_scripts() {
  $my_js_ver  = date("ymd-Gis");

  wp_deregister_script( 'jquery' );
  wp_register_script('jquery', 'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js', array(), '2.1.3', true);

  wp_enqueue_script( '_tandt-main', get_template_directory_uri() . '/_assets/js/main/min/main-min.js', array(), $my_js_ver, true );

  if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
      wp_enqueue_script( 'comment-reply' );
  }
}

add_action( 'wp_enqueue_scripts', '_tandt_scripts' );

As you can see in the screenshot, it's loading the 'main-min.js' first, when it should be the other way around.

screenshot

enter image description here

2
  • because you didn't enqueue jquery, you just registered it. Commented May 3, 2018 at 14:48
  • Wrote it as an answer. You can accept that. Commented May 3, 2018 at 14:51

2 Answers 2

1

You re-registered the jQuery script but you didn't enqueue it to the page.

Add this line after wp_register_script:

wp_enqueue_script("jquery");

and change this line:

wp_enqueue_script( '_tandt-main', get_template_directory_uri() .
    '/_assets/js/main/min/main-min.js', array(), $my_js_ver, true );

to:

wp_enqueue_script( '_tandt-main', get_template_directory_uri() . 
    '/_assets/js/main/min/main-min.js', array("jquery"), $my_js_ver, true );

to have jquery as a dependency.

Sign up to request clarification or add additional context in comments.

Comments

0

You haven't specified that the second script is dependent on the first.

wp_enqueue_script( '_tandt-main',
    get_template_directory_uri() . '/_assets/js/main/min/main-min.js',
    array(), $my_js_ver, true );

This line should be changed to include the jquery dependemcy.

wp_enqueue_script( '_tandt-main',
    get_template_directory_uri() . '/_assets/js/main/min/main-min.js',
    array('jquery'), $my_js_ver, true );

See the WordPress documentation for further details: wp_enqueue_script()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.