Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/08/2015 08:30:22 PM (4 years ago)
Author:
mercime
Message:

Ensure specific classes are injected in BuddyPress widgets.

This fixes the styling of BuddyPress widgets in themes where the
developer has not used variables necessary to generate dynamic CSS
classes in their widget sidebar's before_widget call.

Props r-a-y, DJPaul.
Fixes #6532.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-filters.php

    r10108 r10220  
    10791079}
    10801080add_filter( 'bp_activity_maybe_load_mentions_scripts', 'bp_maybe_load_mentions_scripts_for_blog_content', 10, 2 );
     1081
     1082/**
     1083 * Injects specific BuddyPress CSS classes into a widget sidebar.
     1084 *
     1085 * Helps to standardize styling of BuddyPress widgets within a theme that
     1086 * does not use dynamic CSS classes in their widget sidebar's 'before_widget'
     1087 * call.
     1088 *
     1089 * @since 2.4.0
     1090 * @access private
     1091 *
     1092 * @global array $wp_registered_widgets Current registered widgets.
     1093 * @param  array $params                Current sidebar params.
     1094 * @return array
     1095 */
     1096function _bp_core_inject_bp_widget_css_class( $params ) {
     1097    global $wp_registered_widgets;
     1098
     1099    $widget_id = $params[0]['widget_id'];
     1100
     1101    // If the current widget isn't a BuddyPress one, stop!
     1102    // We determine if a widget is a BuddyPress widget, if the widget class
     1103    // begins with 'bp_'.
     1104    if ( 0 !== strpos( $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base, 'bp_' ) ) {
     1105        return $params;
     1106    }
     1107
     1108    // Dynamically add our widget CSS classes for BP widgets if not already there.
     1109    $classes = array();
     1110
     1111    // Try to find 'widget' CSS class.
     1112    if ( false === strpos( $params[0]['before_widget'], 'widget ' ) ) {
     1113        $classes[] = 'widget';
     1114    }
     1115
     1116    // Try to find 'buddypress' CSS class.
     1117    if ( false === strpos( $params[0]['before_widget'], ' buddypress' ) ) {
     1118        $classes[] = 'buddypress';
     1119    }
     1120
     1121    // Stop if widget already has our CSS classes.
     1122    if ( empty( $classes ) ) {
     1123        return $params;
     1124    }
     1125
     1126    // CSS injection time!
     1127    $params[0]['before_widget'] = str_replace( 'class="', 'class="' . implode( ' ', $classes ) . ' ', $params[0]['before_widget'] );
     1128
     1129    return $params;
     1130}
     1131add_filter( 'dynamic_sidebar_params', '_bp_core_inject_bp_widget_css_class' );
Note: See TracChangeset for help on using the changeset viewer.