1

In addition to one other script, I am tying to enqueue the waypoints script in my functions.php file for Wordpress.

I can't work out why the first script works but the second (waypoints) script will not load?

I am not sure what the true and false relate to and if this is the cause.

I have tried a few variations from different websites, including the following:

function my_custom_scripts() {
    wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/js/custom.js', false );
    }
add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );



function enqueue_waypoints() {
    wp_enqueue_script( 'waypoints', get_stylesheet_directory_uri() . '/js/jquery.waypoints.min.js', array('jquery'), true);
}
add_action( 'wp_enqueue_scripts', 'enqueue_waypoints' );

I also tried:

function my_custom_scripts() {

wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/js/custom.js', array( 'jquery' ),'',true );

wp_enqueue_script( 'waypoints', get_stylesheet_directory_uri() . '/js/jquery.waypoints.min.js', array('jquery'), true);}


add_action( 'wp_enqueue_scripts', 'my_custom_scripts' );

There are no console errors.

What am I doing wrong?

Thanks.

Edit ** - I have just realised that while the first script loads, the addition of the second script breaks the js code in the first script.

Mr Toad
  • 202
  • 2
  • 12
  • 41

2 Answers2

0

I'll try to answer all your questions. For reference from the WP codex: https://developer.wordpress.org/reference/functions/wp_enqueue_script/

True and false in this case refers to loading the scripts inside wp_footer() or not. If you say "true" it will load in the footer vs inside wp_head();

Since you are loading in the footer I'd first check to make sure that the scripts are loading after jQuery, although it looks like you've included them as part of the jQuery array, so you should be good there.

Is the waypoints script inside the same folder as your custom.js?

It's possible that this answer here may help you with regard to Waypoints. WordPress uses jQuery in noconflict mode which could be causing an issue.

What is the error that you are seeing when the second script breaks your first?

elke_wtf
  • 887
  • 1
  • 14
  • 30
  • Hi there - thanks you for your time and explanation. JQuery appears to load in the header, but Waypoints does not load at all. It in in the same folder as custom.js which loads fine, which is why I don't understand. Strangely, the error has disappeared today and the first JS file seems to be loading correctly but the second still isn't. There is no console error. – Mr Toad Aug 13 '19 at 09:19
  • Take it out of the jQuery array and see if it loads. The way you are enqueuing it is tacking it inside the jQuery call. Alternatively you can check the sources tab in Chrome Inspector to see if it's there. – elke_wtf Aug 13 '19 at 13:18
0

I got stuck into the same issue for several hours today. I enqueue many scripts in my project and did the same way also the waypoints.

For some reason, the script was not enquiring at all. I tried many versions, and approaches, and after many hours I found why. It's funny :)

When you enquiring the waypoints script, you must use different unique name than 'waypoints'. Wordpress creates id from the first parameter and that's the point.

Example:

wp_enqueue_script( 'waypoints', get_stylesheet_directory_uri() . '/scripts/vendor/jquery.waypoints.min.js', array ('jquery'), true ); - this ISN'T working

wp_enqueue_script( 'waypoints-script', get_stylesheet_directory_uri() . '/scripts/vendor/jquery.waypoints.min.js', array ('jquery'), true ); - this IS working

Dejw
  • 1
  • 1