Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to load my javascript and exclude 1 page, so I tried the following without success:

function mypage_scripts()

{
if ( !is_admin() )
{ 
    wp_deregister_script('jquery');
    wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js', true, '1.6.2', false);
    wp_enqueue_script('jquery');
/* My Script */ 
if ( !is_page('contact') )
        {       
            wp_enqueue_script('myscript', get_bloginfo('template_url') . '/js/myscript.js', array('jquery'), '1.1', false);
        }
}

}

add_action( 'wp_print_scripts', 'mypage_scripts');

The result is that the script loads on every page except on Admin (as expected) but can't manage to not load on "Contact" page.

Any ideas?

Of course I can list all the pages I want to load it, but as the site is going to have more pages in the future I don't want to change the functions.php file everytime a new one is created.

Thanks.

share|improve this question
    
Why don't put it in header.php? Is it essential to have it in functions.php? –  Alexey Nov 24 '11 at 1:19
    
Not essential in this case in particular, but will be usefull if there's an option to do it in functions.php –  aurrutia Nov 24 '11 at 1:26
2  
You're correct in using the functions.php file. You shouldn't link to js in your header like suggested by Alexey. –  Jeremy Jared Nov 24 '11 at 2:10
    
There's no need for that is_admin check, not when using the wp_print_scripts action(that particular action, if i'm not mistaken, only runs on non-admin pages). –  t31os Nov 24 '11 at 13:11
add comment

1 Answer

up vote 1 down vote accepted

Try giving your function its own parameters and do a template_redirect, something like this:

<?php
  function mypage_scripts() {
    if ( !is_admin() ) {
      wp_deregister_script('jquery');
      wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js', true, '1.6.2', false);
      wp_enqueue_script('jquery');
      /* My Script */ 
  function contact_form_script() {
      if ( !is_page('contact') ) {
        wp_enqueue_script('myscript', get_bloginfo('template_url') . '/js/myscript.js', array('jquery'), '1.1', false);
        }
      }
  add_action( 'template_redirect', 'contact_form_script' );
    }
  add_action( 'wp_print_scripts', 'mypage_scripts');
  }
  ?>

I've not checked this, but I believe it will work.

I Checked this one on on one of my themes and it worked:.

<?php
  function mypage_scripts() {
    if ( !is_admin() ){ 
    wp_deregister_script('jquery');
    wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js', true, '1.6.2', false);
    wp_enqueue_script('jquery');
      }
    add_action( 'wp_print_scripts', 'mypage_scripts');
    }
  function contact_form_script() {
      if ( !is_page('contact') ) {
        wp_enqueue_script('myscript', get_bloginfo('template_url') . '/js/myscript.js', array('jquery'), '1.1', false);
        }
      }
  add_action( 'template_redirect', 'contact_form_script' );
  ?>

NOTE: The one thing I did diferent on my test is I wasn't using the Google scripts. Maybe that is giving you the issue?

share|improve this answer
    
Thanks Jeremy, tested but not worked. –  aurrutia Nov 24 '11 at 2:19
    
I revised my answer after checking the first one and it didn't work for me. The second worked with the noted difference in our code. –  Jeremy Jared Nov 24 '11 at 3:19
    
Thanks again Jeremy, works perfect! –  aurrutia Nov 24 '11 at 5:33
    
@aurrutia Then mark it as The Answer please. –  toscho Feb 6 '12 at 10:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.