BuddyDev

Search

How To Load BuddyPress Scripts & Styles Only On BuddyPress Pages

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #34834

    Hello,

    Please can you help improve this code in something might be missing I want to load BuddyPress scripts & styles only on BuddyPress pages.
    For example, for a news website, loading the CSS and JS for BuddyPress profiles and activity on the homepage, posts or custom post type is not needed.

     function conditional_buddypress_styles_scripts() {
    
      // First check that buddypress exists to prevent fatal errors
      if ( function_exists( 'is_buddypress' ) ) {
        //dequeue scripts and styles
        if ( ! is_buddypress() ) {
          wp_dequeue_style( 'bp-mentions-css' );
          wp_dequeue_style( 'bp-legacy-css' );
          wp_dequeue_script( 'bp-confirm' );
          wp_dequeue_script( 'bp-widget-members' );
          wp_dequeue_script( 'bp-jquery-query' );
          wp_dequeue_script( 'bp-jquery-cookie' );
          wp_dequeue_script( 'bp-jquery-scroll-to' );
          wp_dequeue_script( 'bp-legacy-js' );
          wp_dequeue_script( 'jquery-atwho' );
          wp_dequeue_script( 'bp-mentions' );
        }
      }
    }
    add_action( 'wp_enqueue_scripts', 'conditional_buddypress_styles_scripts', 99 ); 

    Thanks

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #34835

    Please also add support for Nouveau

  • Keymaster
    (BuddyDev Team)
    Posts: 24653
    Brajesh Singh on #34842

    Hi Tosin,
    Thank you for the question.

    I will suggest avoiding such code. It is not going to help you much and will cause a lot of issue in future. There are dependent plugins which may start throwing javascript errors and breaking some of your site functionality.

    It is not worth the effort. You should rather enable browser caching for the assets to avoid loading it again once it gets loaded and look for the best practices like enabling gzip compression etc.

    Also, if you do not want the script and styles, you should better conditionally avoid loading BuddyPress on some of the pages. There is a plugin for it.

    If you still want to proceed with it, Please let me know. I will ask @ravisharma to put some code for the same.

    Regards
    Brajesh

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #34850

    Thanks for the advice I would appreciate your code

  • Keymaster
    (BuddyDev Team)
    Posts: 24653
    Brajesh Singh on #34851

    I will ask @ravisharma to post it asap.

    Regards
    Brajesh

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #34872

    Hello Tosin,

    Try the following code. It will prevent BuddyPress assets not to load on other pages.

    
    
    add_action( 'bp_template_redirect', function () {
    
        if ( is_buddypress() ) {
            return;
        }
    
        if ( has_action( 'wp_enqueue_scripts', 'bp_enqueue_scripts' ) ) {
            remove_action( 'wp_enqueue_scripts', 'bp_enqueue_scripts' );
        }
    } );
    
    

    Please check and let me know if it works or not.

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #34885

    It worked perfectly but the only issue I faced was with notifications, Is it possible to enable the scripts for notifications and disable all other scripts.

    Thank

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #34889

    Hello Tosin,

    Thank you for the acknowledgement. Are you refering notifications in admin bar or any other section of the site?. Above mentioned will affect all widgets and shortcodes offered by BuddyPress on pages other than BuddyPress.

    Regards
    Ravi

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #34901

    I am referring to all notifications in other sections of the site and yes it should affect all widgets and shortcodes

  • Participant
    Level: Guru
    Posts: 900
    Tosin on #34929

    Any feedback sir thanks

You must be logged in to reply to this topic.

This topic is: not resolved