Support

Account

Home Forums General Issues Youtube embed deffer loading Reply To: Youtube embed deffer loading

  • Commenting to remind others like myself: don’t forget to set ‘jquery’ as the dependency when enqueueing John’s script. Otherwise, you might receive “Uncaught TypeError: url.indexOf is not a function”.

    
    add_action( 'wp_enqueue_scripts', function() {
        wp_enqueue_script('load-deferred-videos-js', get_template_directory_uri() . '/static/js/load-deferred-videos.js', ['jquery']);
    });
    

    Notice ['jquery'] as the third argument to wp_enqueue_script.

    P.S.
    If you only want to load this script on a custom post type, such as “car”, then wrap the wp_enqueue_script function inside an if-clause.

    
    if (is_singular('car')) {
            wp_enqueue_script('load-deferred-videos-js', get_template_directory_uri() . '/static/js/load-deferred-videos.js', ['jquery']);
    }