Software Developer

Remove bbPress Styles and Script on Non-Forum Pages

This snippet will stop bbPress from loading the default bbPress CSS stylesheet and the editor.js script on all pages except the bbPress forum pages.

/**
 * Dequeue bbPress CSS and .js on non-forum pages.
 */
function isa_dequeue_bbp_style() {
	if ( class_exists('bbPress') ) {
	  if ( ! is_bbpress() ) {
		wp_dequeue_style('bbp-default');
		wp_dequeue_style( 'bbp_private_replies_style');
		wp_dequeue_script('bbpress-editor');
	  }
	}
}
add_action( 'wp_enqueue_scripts', 'isa_dequeue_bbp_style', 99 );

Please note that bbPress loads the default stylesheet ( ‘bbp-default’ ) on all pages in order to style any bbPress widgets that may be used on non-forum pages. So, if you’re using a bbPress widget on your home page, you may not want to remove the bbPress styles from the home page. In that case, change line 6 to:

if ( ! is_bbpress() && ! is_front_page() ) {

which will load the bbPress stylesheet only on forum pages and on the home page.

By

We've 8 Responses

  1. January 8th, 2015 at 8:51 am

    Ciao Isabel, I would like to use your code – but I’m calling BBPRESS forums and topics using a shortcode on some wordpress pages. What modifications do i need to make to line 6 to make it OK to load on “pages” but not posts, front page, categories, etc.?

    Thanks,

    L

    avatar
      • January 8th, 2015 at 12:09 pm

        Thank you for your quick reply, Isabel! After I hit submit, I realized that my homepage IS a static page. Is there some Syntax where I can say “is not page home” or “is NOT page 123”)?

        Ciao,

        L

        avatar
        • January 8th, 2015 at 12:13 pm

          P.S. I have my forums show up as shortcodes in other pages (to get around some kloogy formatting of my theme or bbpress) so when I tested this change it actually broke the pages where I called the forums.

          P.P.S. My goal is to NOT have the style and script load on the homepage (at least) and everywhere else where I don’t have the shortcode (at best).

          P.P.P.S. Thanks for your great post and at least showing me that it’s possible!

          avatar
          • January 8th, 2015 at 3:07 pm

            I’m sorry about that. The best way may be (and I use this method on 1 site where I also use bbPress shortcodes) to get all the page IDs of the pages where you are using bbPress shortcodes. Then use this, but replace the numbers (123, 124, 125, 126) with your own page IDs:

            if ( ! is_bbpress() && ! is_page( array( 123, 124, 125, 126 ) ) ) {

            That will not remove the scripts on those listed page IDs.

            avatar
          • January 9th, 2015 at 2:27 am

            You rock! This is fantastic. It works!!!

            Now to track down how to stop all of the of the unnecessary scripts and stylesheets from loading on the homepage. If you were ever looking for an idea to make a WordPress plug-in, there’s one!

            I’ve tried two and they were too technical for me to understand or use which is how I ended up on your page. I was searching for individual solutions for each plug-in.

            One down, three to go. : )

            Ciao,

            L

            avatar
  2. April 22nd, 2015 at 4:52 pm

    That is a handy help… I am optimizing my site’s speed and it is useless and wast of bandwidth to include bbpress css in every page… Thiis code works perfectly and made my site more speedy.

    avatar
  3. March 28th, 2016 at 11:06 am

    Thanks for sharing. My blog http://computerstories.net/ loads much faster now. One small thing I would like to add is that the code needs to be put in the functions.php file. That might seem logical for most, but I thought I’d mention it just for some extra clarity.

    avatar

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]