Add Extra CSS Class to Widget

 

/*
 * Add Extra CSS Class to Widget
 */
function add_extra_class_to_widget( $params ) {

    // get widget vars
    $widget_name = $params[ 0 ][ 'widget_name' ];
    $widget_id = $params[ 0 ][ 'widget_id' ];

    // bail early if this widget is not a Text widget
    if ( $widget_name != 'Text' ) {
        return $params;
    }

    // add Class to before_widget
    $class = 'my-class';

    // Get class name from ACF Fields
    // $class = get_field( 'class', 'widget_' . $widget_id );

    if ( $class ) {
        $classe_to_add = 'class="' . $class . ' ';
        $params[ 0 ][ 'before_widget' ] = str_replace( 'class="', $classe_to_add, $params[ 0 ][ 'before_widget' ] );
    }

    return $params;
}

add_filter( 'dynamic_sidebar_params', 'add_extra_class_to_widget' );

Leave a Reply