CSS Body Class for BuddyPress Group Moderators and Admins

I tend to use CSS to hide all sorts of things within the BuddyPress interface rather than unhooking functions. I find that it more quickly and flexibly accommodates my clients’ changing desires.

Today I needed to add a css body class for group moderators and admins. The goal was to hide the “Admin” button for moderators but display it for admins.

To pull it off, I added the following to my functions.php file:

// Add "bp-group-moderator" to body_class function if we are on a BuddyPress-generated page
add_filter('body_class','add_bp_group_moderator_body_classes');
function add_bp_group_moderator_body_classes($classes) {
	
	if ( bp_group_is_mod() ):
	
	// add 'bp-group-moderator' to the $classes array
	$classes[] = 'bp-group-moderator';
	// return the $classes array
	return $classes;
	
	else :
	
		// do nothing
		$classes[] = '';
		// return the $classes array
		return $classes;
		
	endif;
	
}

// Add "bp-group-admin" to body_class function if we are on a BuddyPress-generated page
add_filter('body_class','add_bp_group_admin_body_classes');
function add_bp_group_admin_body_classes($classes) {
	
	if ( bp_group_is_admin() ):
	
	// add 'bp-group-admin' to the $classes array
	$classes[] = 'bp-group-admin';
	// return the $classes array
	return $classes;
	
	else :
	
		// do nothing
		$classes[] = '';
		// return the $classes array
		return $classes;
		
	endif;
	
}

It works nicely!

I plan to release it as a plugin. Perhaps some day this will be in core. In general, I can never have too many body classes. 🙂

The Mighty Mo! Design Co.

RSS From Toby’s Blog

  • The Horrors of Covering Your Own Ass
    Today I emailed a very simple question to support@[companyname].com (2 sentences total). The automated response I got back was this: Keep in mind that the above was an auto-response and: It’s hard to read. Everything has the same import (e.g. none of it is important enough to call out separately). It’s long. It added absolutely-zero…
  • If something stupid makes money, then it’s not stupid.
    I’ve seen some stupid things in my life…And some of them were really really smart. I remember a story from decades ago where the U.S. military was looking to equip the Stealth Bomber with a fancy computer-driven video system so the pilots could see behind them. Well, the computers failed to get the job done,…
  • There is no destination.
    Coding is a process of failure followed by a moment of great joy. You've gotta ride through the failures to experience the joy...

More posts from themightymo.com

minneapolis wordpress design logo

How to Preload Font in WordPress Functional Plugin

By The Mighty Mo! Design Co. | August 5, 2021

use a plugin to do a bunch of optimizations on this site, but for some reason, the preloading of fonts is broken. So I rolled my own font preloading function and placed it in my functional plugin (You could also put it in functions.php). Here’s the code.

How to Change Background on Beaver Builder Sticky Header with CSS

By The Mighty Mo! Design Co. | July 29, 2021

Beaver Builder doesn’t give you an interface to change the background color of the sticky menu after scrolling. Here’s a simple bit of CSS that can help.

How I Optimized My Website Today

By The Mighty Mo! Design Co. | July 24, 2021

Today I decided to invest 1 hour optimizing themightymo.com. Below are the actions I took along with the results. Speed Benchmarks and Initial Observations Before starting on today’s speed optimization, Google Pagespeed Insights scored us as a “44”. There’s some files loading from the wp.com cdn (e.g. Jetpack’s cdn) – Google is penalizing me for…

RSS From Toby’s Blog

  • The Horrors of Covering Your Own Ass
    Today I emailed a very simple question to support@[companyname].com (2 sentences total). The automated response I got back was this: Keep in mind that the above was an auto-response and: It’s hard to read. Everything has the same import (e.g. none of it is important enough to call out separately). It’s long. It added absolutely-zero…
  • If something stupid makes money, then it’s not stupid.
    I’ve seen some stupid things in my life…And some of them were really really smart. I remember a story from decades ago where the U.S. military was looking to equip the Stealth Bomber with a fancy computer-driven video system so the pilots could see behind them. Well, the computers failed to get the job done,…
  • There is no destination.
    Coding is a process of failure followed by a moment of great joy. You've gotta ride through the failures to experience the joy...