Why doesnโ€™t the script fit in Wordpress?

I have a childtheme Wordpress using a .php function that contains the following:

<?php add_action( 'wp_enqueue_scripts', 'load_custom_script' ); function load_custom_script() { wp_register_script( 'custom-script', get_template_directory_uri() . '/js/wpw-custom.js', array('jquery'), '1.0', true ); wp_enqueue_script( 'custom-script' ); } ?> 

This successfully loads my custom script, in the footer, AFTER jQuery loads. The user script contains the following test code to tell me if the queue process is running:

 $( document ).ready(function() { alert( "ready!" ); }); 

I can view the source on my pages and check that my wpw-custom.js has loaded successfully, but it does not fulfill the warning!

What have I done wrong and what steps can I take to methodically find out what I did wrong? Thanks so much for any help!

0
source share
1 answer

jQuery in WordPress works in noConflict mode, which means that the global $ shortcut for jQuery is not available. Replace the code as follows:

 jQuery(document).ready(function($) { alert( 'ready!' ); }); 

Inside the finished packaging of a document, $ can be used as an alias for jQuery.

+4
source

Source: https://habr.com/ru/post/979558/


All Articles