Meta Box Group Subfield Text’s Slug as Query Loop Item ID in Bricks

In the BricksLabs Facebook group a user asks:

I have a metaBox custom field called ‘section_title’ inside a clonable metabox group called ‘about_section’

How can I used the dynamic tag to return the value of section_title with all the spaces replaced with dashes.

e.g. if the section title was ‘section one’, I want to return ‘section-one’ with the echo tag.

The reason is to dynamically add IDs to each section in Bricks query loop via attributes.

This Pro tutorial provides the steps to set the id of each item of a Meta Box Group query to the slug of a text-type subfield’s value.

Consider this field group for a Page:

about_section field being populated:

Bricks editor:

Section JSON

For the query loop-enabled element add id attribute like this:

Add the following in child theme‘s functions.php (w/o the opening PHP tag) or a code snippets plugin:

<?php 

function bl_slugify_section_title(): string {
    $loopObject = BricksQuery::get_loop_object();
    // Utilize the null coalescing operator to handle missing 'section_title' safely
    $sectionTitle = $loopObject['section_title'] ?? null;  // Returns null if key does not exist

    // Return sanitized title if it exists or an empty string as fallback
    return $sectionTitle ? sanitize_title( $sectionTitle ) : '';
}

Replace section_title with the ID of your subfield.

Whitelist the bl_slugify_section_title function.

Ex.:

<?php 

add_filter( 'bricks/code/echo_function_names', function() {
  return [
    'bl_slugify_section_title'
  ];
} );

You should also add any other functions (native or custom) being used in your Bricks instance in addition to bl_slugify_section_title. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.

More info on whitelisting can be found here.