Nicholas Ritson
Nicholas Ritson

Reputation: 909

load javascript in Wordpress

I've been trying to load a javascript file and its not loading, looked up the docs but cant seem to figure out why it wont enqueue. Site is here : site

In the console im getting an error message for cookies, i have also disabled all plugins, recently switching the visual composer one back on but seems like jquery is not working even though i can load it fine.

Also im queueing like this right now:

function edealsdirect_scripts() {

  wp_register_script( 'jquery-cookie', get_template_directory_uri() . '/vendor/js/js.cookie.js', array( 'jquery', '', false ) );
  wp_enqueue_script( 'jquery-cookie' );

}

add_action( 'wp_enqueue_scripts', 'edealsdirect_scripts' );

Upvotes: 1

Views: 80

Answers (3)

Bojan Petkovski
Bojan Petkovski

Reputation: 6933

Try this one:

function edealsdirect_scripts() {
  wp_enqueue_script( 'jquery-cookie', get_template_directory_uri() . '/vendor/js/js.cookie.js', array( 'jquery' ));
}

add_action( 'wp_enqueue_scripts', 'edealsdirect_scripts' );

Currently on your site you are not loading the script file. I have checked if that is the correct path to the cookie script and it is, http://edeals.nickritson.co.uk/wp-content/themes/dragoncove-saltrock/vendor/js/js.cookie.js.

Upvotes: 0

Jamie Livingstone
Jamie Livingstone

Reputation: 242

You are registering a script and then enqueuing is as a style-sheet.

Try this (Left a snippet of how to add CSS so you can use it in future and can see the difference between them):

<?php
function mytheme_custom_scripts(){
    // Register and Enqueue a Stylesheet
   // wp_register_style( 'name-of-style', get_template_directory_uri() . '/css/custom-style.css');
    wp_enqueue_style( 'name-of-style' );

    // Register and Enqueue a Script
    // get_stylesheet_directory_uri will look up child theme location
    wp_register_script( 'jquery-cookie', get_stylesheet_directory_uri() . '/vendor/js/js.cookie.js', array('jquery'));
    wp_enqueue_script( 'jquery-cookie' );

}

add_action('wp_enqueue_scripts', 'mytheme_custom_scripts');

Upvotes: 1

user6086593
user6086593

Reputation:

You are queueing style instead of script. Change wp_enqueue_style to wp_enqueue_script

Also, jquery-cookie has to be loaded after jquery. You have missing $deps argument in function call. See documentation

wp_register_script( 'jquery-cookie', get_template_directory_uri() . '/vendor/js/js.cookie.js', array( 'jquery' ) );
wp_enqueue_script( 'jquery-cookie' );

Upvotes: 1

Related Questions