Reputation: 6485
I'm trying to call multiple JQuery scripts using (a wordpress feature) wp_enqueue_script. The call to JQuery works perfectly but the second call to cufon doesn't. I'm not a php or javascript expert - could anyone lend a hand, is there a best practice method for this?
function my_init_method() {
if (!is_admin()) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js');
wp_enqueue_script( 'jquery' );
}
}
add_action('init', 'my_init_method');
function my_init_method2() {
if (!is_admin()) {
wp_deregister_script( 'cufon' );
wp_register_script( 'cufon', 'http://mydomain.com/wp-content/themes/simplefolio/js/cufon-yui.js');
wp_enqueue_script( 'cufon' );
}
}
add_action('init', 'my_init_method2');
Upvotes: 0
Views: 525
Reputation: 26
My choice of way to deregister_script and enqueue_script is as followed (feel free to adjust it to your needs):
function my_deregister_javascript() {
wp_deregister_script ( 'jquery-ui-tabs' );
wp_deregister_script ( 'jquery-ui-core' );
wp_deregister_script ( 'jquery-cycle' );
wp_deregister_script ( 'hoverintent' );
wp_deregister_script ( 'superfish' );
wp_deregister_script ( 'jquery-validate' );
wp_deregister_script ( 'arras_add_header_js' );
wp_deregister_script ( 'arras_add_slideshow_js' );
wp_deregister_script ( 'ratings_scripts' );
wp_deregister_script ( 'wp-postratings' );
wp_deregister_script ( 'sharing-js' );
wp_deregister_script ( 'jquery' );
wp_enqueue_script ('jquery', '/js/mymusicplug.js', '', '1.4.4');
}
if ( !is_admin() ) {
add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );
}
As you see, my 'if ( !is_admin() )' is relative to the scripts 'output'. I also deregistered with no registering - registering the script via wp_register_script causes total meltdown - and then enqueue the script file of which your combined/minified JS is located in. This definitely works for me, with the result being a 143,000+ b JS file. I know, it may seem like a hefty tag on a JS file, but with the simplification of things like combining JS in WP via 'deregister_script', it reduced my page by over 30+ HTTP requests for local and external JS. The heaviest JS on my page now involves analytics in the footer, and AdSense code. And still, I'm down to only around 10-15 requests.
Great Tip: Combine CSS too, 1 CSS file, 1 JS file, your page will fly!
Hope this helped.
Upvotes: 1
Reputation: 39289
I'm not exactly sure what you're looking for here but it looks like you could simplify things by combining your two init statements
function my_init_method() {
if (!is_admin()) {
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js');
wp_enqueue_script( 'jquery' );
wp_deregister_script( 'cufon' );
wp_register_script( 'cufon', 'http://mydomain.com/wp-content/themes/simplefolio/js/cufon-yui.js');
wp_enqueue_script( 'cufon' );
}
}
add_action('init', 'my_init_method');
Also you should ensure that the http://mydomain.com/wp-content/themes/simplefolio/js/cufon-yui.js
file exists and is accessible from your browser.
Upvotes: 0