black friday sale

When creating a custom meta box, you also create a list of custom fields, each of which is an array of field settings. You probably will need to refer to field's name, field's options array (in a select field) or other settings later in your code. So, how to get the field settings array by field ID?

The Meta Box plugin has a helper function for that. It's called rwmb_get_field_settings(). It's been being used for the internal purpose, but I think it's also helpful for other developers, too.

To get field settings by field ID, just call:

$field = rwmb_get_field_settings( $field_id );
// Result: array( 'id' => 'field_id', 'name' => 'Field Name', 'type' => 'text, ... );

However, if you create a custom field for terms, user profile or a settings page, you will need to specify object type in the second parameter:

$field = rwmb_get_field_settings( $field_id, array( 'object_type' => 'term' ) ); // or 'user', 'setting

As you might see, the 2nd parameter is very similar to the 2nd parameter in the rwmb_meta() helper function if you use it to get meta value for terms, user profile or settings page. That's because the function rwmb_get_field_settings() is used internally inside the helper function to find field settings.

Finally, there's an edge case that you create 2 custom fields with the same ID but different settings for 2 different custom post types. In this case, to get the correct field settings, you need to specify the post ID in the last parameter:

$field = rwmb_get_field_settings( $field_id, '', $post_id );

It's the same if you create 2 custom fields with the same ID for 2 taxonomies or 2 settings pages. In that case, in addition to the 3rd parameter (which should be term ID or settings page ID), you need to specify the object type in the 2nd parameter, just like you did above.

The general syntax for the function is:

$field = rwmb_get_field_settings( $field_id, $args, $object_id );

Example

The example below is a real use-case of the rwmb_get_field_settings() that a user asked me. He had a group of some text fields and wanted to list all the sub-fields in the format Field Name: Field Value. Without this function, he needed to manually type the sub-field name and ID. But now he can do it automatically.

<?php
// Function to get the sub-field values from groupped meta fields.
function prefix_meta($group, $field){
    $group = rwmb_meta($group);
    $value = isset($group[$field]) ? $group[$field] : '' ;
    return $value;
}
// Function to dynamically display group based meta information.
function prefix_show_meta_info($group){
    $grp = rwmb_get_field_settings($group);
    $grpsize = rwmb_meta($group);
    for ($i=0; $i < count($grpsize); $i++) { ?>
        <div class="sub-field"><?= $grp['fields'][$i]['name'].": "; ?><?= prefix_meta($group,$grp['fields'][$i]['id']); ?></div>
    <?php }
}

// Call: prefix_show_meta_info('group_id')

Using rwmb_get_field_settings(), you don't have to remember the field's name anymore. Your code will be minimal and clear.

So, if you need to refer to the field settings, this function is all you need. If you have any question, please let me know in the comments or open a new support ticket.

Leave a Reply

Your email address will not be published. Required fields are marked *