recently I wrote this code for one of my project
I shared on my site too; Add Custom Setting to Bricks Builder Post/Page Settings and Dynamic Tag to Access that Textarea Content › Sinan Isler | WordPress Developer
// adds the control textarea and setting
add_filter( 'builder/settings/page/controls_data', function( $data ) {
if ( isset( $data['controlGroups']['general'] ) ) {
$data['controls']['template_code'] = [
'group' => 'general',
'type' => 'textarea',
'label' => esc_html__( 'Template Code', 'bricks' ),
'description' => esc_html__( 'Enter custom template code for this page.', 'bricks' ),
'placeholder' => esc_html__( 'Add your template code here', 'bricks' ),
];
}
return $data;
});
// adds the {template_code}
add_filter( 'bricks/dynamic_tags_list', 'register_template_code_tag' );
function register_template_code_tag( $tags ) {
$tags[] = [
'name' => '{template_code}',
'label' => 'Template Code',
'group' => 'Custom Tags',
];
return $tags;
}
add_filter( 'bricks/dynamic_data/render_tag', 'render_template_code_tag', 10, 3 );
function render_template_code_tag( $tag, $post, $context = 'text' ) {
if ( $tag !== 'template_code' ) {
return $tag;
}
$page_settings = get_post_meta( $post->ID, '_bricks_page_settings', true );
if ( $page_settings ) {
$data = maybe_unserialize( $page_settings );
if ( isset( $data['template_code'] ) ) {
return $data['template_code'];
}
}
return 'No Template Code Found';
}
add_filter( 'bricks/dynamic_data/render_content', 'render_template_code_tag_in_content', 10, 3 );
function render_template_code_tag_in_content( $content, $post, $context = 'text' ) {
if ( strpos( $content, '{template_code}' ) !== false ) {
$template_code = render_template_code_tag( 'template_code', $post, $context );
$content = str_replace( '{template_code}', $template_code, $content );
}
return $content;
}
this adds a control to the page setting and data tag.
I made this because I wanted to control my custom fields from the bricks editor without leaving editor. I get lots of big projects that require cpt and cf.
problem is bricks control saves the data serialized php and every time I need to use I must unserialize the string.
is it possible to save controls_data without serializing it?