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 630 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Removing Action/Filter Inside a Plugin’s Namespaced Class Constructor

Removing Action/Filter Inside a Plugin’s Namespaced Class Constructor

Recently worked with a friend figuring out how to remove/undo/cancel an add_action() line that's inside a plugin's constructor inside a PHP Class with the file…
Categories:
Tags:
How to Add a Custom Control Field to an Element in Bricks

How to Add a Custom Control Field to an Element in Bricks

In this tutorial, we'll learn how to add a custom control field to an element and dynamically change a CSS property inside the builder. Introduction…
Categories:
Tags:
Pro
How to add a Class to all Images in Bricks Galleries

How to add a Class to all Images in Bricks Galleries

Learn how to add a custom class to all the images rendered by Bricks' Gallery elements so these images can be excluded from Perfmatters' Lazy…
Pro
x Number of Random Bricks Query Loop Items(s) to be Shown Every x Hours

x Number of Random Bricks Query Loop Items(s) to be Shown Every x Hours

Showing how to display one random row of a Meta Box group for an hour and then output another random row.