Get a custom fields value from a settings page

Support MB Settings Page Get a custom fields value from a settings pageResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35202
    Aaron KesslerAaron Kessler
    Participant

    I can remember I did run into this before, but I do not know how I solved it.
    There must be something obvious I am missing. I used Meta Box AIO for code generation and did look at https://docs.metabox.io/extensions/mb-settings-page/#getting-field-value, but I can't see anything wrong.

    I did a bare-bones theme for testing:

    functions.php

    
    <?php
    
    //all code is generated from Meta Box AIO
    
    add_filter('rwmb_meta_boxes', 'custom_fields');
    
    function custom_fields($meta_boxes)
    {
        $prefix = '';
    
        $meta_boxes[] = [
            'title' => __('custom_field_title', 'your-text-domain'),
            'id' => 'custom_field_title',
            'settings_pages' => ['settings_page_titel'],
            'fields' => [
                [
                    'name' => __('Text', 'your-text-domain'),
                    'id' => $prefix.'custom_field',
                    'type' => 'text',
                ],
            ],
        ];
    
        return $meta_boxes;
    }
    
    add_filter('mb_settings_pages', 'custom_settingspage');
    
    function custom_settingspage($settings_pages)
    {
        $settings_pages[] = [
            'menu_title' => __('settings_page_titel', 'your-text-domain'),
            'option_name' => 'settings_page',
            'position' => 25,
            'style' => 'no-boxes',
            'columns' => 1,
            'icon_url' => 'dashicons-admin-generic',
        ];
    
        return $settings_pages;
    }
    

    index.php

    
    <?php
    
    get_header();
    
    $value = rwmb_meta('settings_page', ['object_type' => 'setting'], 'custom_field');
            var_dump($value);
    
    get_footer();
    

    I would expect the output to be:
    .../wp-content/themes/test/index.php:12:string 'lorem'

    instead I get:
    .../wp-content/themes/test/index.php:12:boolean false

    #35229
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Please swap field ID and option name in your code. The format to get the setting's value is

    $value = rwmb_meta( $field_id, ['object_type' => 'setting'], $option_name );
    echo $value;

    it should be

    $value = rwmb_meta( 'custom_field', ['object_type' => 'setting'], 'settings_page' );
    echo $value;
    #35234
    Aaron KesslerAaron Kessler
    Participant

    Obvious 🙄, thank you!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.