0

Is there another way than loading jquery through the link:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>

I tried this:

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

<?php wp_head(); ?>

But when I use wp_enqueue_script("jquery") the local wordpress homepage (with xampp) returns the value NULL

4
  • Do you have any other plugin which deque the jquery? Or ay caching plugin? Commented Feb 26, 2018 at 18:02
  • Yes got three other Plugins where wp_enqueue_script('jquery') is part of the code. So I use it the forth time for my new plugin. What can I do?
    – user136032
    Commented Feb 26, 2018 at 18:21
  • It does not matter how much time the script enqueue. WordPress load it only once. Try by deactivating all of other plugins temporary. Commented Feb 26, 2018 at 18:27
  • your question is not very clear. wordpress do not load jquery from any CDN, and I have no idea what NULL are you talking about Commented Feb 27, 2018 at 6:17

1 Answer 1

0

Although it seems like you should be able to just call the function and have it load the script, it's slightly more complicated. Instead, scripts need to be registered and enqueued inside a hook, so they're loaded in the right order.

To get jQuery loaded on the front end of your theme wrap the function call inside a callback for the wp_enqueue_scripts action. One way is to add this to your theme's functions.php file:

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

See https://developer.wordpress.org/reference/functions/wp_enqueue_script/#notes for more info.

4
  • technically this isn't correct. If you do not need the JS in the header you can enqueue any time before the relevant footer hook is called. Commented Feb 27, 2018 at 6:19
  • Thanks guys! I deactivated all plugins and tried the add action hook, too. It was really painfully overall and didn't work unfortunately. So I just saved the newest jquery 3.3.1 locally and changed <script src="cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"> </script> through <script src="<?php echo get_stylesheet_directory_uri().'/js/jstree/dist/jquery.js';?>"></script> which is my own copy of jquery 3.3.1. Can I just do that in my own template/plugin without worrying that I get in troubles with the already registered version in wordpress(V.1.12.4) on the other pages?
    – user136032
    Commented Feb 27, 2018 at 11:53
  • 1
    You may run into issues with the different versions, but there's no guarantee. To be safe, run your jQuery-based scripts in noconflict mode. api.jquery.com/jQuery.noConflict Commented Feb 27, 2018 at 17:21
  • @MarkKaplun can and should are two different things here. Yes, you can run the function above the call to wp_head(); no, you shouldn't. Current best practice is to enqueue on a hook. Commented Feb 27, 2018 at 17:29

Your Answer

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