0

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.

4
  • Why don't put it in header.php? Is it essential to have it in functions.php?
    – Alexey
    Nov 24, 2011 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, 2011 at 1:26
  • 3
    You're correct in using the functions.php file. You shouldn't link to js in your header like suggested by Alexey. Nov 24, 2011 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, 2011 at 13:11

1 Answer 1

2

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?

3
  • Thanks Jeremy, tested but not worked.
    – aurrutia
    Nov 24, 2011 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. Nov 24, 2011 at 3:19
  • @aurrutia Then mark it as The Answer please.
    – fuxia
    Feb 6, 2012 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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