How To Load BuddyPress Scripts & Styles Only On BuddyPress Pages
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!
Scott Hartley
Founder & CEO, Sert Media
Founder and CEO of Sert Media, a Nashville-based digital marketing agency. Scott has spent over 15 years helping businesses grow through SEO, web performance optimization, and strategic digital marketing. His deep expertise in WordPress development, site speed, and technical SEO has guided hundreds of brands toward measurable results. When he's not auditing Core Web Vitals or refining campaign strategies, he's writing about the tools, techniques, and trends shaping the modern web.
Related Articles
How to Add Security Headers to WordPress Using Cloudflare Transform Rules
If you've ever run your WordPress site through securityheaders.com and gotten a D or F…
Use This Cloudflare Rule To Reduce Plugin Hack Attempts
Cloudflare Firewall Rules give you a lot of flexibility in further protecting your website. Here…
Set Up Cloudflare's Automatic Platform Optimization Feature
Automatic Platform Optimization (APO) is a new feature from Cloudflare that allows you to cache…