WordPress.org

Ready to get started?Download WordPress

Forums

Function to load on homepage only in function.php (9 posts)

  1. bubaphex
    Member
    Posted 2 years ago #

    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.

  2. Sennza Pty Ltd
    Member
    Posted 2 years ago #

    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!

  3. bubaphex
    Member
    Posted 2 years ago #

    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.

  4. Sennza Pty Ltd
    Member
    Posted 2 years ago #

    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!

  5. Oliver Farrell
    Member
    Posted 2 years ago #

    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.

  6. Oliver Farrell
    Member
    Posted 2 years ago #

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

  7. Sennza Pty Ltd
    Member
    Posted 2 years ago #

    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 :)

  8. Oliver Farrell
    Member
    Posted 2 years ago #

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

  9. bubaphex
    Member
    Posted 2 years ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Topic