wpmetabox / meta-box

The best plugin for WordPress custom fields and custom meta boxes
https://metabox.io
1.19k stars 423 forks source link

Obtain the metadata of a MetaBox setting. #1575

Closed prconcepcion closed 5 months ago

prconcepcion commented 6 months ago

Issue Overview

This is not really an issue but more of a question. So when I do something like this:

$field_registry = rwmb_get_registry( 'field' );
$fields = $field_registry->get_by_object_type( 'setting' );

$fields would return an array of settings with fields it contains. The key of each setting is the option_name. Is there any way I can obtain the metadata of the setting through the option_name? Because I wanna display the setting's label.

rilwis commented 5 months ago

Hi @prconcepcion ,

Do you mean "metadata" = the settings page's settings or the field settings?

prconcepcion commented 5 months ago

@rilwis, sorry I was not clear but I meant settings page's settings

prconcepcion commented 5 months ago

If there is also a way to obtain the settings page's settings through the setting's page id, that would also be nice.

rilwis commented 5 months ago

@prconcepcion

This is a quick & dirty snippet that you can use to get the settings page's settings from option name or ID:

$settings_pages = apply_filters( 'mb_settings_pages', [] );
foreach ( $settings_pages as $sp ) {
    // if ( $sp['id'] == $id ) {
    if ( $sp['option_name'] == $option_name ) {

        // Do something with $sp
    }
}

You can extract it into a helper function to reuse it later.