Nicholas Ritson Nicholas Ritson - 6 months ago 19
Javascript Question

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' );

Answer

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');
Comments