Disable bbPress and bbPress Post Toolbar Scripts and Styles if Not on a Forum Page

Here’s a quick little tidbit for you. I was recently working on a few small things to make my site load a bit faster, and I was digging through all my scripts and styles that load. I noticed that the scripts and styles for the bbPress 2.x plugin and the bbPress Post Toolbar plugin loaded on every page. Here’s a quick fix to load them only on an actual bbPress page.

Luckily, back on RC1 I think it was, bbPress implemented an is_bbpress conditional. This works out real nice for us. We can just check to make sure that function exists (so our site doesn’t crash if we disable bbPress) and then conditionally disable the scripts and styles based on that. First up, here’s what I wanted to disable.

  1. bbPress overall style
  2. bbPress Post Toolbar style
  3. bbPress Post Toolbar uploader style
  4. bbPress Post Toolbar javascript
  5. bbPress Post Toolbar uploader javascript

We’re just gonna make handy use of the wp_deregister_script and wp_deregister_style functions to take care of all this. I cruised through the source of each plugin to find the handles I needed, and then plopped them inside some conditionals, and here we have it:

//DEREGISTER SCRIPTS AND STYLES ASSOCIATED WITH BBPRESS AND BBPRESS TOOLBAR
add_action( 'wp_print_styles', 'voodoo_deregister_bbstyles', 100 );
add_action( 'wp_print_scripts', 'voodoo_deregister_bbscript', 100 );

function voodoo_deregister_bbstyles() {
	if ( function_exists( 'is_bbpress' ) ) {
		if ( !is_bbpress() ) {
			wp_deregister_style( 'bbp_5o1_post_toolbar_style' );
			wp_deregister_style( 'bbp_5o1_post_toolbar_uploader_style' );
			wp_deregister_style( 'bbpress-style' );
		}
	}
}

function voodoo_deregister_bbscript() {
	if ( function_exists( 'is_bbpress' ) ) {
		if ( !is_bbpress() ) {
			wp_deregister_script( 'bbp_5o1_post_toolbar_script' );
			wp_deregister_script( 'bbp_5o1_post_toolbar_uploader_script' );
		}
	}
}

See that, easy! If you don’t use the toolbar, you can just use the styles portion with the one line for bbpress-style if you like. But with this simple block of code, we’ve stopped 5 files from loading when not needed. Cool!

6 Replies to “Disable bbPress and bbPress Post Toolbar Scripts and Styles if Not on a Forum Page”

  1. Thank you so much for sharing your valuable WP/BP voodoo Reverend. This little snippet of code seemed like it sped up the BuddyPress site I’m working on. Getting ready to check out more of your site.

Leave a Reply