BuddyPress 11.0.0 to Limit JavaScript and CSS Asset Loading to Community Pages Using a Filter

BuddyPress will soon be improving the way it loads its JavaScript and CSS assets so that they are only loaded on community pages. Previously, the plugin would load them indiscriminately on every page.

BuddyPress lead developer Mathieu Viet said he’s not sure there is a specific reason explaining why this was kept in place. Before the plugin introduced the BP Theme Compat API in version 1.7, it was necessary to use a BuddyPress compatible theme like the one bundled by default (BP Default).

“I think we kept the way this theme was loading these assets into the first Template Pack (BP Legacy) we added to BuddyPress,” Viet said.

Users have often requested BuddyPress only load its assets on community pages in hopes of further optimizing their websites. For example, in 2020, a user on the BuddyDev forums requested custom code to accomplish this. Experts recommended against doing it

“It is not going to help you much and will cause a lot of issues in future,” BuddyPress contributor Brajesh Singh said. “There are dependent plugins which may start throwing JavaScript errors and breaking some of your site functionality. It is not worth the effort.”

Singh recommended the user enable browser caching to avoid loading the assets multiple times and stick to best practices for enabling gzip compression and other optimization measure. He also recommended adding a plugin that would conditionally prevent loading BuddyPress on certain pages.

Coming in version 11.0.0, BuddyPress core will progressively move towards loading only the assets it needs in community areas. This update will still load JS and CSS everywhere but will offer a filter that users can add to their bp-custom.php files in order to keep BP assets on community pages only:

add_filter( ‘bp_enqueue_assets_in_bp_pages_only’, ‘__return_true’ );

“If using the above filter, you notice something is going wrong with your website due to the use of a specific BP plugin or theme, report it here and we’ll then have another development cycle to fix things before we completely restrict these assets to BuddyPress generated pages in a second step in version 12.0.0,” Viet said.

Version 11.0.0 is expected to be released on December 14, 2022. Early adopters and BuddyPress site owners who have always wished for the plugin to behave this way can take advantage of it after the next major update using the filter. The filter can also be easily removed if users are troubleshooting and having issues with plugins.