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 );   
  }
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' );
  • 1
    Thanks a lot Sam but can you also let me know what the is_singular() is doing here? – Behseini May 19 '16 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. – Samuel Elh May 19 '16 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 – Behseini May 19 '16 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! – Behseini May 19 '16 at 17:42
  • Did you try is_front_page() ? – Samuel Elh May 19 '16 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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