Archived topic
Dynamic Button Class
3 replies · Started by Tom on May 4, 2021
GP Team -
I'm loving the new GP Premium 2.0 updates! Great work.
One item for the wishlist (as I don't think it's possible with the new dynamic data fields?)... I currently have a couple page heroes built using the classic header element. The button class refers to a custom field - as there are various pop-ups used throughout the site.
Would love to be able to transfer the headers over to a block element for easier editing/maintenance. But this is the one piece holding me back! I think I would have to create an element for each pop-up class.
So... adding dynamic data to the button class field would be an excellent update :)

Hi Tom,
Glad you're enjoying it!
The main issue I see here is that the Button block is actually a static HTML element - it's not dynamic.
However, you could add a placeholder class in the CSS Classes field in the Advanced panel, then dynamically replace it with the dynamic value using a filter:
add_filter( 'render_block', function( $block_content, $block ) {
if ( 'generateblocks/button' === $block['blockName'] ) {
$my_dynamic_field = get_post_meta( get_the_ID(), 'my_custom_field', true );
if ( $my_dynamic_field ) {
$block_content = str_replace( 'my-placeholder-class', $my_dynamic_field, $block_content );
}
}
return $block_content;
}, 10, 2 );
Hope this helps!
Love it. Works like a charm. Thanks, Tom!
You're welcome! :)