Issue with a variable causing a reference error in Wordpress JavaScript

As someone who is relatively new to Wordpress theming, I've come across a common issue with Jquery $ code and how it can be misunderstood by Wordpress. It's recommended to use jQuery(document).ready(function($) {} instead of just plain $(.) code to avoid conflicts. *Note: All JS files have been properly registered and enqueued, appearing in the page source.

The problem I'm facing involves a custom.js file containing custom JS and Jquery code for my theme. While it works fine without Wordpress, it breaks when integrated with Wordpress.

Below is the code snippet:

//sticky header on scroll
jQuery(window).load(function() {
jQuery(".sticky").sticky({topSpacing: 0});
});

//parallax
jQuery(window).stellar({
    horizontalScrolling: false,
    responsive: true/*,
     scrollProperty: 'scroll',
     parallaxElements: false,
     horizontalScrolling: false,
     horizontalOffset: 0,
     verticalOffset: 0*/
});

/*====flex  slider for main slider on header2====*/
jQuery(document).ready(function() {
jQuery('.main-slider').flexslider({
    slideshowSpeed: 5000,
    directionNav: false,
    controlNav: true,
    animation: "fade"
})
});

//owl carousel for work
jQuery(document).ready(function() {
    jQuery("#work-carousel").owlCarousel({
        // Most important owl features
        items: 4,
        itemsCustom: false,
        itemsDesktop: [1199, 4],
        itemsDesktopSmall: [980, 3],
        itemsTablet: [768, 3],
        itemsTabletSmall: false,
        itemsMobile: [479, 1],
        singleItem: false,
        startDragging: true
    });

});

//owl carousel for testimonials
jQuery(document).ready(function() {

 jQuery("#testi-carousel").owlCarousel({
        // Most important owl features
        items: 1,
        itemsCustom: false,
        itemsDesktop: [1199, 1],
        itemsDesktopSmall: [980, 1],
        itemsTablet: [768, 1],
        itemsTabletSmall: false,
        itemsMobile: [479, 1],
        singleItem: false,
        startDragging: true
    });

});
//owl carousel for full width image slider
jQuery(document).ready(function() {

    jQuery("#full-img-slide").owlCarousel({
        // Most important owl features
        items: 1,
        itemsCustom: false,
        itemsDesktop: [1199, 1],
        itemsDesktopSmall: [980, 1],
        itemsTablet: [768, 1],
        itemsTabletSmall: false,
        itemsMobile: [479, 1],
        singleItem: false,
        startDragging: true
    });

});

/* ==============================================
 Counter Up
 =============================================== */
jQuery(document).ready(function($) {
    jQuery('.counter').counterUp({
        delay: 100,
        time: 800
    });
});


/* ==============================================
 WOW plugin triggers animate.css on scroll
 =============================================== */

var wow = new WOW(
        {
            boxClass: 'wow', // animated element css class (default is wow)
            animateClass: 'animated', // animation css class (default is animated)
            offset: 100, // distance to the element when triggering the animation (default is 0)
            mobile: false        // trigger animations on mobile devices (true is default)
        }
);
wow.init();

//portfolio mix
jQuery('#grid').mixitup();


/*========tooltip and popovers====*/
/*==========================*/
jQuery("[data-toggle=popover]").popover();

jQuery("[data-toggle=tooltip]").tooltip();

An error appears in the console stating Uncaught ReferenceError: WOW is not defined for the following lines:

var wow = new WOW(
        {
            boxClass: 'wow', // animated element css class (default is wow)
            animateClass: 'animated', // animation css class (default is animated)
            offset: 100, // distance to the element when triggering the animation (default is 0)
            mobile: false        // trigger animations on mobile devices (true is default)
        }
);
wow.init();

It seems that WOW is being recognized as an undefined variable. The reference to WOW comes from the wow.js library (https://github.com/matthieua/WOW) and is used to call it on the homepage. Any assistance in resolving this would be greatly appreciated!

Answer №1

Finally, the solution to this issue has been found! It's amazing how well it worked for me!

 jQuery( document ).ready(function() {
    wow = new WOW(
        {
            boxClass:     'wow',      
            animateClass: 'animated', 
            offset:       100,         
            mobile:       false,     
            live:         true       
        }
    )
    wow.init();
});

Quick reminder: Make sure to nest it inside the document ready function.

Answer №2

Did you remember to use wp_enqueue_script() or wp_register_script() in your functions.php file in WordPress for wow.js?

The WordPress Codex recommends using this method to link JavaScript to a WordPress generated page.

Take a look at the official WordPress documentation. Alternatively, you can also read wpbeginner.com's insights on this topic ;)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for syncing input field values with mui slider?

Implementing a MUI slider that ranges from a minimum of 8 to a maximum of 255. <Slider value={range} min={minVal} max={maxVal} onChange={handleSliderChange} valueLabelDisplay="auto" /> In addition, displaying input fields to show ...

Using React to dynamically assign a backgroundImage based on a JSON response

I am having an issue with retrieving data from my Wordpress API and displaying it in my react app. Specifically, I am struggling to set the post's featured image as a background-image for an element. Here is an example of the JSON response: { "id" ...

What is the best way to eliminate specific duplicate characters from a string using JavaScript?

I have a project involving managing email responses, where the reply function includes pre-written content like Re: ${Subject of the email} The issue I'm facing is that after the 2nd reply, there is a repeated Re: , so I created a function to remove ...

Conceptualization of a Strategy Game Server

Recently, I've been brainstorming the idea of developing a WebGL-based real-time strategy game that allows multiple players to join and play together. My plan is to utilize Node.js for creating the game server, along with websockets to establish real- ...

Is there a way retrieve all named HTML tags and store them in an array?

In need of assistance with parsing data from a page that is formatted as follows: <tag>dataIwant</tag> <tag>dataIwant</tag> <tag>dataIwant</tag> <othertag>moarData</othertag> <othertag>moarData</oth ...

The issue I am encountering is that the value from jQuery autocomplete is not getting transferred to the

I'm having trouble retrieving a textInput from a Form where I am extracting values from Jquery Autocomplete. The selected value is not being transferred to the form. Can you please help me identify what I am missing? $(function() { var availableT ...

Separate the object/variable from the asynchronous function

Having trouble exporting my JSON object variable "X" out of a loop in my code. Despite trying different methods, I keep getting a "promise.pending" instead of the actual object. Any help from someone experienced would be greatly appreciated. Here's th ...

JavaScript string representing the actual value of a string

In order to reliably extract string literals from a JavaScript string, I need to create a function, let's name it f. Here are some examples: f('hello world') //-> 'hello world' (or "hello world") f('hello "world"') / ...

Generating a string indicating the range of days chosen

Imagine a scenario where there is a selection of days available to the user (they can choose multiple). The list includes Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday, each with an associated number from 0 to 6. For instance, Sunday ...

What steps can I take to guarantee that a directive's link function is executed prior to a controller?

Our application features a view that is loaded through a basic route setup. $routeProvider .when('/', { template: require('./views/main.tpl.html'), controller: 'mainCtrl' }) .otherwise({ re ...

Displaying content in Bootstrap 5 modal with jQuery may cause a delay in the appearance of a centered spinner before the content loads

In my Bootstrap 5 modal, I have implemented specific functionality in jQuery where a centered spinner is supposed to show up in the modal before loading the content. You can see a live example of this here. The issue I am facing is that the spinner takes ...

Double-clicking on a row in Ant Design table allows for specific actions to be

I am looking to implement a modal window that opens when I double click on a row in an Antd Table, and then pass a property to the modal. Here is what I have: <Table columns={columns} components={components} dataSource={dataSource} onRow ...

What is the best way to convert the response from an XMLHttpRequest to a JavaScript array and display it

Looking to transform the data from xmlhttp.responseText into a JavaScript array I have this {"name":"Eswara Manikanta Varma","email":"contact@email.com","mobile":"9966578911"} retrieved from xmlhttp.responseText, and now I need to convert it into a JavaSc ...

Consistent height for h2 containers across all CSS grid columns

Is there a way to ensure all h2 containers have the same height, both with JavaScript and without it? The h2 titles are dynamic and can vary in length, but I want to maximize the space for all containers. img { max-width: 100%; height: auto; } .gri ...

Guide on displaying a dropdown in AngularJS when the select option is already applied but not showing

I am currently working on creating a select option using JSON data, but for some reason it is not displaying. I have used the select tag and placed the option tags inside it. <div ng-switch-when="SWITCH"> <select name="selectedFacilit ...

Using an Ajax XML object to dynamically set images in a DataList

When making an Ajax call to retrieve an XML response and trying to set image names from the XML on a datalist, I encountered some issues. The code snippet I used for setting the image is as follows: $(".Image", tablex).attr('src', product.find( ...

Node.js is not accurately setting the content length. How can I resolve this issue?

I could use some assistance. Even though I have set the content-length on the response object, it doesn't seem to be working. Have I done something incorrectly? res.set({ 'Content-Type': res._data.ContentType, 'Content-Length' ...

Determining the number of items in an array using Javascript

I am working with an array that is made up of an unlimited number of objects, all with the same structure. When I log the entire array, it shows up like this: [object], [object], [object], and so on... Adding a new object to the array means I could eithe ...

The database threw an error: "Duplicate key found in collection causing MongoError: E11000"

I'm currently in the process of updating an object within an array found in a document "state": [], "users": [{ "ready": false, "_id": { "$oid": "5fb810c63af8b3 ...

Is it feasible for Javascript to trigger a callback or signal with Selenium?

Imagine using Selenium on a web page that utilizes an AJAX request to dynamically update content within a specific #content element. The #content element always has exactly five children, each without any classes, ids, or unique identifiers. Periodically, ...