1

I am using following code to add some scripts in WP pages like

function add_js() {
  wp_deregister_script('jquery');
  wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js", false, null);
  wp_enqueue_script('jquery');
  wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() .'/js/bootstrap.min.js', array('jquery'),'',true );
  wp_enqueue_script( 'colorbox-js', get_template_directory_uri() .'/js/jquery.colorbox-min.js', array('jquery'),'',true );
  wp_enqueue_script( 'img-loader', get_template_directory_uri() .'/js/img-load.js', array('jquery'),'',true );
  wp_enqueue_script( 'box-tanzim', get_template_directory_uri() .'/js/tanzim.js', array('jquery'),'',true );
  wp_enqueue_script( 'scripts-js', get_template_directory_uri() .'/js/scripts.js', array('jquery'),'',true );

}
add_action( 'wp_enqueue_scripts', 'add_js' );

but I don't want to add unnecessary scripts on some pages! for example I would like to have all of above scripts on front-page.php but on other pages I don't need last three scripts.

Can you please let me know how I can filter them?

Thanks

Update

    $frondPage = get_option('page_on_front');
    if(is_page($frondPage) ) {
    wp_enqueue_script( 'wait.js', get_template_directory_uri() .'/js/wait.js', array('jquery'),'',true );   
  }

1 Answer 1

2

You can prevent adding a script to a specific WordPress page by checking if whether we are on that page or not is_page($page_id):

function add_js() {
  wp_deregister_script('jquery');
  wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js", false, null);
  wp_enqueue_script('jquery');
  wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() .'/js/bootstrap.min.js', array('jquery'),'',true );
  wp_enqueue_script( 'colorbox-js', get_template_directory_uri() .'/js/jquery.colorbox-min.js', array('jquery'),'',true );

  $pages = array(1,2,32); // insert the pages ID where you don't want these scripts enqueued

  if( ! is_page($pages) ) {

    wp_enqueue_script( 'img-loader', get_template_directory_uri() .'/js/img-load.js', array('jquery'),'',true );
    wp_enqueue_script( 'box-tanzim', get_template_directory_uri() .'/js/tanzim.js', array('jquery'),'',true );
    wp_enqueue_script( 'scripts-js', get_template_directory_uri() .'/js/scripts.js', array('jquery'),'',true );

  }

}
add_action( 'wp_enqueue_scripts', 'add_js' );
7
  • 1
    Thanks a lot Sam but can you also let me know what the is_singular() is doing here? Commented May 19, 2016 at 17:02
  • Oh yes thanks totally got confused with is_page_template() which requires that. Drop is_singular() and everything will work fine. Editing the answer for you. Commented May 19, 2016 at 17:34
  • Thanks again, but I am having issue on adding a script only to front page! can you please take a look at the update? As you can see Commented May 19, 2016 at 17:41
  • I tried to get the front-page.php if by using $frondPage = get_option('page_on_front'); but this is noy working! Commented May 19, 2016 at 17:42
  • Did you try is_front_page() ? Commented May 19, 2016 at 17:47

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.