Render an Element conditionally based on ACF field stored in options table

Display an element conditionally based on content in the options_meta table, referencing an ACF field.

I’ve created a Theme Options ACF Options Page, which stores its field data in the options table. I need to conditionally display an element in a global Header Template based on the content of that field. I think I’m close, but not quite.

Working of of example 5 on the Filter:Render page:

add_filter( 'bricks/element/render', function( $render, $element )
// Get the element custom HTML ID 
$html_id = isset( $element->settings['_cssId'] ) ? $element->settings['_cssId'] : false;
 //Check if the element has the HTML ID "header-primary-button" 
if ( $html_id && $html_id === 'header-primary-button' ) {
return ! get_option('options_add_primary_cta_button', false); }
return $render; }, 10, 2 );

I know I’m close, but it’s not quite right. The option is correctly stored in the database, but I don’t think I’m evaluating it correctly.

Try replacing that with

return get_field( 'options_add_primary_cta_button', 'option' );

Thanks for the reply!

Unfortunately, that code isn’t quite working- but I think there’s logic missing. I tried adding that code to my site and it’s not showing the element whether the field contains ‘true’ or ‘false’.

What I need to do is have the rendering of the element be true if the content of the field ‘options_add_primary_cta_button’ is ‘true’. I was thinking something like:

// Check if the element has the HTML ID "header-primary-button"
  if ( $html_id && $html_id === 'header-primary-button' ) {
    $check =  get_field( 'options_add_primary_cta_button', 'option');
     if ( $check === 'true' ) {
    return $render;
}
  }

But I’m not sure that’s quite right either. Any ideas?

What is the type of your custom field? Checkbox?

I’m using a radio button field.