Using Astra theme, I cannot use jQuery on the frontend. It keeps logging Uncaught ReferenceError: jQuery is not defined at VM105:23
. It is actually supposed that Wordpress is equipped with jQuery, but I cannot figure out why I get that 'undefined' error message in console.
So as to add jQuery, I just write the following code, yet to no effect:
function addjq () {
wp_enqueue_script('jquery', plugin_dir_url(__FILE__) . '/in/jq.js', array('jquery'), null, false);
}
add_action('wp_enqueue_scripts', 'addjq');
I am using this piece inside a widget, and it is not including jquery
to the frontend. It actually comes up with that Uncaught ReferenceError
. Is the problem somehow related to my jquery
file url, or is it a core problem that I need to address. Can you help me out of the problem, and let me know how I can use jquery
inside a plugin widget
. The following jQuery code throws that console error:
jQuery(document).ready(function() {
// Some Code Goes Here...
})
PS: I also tried to add a src
attribute to the script tag, and bring in the jQuery. RESULT
: The error disappeared, yet no jQuery code was run. Even I could not do a simple console.log()
.
Thanks in advance...
Using Astra theme, I cannot use jQuery on the frontend. It keeps logging Uncaught ReferenceError: jQuery is not defined at VM105:23
. It is actually supposed that Wordpress is equipped with jQuery, but I cannot figure out why I get that 'undefined' error message in console.
So as to add jQuery, I just write the following code, yet to no effect:
function addjq () {
wp_enqueue_script('jquery', plugin_dir_url(__FILE__) . '/in/jq.js', array('jquery'), null, false);
}
add_action('wp_enqueue_scripts', 'addjq');
I am using this piece inside a widget, and it is not including jquery
to the frontend. It actually comes up with that Uncaught ReferenceError
. Is the problem somehow related to my jquery
file url, or is it a core problem that I need to address. Can you help me out of the problem, and let me know how I can use jquery
inside a plugin widget
. The following jQuery code throws that console error:
jQuery(document).ready(function() {
// Some Code Goes Here...
})
PS: I also tried to add a src
attribute to the script tag, and bring in the jQuery. RESULT
: The error disappeared, yet no jQuery code was run. Even I could not do a simple console.log()
.
Thanks in advance...
Share Improve this question edited Jan 9, 2021 at 20:43 H. M.. asked Jan 9, 2021 at 20:18 H. M..H. M.. 1135 bronze badges2 Answers
Reset to default 1Special thanks are offered to @Pat J. The problem was actually with the jQuery code I had written.
jQuery(document).ready(function() {
// Some Code Goes Here...
})
Just take a look at the complete segment:
public function doSomethingSpecific() {
?>
<script type="text/javascript" >
jQuery(document).ready(function() {
console.log('Hello World!');
})
}
</script>
<?php
}
Since the above function was not correctly enqueued, and actually supposedly kept running before jQuery was loaded, I received that Uncaught ReferenceError: jQuery is not defined at VM105:23
error. So what I did to solve the problem was to wait for the page to load fully and then call the code. Therefore I changed that jQuery
segment shown above to the following and it was good to go. Problem Solved, and definitely there was no need to enqueue 'jQuery'
.
public function webcamcapchur() {
?>
<script type="text/javascript" >
window.onload = function doLoadThePage(event) {
jQuery(document).ready(function() {
console.log('Hello World!');
})
}
</script>
<?php
}
'jquery'
is already a registered script in WordPress. Your attempt to enqueue it is failing because WordPress thinks you're trying to register jquery
with a new URL, which it ignores. See the notes on wp_enqueue_script()
.
To load WordPress's existing jQuery library, do this:
function wpse_381226_load_jquery () {
wp_enqueue_script( 'jquery');
}
add_action( 'wp_enqueue_scripts', 'wpse_381226_load_jquery' );