Hello,
Please can you help improve this code in something might be missing I want to load BuddyPress scripts & styles only on BuddyPress pages.
For example, for a news website, loading the CSS and JS for BuddyPress profiles and activity on the homepage, posts or custom post type is not needed.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 );
Thanks
Hi Tosin,
Thank you for the question.I will suggest avoiding such code. It is not going to help you much and will cause a lot of issue in future. There are dependent plugins which may start throwing javascript errors and breaking some of your site functionality.
It is not worth the effort. You should rather enable browser caching for the assets to avoid loading it again once it gets loaded and look for the best practices like enabling gzip compression etc.
Also, if you do not want the script and styles, you should better conditionally avoid loading BuddyPress on some of the pages. There is a plugin for it.
If you still want to proceed with it, Please let me know. I will ask @ravisharma to put some code for the same.
Regards
BrajeshI will ask @ravisharma to post it asap.
Regards
BrajeshHello Tosin,
Try the following code. It will prevent BuddyPress assets not to load on other pages.
add_action( 'bp_template_redirect', function () { if ( is_buddypress() ) { return; } if ( has_action( 'wp_enqueue_scripts', 'bp_enqueue_scripts' ) ) { remove_action( 'wp_enqueue_scripts', 'bp_enqueue_scripts' ); } } );
Please check and let me know if it works or not.
Regards
Ravi
You must be logged in to reply to this topic.