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.
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.
Or not.
The wp_enqueue_scripts didnt fix the problem with the admin-bar.
he wp_enqueue_scripts didnt fix the problem with the admin-bar.
Yeah. If you're dequeueing the version of jQuery bundled with WordPress - i.e. the version of jQuery against which the Admin Bar and other core code has been tested - and enqueueing your own version of jQuery, you assume some degree of risk of breaking things - things such as the Admin Toolbar.
Back at zero then.
Any suggestions on how I should add my own script without breaking anything then?
Any suggestions on how I should add my own script without breaking anything then?
Is there any reason that you can't just use the core-bundled version of jQuery?
No, not really. But how would I go about writing jQuery using that? Just throwing a script in there doesnt work so thats kind of where Im lost. :P
In the last version of WordPress I used, I could just link to the jQuery-file in the header and write my script, which in 3.3.1 causes problems.
No, not really. But how would I go about writing jQuery using that? Just throwing a script in there doesnt work so thats kind of where Im lost. :P
Are you trying to enqueue a jQuery library, or are you trying to write an inline script using jQuery? If you're trying to write an inline script (e.g. for a slider or something), that should work just fine. Just be aware that WordPress uses no-conflict, so you can't nakedly use $ as a shorthand for jQuery.
In the last version of WordPress I used, I could just link to the jQuery-file in the header and write my script, which in 3.3.1 causes problems.
...in which case, we're right back where @esmi started: wp_enqueue_script().
I'm trying to write a script, but I thought I needed to get the library-file first, because when I just write a script it doesnt work.
<script>
jQuery(document).ready(function(){
jQuery('.single #recent-posts-2').appendTo('.topgrid');
});
</script>
...in which case, we're right back where @esmi started: wp_enqueue_script().
That again causes problems. :S
This topic has been closed to new replies.