Difficulty Extracting First Letter from Custom Field Using Dynamic Data in Bricks Builder

Hello Bricks Community,

I’m currently working on a project where I need to display only the first letter of a custom field, je_cct_reviews_reviewer_name, using Bricks Builder’s dynamic data capabilities. I’ve attempted the following methods without success:

  1. **Using the substr Modifier:**I tried the dynamic data tag {je_cct_reviews_reviewer_name:substr:0:1}, but it retrieves the first whole word instead of just the first letter.
  2. **Custom PHP Function with {echo}:**I created a custom function to extract the first letter and used it with the {echo} tag:

php

CopyEdit

function get_first_letter() {
    $reviewer_name = get_post_meta(get_the_ID(), 'je_cct_reviews_reviewer_name', true);
    return $reviewer_name ? substr($reviewer_name, 0, 1) : '';
}

In Bricks, I used {echo:get_first_letter()}, but it didn’t produce any output.
3. **Custom Shortcode:**I also defined a shortcode to achieve this:

php

CopyEdit

function get_first_letter_shortcode($atts) {
    $atts = shortcode_atts(
        array(
            'field' => '',
        ),
        $atts,
        'first_letter'
    );

    if (empty($atts['field'])) {
        return '';
    }

    $full_name = get_post_meta(get_the_ID(), $atts['field'], true);
    return $full_name ? substr($full_name, 0, 1) : '';
}
add_shortcode('first_letter', 'get_first_letter_shortcode');

Then, I used [first_letter field="je_cct_reviews_reviewer_name"] in Bricks, but still received no output.

Despite these attempts, I haven’t been able to display the first letter as intended. I came across a forum discussion mentioning issues with the {echo} function in Bricks v1.9.1.1, but I’m unsure if it’s related to my situation.

Could anyone provide guidance or alternative methods to achieve this? Any insights would be greatly appreciated.

Thank you in advance for your help!

I guess you can’t use WordPress built-in get_post_meta with JetEngine’s CCT since it’s not posts, just different objects with separated table in database

can you share how i can do this if you know? Thanks

Hello @yasir!

To quote the academy tutorial :

you must explicitly allow any function names you want to call via Bricks’ dynamic data echo tag using the new bricks/code/echo_function_names filter.

In your case, add the following snippet in your Bricks child theme functions.php file :

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

And don’t forget to enable code execution in Bricks settings, under the Custom code tab.

Then you will be able to use the {echo:get_first_letter} dynamic data tag.

Hope it solved your problem.

Thanks @Abd but it’s not working i tried this even shared with bricks support they shared same thing what i’m doing so i replied and now waiting for their reply.

I tried this: {echo:get_first_letter( ‘{je_cct_reviews_reviewer_name}’ )}

Your get_the_first_letter function doesn’t take any parameter, so you can’t use the dynamic data tag {echo:get_first_letter( ‘{je_cct_reviews_reviewer_name}’ )}.

You have to use this dynamic data tag {echo:get_first_letter}, after completing every step as explained in my previous post!

This is what i did so now it’s working :slight_smile:

Step 1: i added this code in function.php

// 1. First create the function
function get_first_letter($name = '') {
  // If no name is passed, try to get it from the Bricks context
  if (empty($name) && isset($GLOBALS['bricks_loop_context']['je_cct_reviews_reviewer_name'])) {
    $name = $GLOBALS['bricks_loop_context']['je_cct_reviews_reviewer_name'];
  }
  
  // Get first letter
  return !empty($name) ? mb_substr(trim($name), 0, 1) : '';
}

// 2. Then register it with Bricks (what you already have)
add_filter('bricks/code/echo_function_names', function() {
  return [
    'get_first_letter',
  ];
});

Step 2: add the tag in bricks editor

{echo:get_first_letter('{je_cct_reviews_reviewer_name}')}
1 Like