Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Adds classes to body element when viewing CiviCRM in WordPress admin.
<?php
add_filter( 'admin_body_class', 'my_admin_body_classes' );
/**
* Adds classes to body element when viewing CiviCRM in WordPress admin.
*
* This allows selectors to be written for CiviCRM admin pages.
*
* @param str $classes The existing admin body classes.
* @return str $classes The modified admin body classes.
*/
function my_admin_body_classes( $classes ) {
// bail if no CiviCRM
if ( ! function_exists( 'civi_wp' ) ) {
return $classes;
}
// base our classes on the current request args
$args = civi_wp()->get_request_args();
// bail if we don't have any
if ( is_null( $args['argString'] ) ) {
return $classes;
}
// init CiviCRM classes
$civicrm_classes = array();
// check for top level - it can be assumed this always 'civicrm'
if ( isset( $args['args'][0] ) AND ! empty( $args['args'][0] ) ) {
$civicrm_classes[] = $args['args'][0];
}
// check for second level - the component
if ( isset( $args['args'][1] ) AND ! empty( $args['args'][1] ) ) {
$civicrm_classes[] = $args['args'][0] . '-' . $args['args'][1];
}
// check for third level - the component's configuration
if ( isset( $args['args'][2] ) AND ! empty( $args['args'][2] ) ) {
$civicrm_classes[] = $args['args'][0] . '-' . $args['args'][1] . '-' . $args['args'][2];
}
// check for fourth level - because well, why not?
if ( isset( $args['args'][3] ) AND ! empty( $args['args'][3] ) ) {
$civicrm_classes[] = $args['args'][0] . '-' . $args['args'][1] . '-' . $args['args'][2] . '-' . $args['args'][3];
}
// add to admin body classes
$classes .= ' ' . implode( ' ', $civicrm_classes );
// --<
return $classes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment