Problem with including jQuery
-
Hi.
How should I go about including jQuery in 3.3.1? Every way I can think of either create problems with the admin-bar (hiding it) or it won’t work.
I have Googled for other ways, but it doesn’t do anything good.
-
jQuery is already available in WP 3.3.1. Perhaps you need to review wp_enqueue_script?
I must have missed something, because that didn’t help.
I’m sorry but all of the information that you need is on that page.
I guess me being a newbie at this doesnt help, but, more right to the point.
How to write jQuery in a template-file? What I got from that is to add the script under “Load a default WordPress script from a non-default location”. :S
Is the script you want to load within your theme or located elsewhere?
The jQuery-script I have written(?) is in my sidebar.php theme-file. The jQuery source-file is on the Google API-site.
I’m sorry if I dont understand everything, being bad at English doesnt help my situation. 🙂
Take your script out of your sidebar file, save it as a standalone script and then enqueue it – eg:
<?php function my_scripts_method() { wp_deregister_script( 'jquery' ); wp_register_script( 'google-jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js'); wp_enqueue_script( 'google-jquery' ); wp_register_script( 'my-jquery', get_template_directory_uri() . '/my-script.js', 'google-jquery'); wp_enqueue_script( 'my-jquery' ); } add_action('wp_enqueue_scripts', 'my_scripts_method'); ?>Okey, so I did what you suggested.
I took my script out of sidebar.php and into a new file, myjquery.js inside the js-directory.
Then, I posted this in my functions.php-file:
function my_scripts_method() { wp_deregister_script( 'jquery' ); wp_register_script( 'google-jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js'); wp_enqueue_script( 'google-jquery' ); wp_register_script( 'my-jquery', get_template_directory_uri() . 'js/myjquery.js', 'google-jquery'); wp_enqueue_script( 'my-jquery' ); } add_action('wp_enqueue_scripts', 'my_scripts_method');I also tried seperating it into its own PHP-tag. It didn’t work unfortunaly.
That was just an example! you have to substitute your own script location and the correct Google jQuery call.
I did, didnt I? 🙂
I can try with the 1.7.1-link.
I think you need a slash before js/myjquery.js
ie:
function my_scripts_method() { wp_deregister_script( 'jquery' ); wp_register_script( 'google-jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js'); wp_enqueue_script( 'google-jquery' ); wp_register_script( 'my-jquery', get_template_directory_uri() . '/js/myjquery.js', 'google-jquery'); wp_enqueue_script( 'my-jquery' ); } add_action('wp_enqueue_scripts', 'my_scripts_method');Tried it, tried full URL, doesn’t work. And it still creates a problem with the admin-bar.
Can you post your sites URL so I can take a quick look
Go ahead: http://goo.gl/Cpq2E 🙂
But I did some troubleshooting myself, and there is two main problems. It cant find the jquery file and it looks for a text/html-file, not javascript.
I was able to fix it. 🙂
In the end it was just sloppy mistakes ruining it for me.
The topic ‘Problem with including jQuery’ is closed to new replies.