Populate Bricksforge select field with JetEngine glossary data

David Denedo

Originally published on . Updated on .

Table of Content

Introduction

One unique advantage WordPress offers is the ability to create maintainable websites by reusing content. If you have a massive list of options you want to reuse in different forms, JetEngine —a powerful WordPress plugin for managing custom post types and custom fields—has a glossary feature that allows you to save large lists of data (either manually or via CSV/JSON) to be reused in form select fields. For example, you can save a list of countries, affiliate links or businesses and reuse them in multiple forms without creating separate options pages.

Although Bricksforge supports directly uploading JSON data to the array fields, JetEngine glossary gives you a better UI to manage the JSON data with the added advantage of being reusable in multiple forms and pages.

In this tutorial, you’ll learn how to retrieve data from the JetEngine glossary and use it in a Bricksforge Pro Form.

Requirements

  • Bricks Builder
  • Bricksforge
  • JetEngine

Step-by-step guide

Create a JetEngine glossary

  1. Navigate to WordPress Dashboard → JetEngine → Glossaries.
  2. Create a new glossary with your list of options.
  3. After saving, note the ID of your newly created glossary post.
JetEngine dashboard showing Glossaries section with three items: Nationality (ID: 6), Names (ID: 24), and Nigerian States (ID: 26), plus a New Glossary button.

Add the code snippets

Using your preferred code snippets plugin (such as WPCodeBox ) or your Bricks child theme’s functions.php file, add the following two code snippets:

Snippet 1: Glossary Retrieval Function

<?php 

/**
 * Get meta fields from jet_post_types as JSON
 * 
 * @param int $id The ID of the jet_post_type to retrieve meta fields from
 * @return string JSON string containing label and value pairs
 */
function get_glossary_json($id) {
    global $wpdb;
    
    // Sanitize the ID input
    $id = intval($id);
    
    // Return error if no valid ID provided
    if ($id <= 0) {
        return json_encode(array('error' => 'Please provide a valid ID.'));
    }
    
    // Get the meta_fields from the database
    $meta_fields_serialized = $wpdb->get_var(
        $wpdb->prepare(
            "SELECT meta_fields FROM {$wpdb->prefix}jet_post_types WHERE id = %d",
            $id
        )
    );
    
    // Return error message if no data found
    if (empty($meta_fields_serialized)) {
        return json_encode(array('error' => 'No meta fields found for ID: ' . $id));
    }
    
    // Try to unserialize the data
    $meta_fields = @unserialize($meta_fields_serialized);
    
    // Check if unserialization was successful
    if ($meta_fields === false) {
        return json_encode(array('error' => 'Error processing meta fields data.'));
    }
    
    // Create simplified array with just label and value
    $simplified_fields = array();
    foreach ($meta_fields as $field) {
        $simplified_fields[] = array(
            'label' => $field['label'],
            'value' => $field['value']
        );
    }
    
    // Return as JSON
    return json_encode($simplified_fields, JSON_PRETTY_PRINT);
}

/**
 * Usage example:
 * 
 * Echo the JSON directly (e.g. in a template file)
 * echo get_glossary_json(8);
 * 
 */

Snippet 2: Whitelist Echo Function for Bricks

<?php 

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

Set up the Bricksforge Pro Form

  1. Create a new Bricksforge Pro Form.
  2. Add your desired form fields, including the field you want to populate with glossary data (Select, Checkbox Wrapper, or Radio Wrapper).
  3. For the array field you want to populate:
    • Delete the nested option element.
    • Go to the Options panel in the parent element.
    • Change the Populate option to Dynamic Data
    • Enter the following dynamic data: (Replace ’26’ with your actual glossary ID)
Bricks structure panel showing the Bricksforge pro form nested elements including a select field
Bricksforge Pro Form in Structure Panel
Bricksforge select field element settings showing the Options section with Dynamic Data selected, containing the echo function to populate select field options.
Bricksforge select element populate option

Test your form

Preview your page to verify that the form field is properly populated with your glossary data.

How it works

  1. The first code snippet creates a function that:
    • Retrieves the glossary data from the JetEngine database table
    • Processes the data into a simple array of label/value pairs
    • Returns the data as JSON
  2. The second code snippet whitelists our function so it can be used with Bricks’ functionality
  3. When the form loads, Bricksforge retrieves the JSON data through the dynamic data function and populates the dropdown options automatically

Troubleshooting

  • If your select field appears empty, double-check your glossary ID.
  • Ensure both code snippets are properly added and active.
  • Verify that your JetEngine glossary contains data

Final thoughts

Combining JetEngine’s glossary feature with Bricksforge forms can help you create reusable, maintainable form elements that pull from a central data source. This approach is especially valuable for sites that need consistent options across multiple forms.

Leave the first comment