How To Load BuddyPress Scripts & Styles Only On BuddyPress Pages

Home » WordPress Tutorials » How To Load BuddyPress Scripts & Styles Only On BuddyPress Pages
  • buddypress

How To Load BuddyPress Scripts & Styles Only On BuddyPress Pages

BuddyPress is the social network “in a box” for WordPress. It allows users to build profiles, groups, and post to their own activity feeds. The project is very well maintained and optimized out of the box, but a lot of websites do not built it as the focus. Instead, they add it to their site to add functionality. However, once you add BuddyPress, the number of scripts and styles that are loaded tends to increase dramatically.

This is because there is a lot of content and functionality out of the box.. However, for many users, there is no reason to load this content on every single page.

For example, for a news website, loading the CSS and JS for BuddyPress profiles on your homepage or posts is not needed. Therefore, I have written some quick code that will only load BuddyPress Scripts and Styles on BuddyPress pages.  This is to help reduce the load time on your website.

Loading BuddyPress Scripts Only On BuddyPress Pages

Add the following piece of code to your child themes functions.php file or to a site-specific plugin.

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

After doing so you should see a major decrease in the number of CSS & JS files loaded on non-BuddyPress pages. It also includes code that checks to see if BuddyPress was deactivated. If so, you will not see a white screen of death upon deactivation.

If you have any questions please feel free to ask me below and I will be sure to answer! Also, if you are looking to further improve your websites load time consider purchasing our optimization service!

By | 2017-12-18T20:47:21+00:00 December 18th, 2017|Categories: WordPress Tutorials|Tags: , , |0 Comments

About the Author:

Scott Hartley is a WordPress Developer who focuses on website performance optimization, SEO, and security. He can't tell you what a color wheel is but he knows the intricacies of making a website load quickly.

Leave A Comment