How to Remove / Dequeue BuddyPress Scripts
So you’re running BuddyPress, and you’ve got a beautiful mini-social network of some sort or another up and running, eh?
But then you do a Pagespeed analysis and realize that your entire site is loading a ton of (really small) scripts that are just killing you with pageload times and extra resources blocking the rest of your site from loading as fast as we all want the web to work these days.
What to do?
Well, dequeueing BuddyPress scripts is actually pretty easy. Open up your functions.php file and paste in the following:
function dequeue_buddypress() {
if (!is_admin()) {
wp_dequeue_style('bp-legacy-css');
wp_deregister_script('bp-jquery-query');
wp_deregister_script('bp-confirm');
}
}
add_action('wp_enqueue_scripts', 'dequeue_buddypress');
That’ll do it. We remove the BuddyPress style sheet and confirm.min.js, as well as all other scripts with that one call to bp-jquery-query.
Now I’m not saying you should do this. Lots of stuff in BuddyPress will break if you remove all of its Javascript, and you’ll need to create a new stylesheet to work with your theme. However, BuddyPress doesn’t always run on every page of a site. So you could modify this code like so, say if you didn’t need to load these scripts on your homepage:
function dequeue_buddypress() {
if (!is_admin() && !is_front_page()) {
wp_dequeue_style('bp-legacy-css');
wp_deregister_script('bp-jquery-query');
wp_deregister_script('bp-confirm');
}
}
add_action('wp_enqueue_scripts', 'dequeue_buddypress');
Note the highlighted addition to the code above, and switch it out with whatever conditional statement might suit your particular needs!