Support » Themes and Templates » Function to load on homepage only in function.php

  • Hi All,

    is there a way to put conditions around

    function wpsc_deregister_scripts() {
    	wp_deregister_script( 'wpsc-thickbox' );
    	wp_deregister_script( 'jquery-rating' );
    	wp_deregister_script( 'wp-e-commerce' );
    	wp_deregister_script( 'jQuery' );
    	wp_deregister_script( 'infieldlabel' );
    	wp_deregister_script( 'wp-e-commerce-ajax-legacy' );
    	wp_deregister_script( 'wp-e-commerce-dynamic' );
    	wp_deregister_script( 'livequery' );
    	wp_deregister_script( 'wp-e-commerce-legacy' );
    	wp_deregister_script( 'l10n' );
    	wp_deregister_script( 'wpsc-gold-cart' );
    }
    add_action( 'wp_print_scripts', 'wpsc_deregister_scripts', 100 );

    Basically i want that to stop working on one section of my website.

    Basicly it turns off the JS for WP ecommerce which loads regarless of what page you are on.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You can put a conditional around it such as:

    if ( is_home() || is_front_page() ) {
    function wpsc_deregister_scripts() {
    	wp_deregister_script( 'wpsc-thickbox' );
    	wp_deregister_script( 'jquery-rating' );
    	wp_deregister_script( 'wp-e-commerce' );
    	wp_deregister_script( 'jQuery' );
    	wp_deregister_script( 'infieldlabel' );
    	wp_deregister_script( 'wp-e-commerce-ajax-legacy' );
    	wp_deregister_script( 'wp-e-commerce-dynamic' );
    	wp_deregister_script( 'livequery' );
    	wp_deregister_script( 'wp-e-commerce-legacy' );
    	wp_deregister_script( 'l10n' );
    	wp_deregister_script( 'wpsc-gold-cart' );
    }
    add_action( 'wp_print_scripts', 'wpsc_deregister_scripts', 100 );
    }

    I added both the is_home and is_front_page seeing I’m not sure if you have a static home page for your front page or a list of blog posts. Hope that helps!

    Thanks for the suggestion Unfortunately it didn’t work for me, is there another way to do it?

    i was thinking of doing it via URI segments how-ever i doubt that would work.

    Ahh I didn’t pay enough attention to your code before. You’re using wp_print_scripts when you’re probably after wp_enqueue_scripts.

    Try this:

    if ( is_home() || is_front_page() ) {
    function wpsc_deregister_scripts() {
    	wp_deregister_script( 'wpsc-thickbox' );
    	wp_deregister_script( 'jquery-rating' );
    	wp_deregister_script( 'wp-e-commerce' );
    	wp_deregister_script( 'jQuery' );
    	wp_deregister_script( 'infieldlabel' );
    	wp_deregister_script( 'wp-e-commerce-ajax-legacy' );
    	wp_deregister_script( 'wp-e-commerce-dynamic' );
    	wp_deregister_script( 'livequery' );
    	wp_deregister_script( 'wp-e-commerce-legacy' );
    	wp_deregister_script( 'l10n' );
    	wp_deregister_script( 'wpsc-gold-cart' );
    }
    add_action( 'wp_enqueue_scripts', 'wpsc_deregister_scripts', 10 );
    }'

    If that doesn’t work then try changing the priority down from 10 to maybe 9. The 100 you had in your example is very late.

    Also wp_deregister_script( 'jQuery' ); might not work so maybe try wp_deregister_script( 'jquery' );

    I’d be very careful deregistering jQuery as well because so many plugins and themes use it these days.

    And if that doesn’t work still trying using:

    if ( is_page( 54 ) ) {

    as your conditional statement where 54 is the page ID of your homepage. Hopefully that might get you on the right track!

    What Bronson suggested should work just fine (if the homepage/front page are the pages you want to disable the scripts).

    If there is a specific page/s you wish to disable the scripts on, do the following.

    if ( is_page('about') || is_page('hello-world') ) {
    function wpsc_deregister_scripts() {
    	wp_deregister_script( 'wpsc-thickbox' );
    	wp_deregister_script( 'jquery-rating' );
    	wp_deregister_script( 'wp-e-commerce' );
    	wp_deregister_script( 'jQuery' );
    	wp_deregister_script( 'infieldlabel' );
    	wp_deregister_script( 'wp-e-commerce-ajax-legacy' );
    	wp_deregister_script( 'wp-e-commerce-dynamic' );
    	wp_deregister_script( 'livequery' );
    	wp_deregister_script( 'wp-e-commerce-legacy' );
    	wp_deregister_script( 'l10n' );
    	wp_deregister_script( 'wpsc-gold-cart' );
    }
    add_action( 'wp_print_scripts', 'wpsc_deregister_scripts', 100 );
    }

    Alternatively, if there is a top-level page and it’s sub pages you wish the disable the scripts one, try looking into the ‘is_tree()’ function.

    Oops, that’ll teach me to post before refreshing. Bronson’s suggestion should work just fine.

    Haha looks like we posted at the same time Oliver! 🙂

    If you’re using is_page with the page slug then I’d recommend using an array just in case someone changes the slug name down the track so I’d do something like:

    if ( is_page( array ( 2, 'about', 'About Me' ) ) ) {

    That way it’ll look for the page ID first, the slug second then the title. I learnt the hard way when I used just the slug as a conditional once for a client and they changed the slug name on me and my conditional logic broke 🙁

    Normally I would’ve done the full array but seeing bubaphex didn’t add a link to their URL I couldn’t be as specific 🙂

    Ah! That would make sense 🙂 That could save me a lot of messing about.

    Thanks for you help guys,

    Ive tried the above but didnt work for me, even tried page specific to which i had no luck.

    perhaps im mis-understanding where i should put this, should this be on the functions.php (which is where i have it now) or does this go on the page it self?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Function to load on homepage only in function.php’ is closed to new replies.