Capitan Duke Capitan Duke - 1 year ago 190
jQuery Question

Uncaught TypeError: $ is not a function wordpress

There are some questions about this error, but I couldn't find any solutions on them. I'm developing a wordpress theme and during the enqueue script this error came up:

Uncaught TypeError: $ is not a function
at components.js?ver=4.8:65
at components.js?ver=4.8:95


here is my enqueue code add to the functions.php:

function goc_scripts() {
wp_enqueue_style( 'goc-style', get_stylesheet_uri() );

wp_enqueue_style( 'socicon', get_stylesheet_directory_uri() . '/css/socicon.css', array() );

wp_enqueue_style( 'bootstrap-social', get_stylesheet_directory_uri() . '/css/bootstrap-social.css', array() );

wp_enqueue_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.min.css');

wp_enqueue_style('font-awesome', get_template_directory_uri() . '/css/font-awesome.min.css');

wp_enqueue_style('simple-line-icons', get_template_directory_uri() . '/css/simple-line-icons.min.css');

wp_enqueue_style('animate', get_template_directory_uri() . '/css/animate.min.css');

wp_enqueue_style('cubeportfolio', get_template_directory_uri() . '/css/cubeportfolio.min.css');

wp_enqueue_style( 'owlcarousel-style', get_template_directory_uri() . '/css/owl.carousel.css' );

wp_enqueue_style( 'fancybox', get_template_directory_uri() . '/css/jquery.fancybox.css' );

wp_enqueue_style( 'goc-slider', get_template_directory_uri() . '/css/slider.css' );

wp_enqueue_style( 'goc-plugins', get_template_directory_uri() . '/css/plugins.css' );

wp_enqueue_style( 'goc-red3', get_template_directory_uri() . '/css/themes/red3.css' );

wp_enqueue_style( 'goc-custom', get_template_directory_uri() . '/css/custom.css' );

wp_enqueue_script( 'goc-jquery-min', get_template_directory_uri() . '/js/jquery.min.js', array(), false, true );

wp_enqueue_script( 'goc-jquery-migrate', get_template_directory_uri() . '/js/jquery-migrate.min.js', array(), false, true );

wp_enqueue_script( 'goc-jquery-easing', get_template_directory_uri() . '/js/jquery.easing.min.js', array(), false, true );

wp_enqueue_script( 'goc-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20151215', true );

wp_enqueue_script( 'goc-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true );

wp_enqueue_script( 'bootstrap-js', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', array(jquery), true); // prueba

wp_enqueue_script( 'goc-reveal-animate', get_template_directory_uri() . '/js/reveal-animate.js', array(), '4.8', true );

wp_enqueue_script( 'goc-reveal-animate-wow', get_template_directory_uri() . '/js/reveal-animate/wow.js', array(), false, true );

wp_enqueue_script( 'goc-cubeportfolio', get_template_directory_uri() . '/js/cubeportfolio/js/jquery.cubeportfolio.min.js', array(), false, true );

wp_enqueue_script( 'goc-owl-carousel', get_template_directory_uri() . '/js/owl-carousel/owl.carousel.min.js', array(), false, true );

wp_enqueue_script( 'goc-counterup', get_template_directory_uri() . '/js/counterup/jquery.counterup.min.js', array(), false, true );

wp_enqueue_script( 'goc-waypoints', get_template_directory_uri() . '/js/counterup/jquery.waypoints.min.js', array(), false, true );

wp_enqueue_script( 'goc-fancybox', get_template_directory_uri() . '/js/fancybox/jquery.fancybox.pack.js', array(), false, true );

wp_enqueue_script( 'goc-jquery.smooth-scroll', get_template_directory_uri() . '/js/smooth-scroll/jquery.smooth-scroll.js', array(), false, true );

wp_enqueue_script( 'goc-slider', get_template_directory_uri() . '/js/slider-for-bootstrap/js/bootstrap-slider.js', array(jquery), false, true );

wp_enqueue_script( 'goc-component', get_template_directory_uri() . '/js/components.js', array(jquery), '4.8', false );

wp_enqueue_script( 'goc-components', get_template_directory_uri() . '/js/components-shop.js', array(), '4.8', true );

wp_enqueue_script( 'goc-appjs', get_template_directory_uri() . '/js/app.js', array(), false, true );

if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'goc_scripts' );


and here is the code components.js where the error happens (line 65 and line 95):

var LayoutHeader = function () {
var offset = parseInt($('.c-layout-header').attr('data-minimize-offset') > 0 ? parseInt($('.c-layout-header').attr('data-minimize-offset')) : 0);
var _handleHeaderOnScroll = function () {
if ($(window).scrollTop() > offset) {
$("body").addClass("c-page-on-scroll");
} else {
$("body").removeClass("c-page-on-scroll");
}
}

var _handleTopbarCollapse = function () {
$('.c-layout-header .c-topbar-toggler').on('click', function (e) {
$('.c-layout-header-topbar-collapse').toggleClass("c-topbar-expanded");
});
}

return {
//main function to initiate the module
init: function () {
if ($('body').hasClass('c-layout-header-fixed-non-minimized')) {
return;
}

_handleHeaderOnScroll();
_handleTopbarCollapse();

$(window).scroll(function () {
_handleHeaderOnScroll();
});
}
};
}();


Hope someone can help :).

Kind regards,

Answer Source

There are two solution for this:

#1

Repalce all $ to jQuery in your code.

#2

Or a Simple way.

just assign jQuery to $ as a global variable like following on the top of your .js file and all your code will work fine.

var $ = jQuery;

if it works don't forget to mark as answer so others can follow it to.ty

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download