9th Oct '23
/
0 comments

Class-based Data Attributes in Bricks

Bricks provides a bricks/element/set_root_attributes filter using which HTML classes and data attributes can be set to elements programmatically on the frontend.

If you want to add a data attribute conditionally depending on the element’s class, here’s the sample code:

add_filter( 'bricks/element/set_root_attributes', function( $attributes, $element ) {
	if ( in_array( 'test-class', $attributes['class'] ) ) {
		$attributes['data-header-type'] = 'shrinking'; 
	}

	return $attributes;
}, 10, 2 );

The above will add data-header-type attribute having a value of shrinking to any/all element(s) that have a class of test-class.

Get access to all 633 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Filtering out Media Items from “Select post/page” Bricks control

Filtering out Media Items from “Select post/page” Bricks control

The dropdown that appears after choosing "Internal post/page" when setting a link in Bricks shows media (attachment) items besides Pages, Posts and other CPTs. If…
How to Remove Name and Website Fields in Bricks Comment Forms

How to Remove Name and Website Fields in Bricks Comment Forms

Updated on 14 Mar 2024 In the BricksLabs Facebook group a user asked: What is the best method to remove fields from the Bricks "Comments"…
Pro
Programmatically populating ACF field values in WordPress

Programmatically populating ACF field values in WordPress

An example of how to set the values of a Select-type field with the names and labels of all public post types.
Categories: