Custom Fields
Re-Envisioned
Support MB Settings Page Getting field values from Settings page

  • Creator
    Topic
  • #26783
    Resolved Sridhar
    Participant

    I am probably missing something obvious but can't get the URL of an image-type field from a Settings page to appear on the front end.

    Settings page:

    <?php
    add_filter( 'mb_settings_pages', 'custom_settings_page' );
    
    function custom_settings_page( $settings_pages ) {
        $settings_pages[] = [
            'menu_title'  => __( 'Options', 'project-name' ),
            'option_name' => 'my_options',
            'position'    => 25,
            'columns'     => 1,
            'icon_url'    => 'dashicons-admin-generic',
        ];
    
        return $settings_pages;
    }

    Field group:

    <?php
    add_filter( 'rwmb_meta_boxes', 'custom_sitewide_fields_group' );
    
    function custom_sitewide_fields_group( $meta_boxes ) {
        $prefix = 'settings_';
    
        $meta_boxes[] = [
            'title'          => __( 'Sitewide', 'project-name' ),
            'id'             => 'sitewide',
            'settings_pages' => ['my-options'],
            'fields'         => [
                [
                    'name'              => __( 'Logo', 'project-name' ),
                    'id'                => $prefix . 'logo',
                    'type'              => 'image',
                    'label_description' => __( 'Upload/select site\'s logo image', 'project-name' ),
                    'max_file_uploads'  => 1,
                ],
                [
                    'name'              => __( 'Notice', 'project-name' ),
                    'id'                => $prefix . 'notice',
                    'type'              => 'wysiwyg',
                    'label_description' => __( 'If present, will appear above the site header. Can be used for specials and promotional messages.', 'project-name' ),
                ],
                [
                    'name' => __( 'Button Background Color', 'project-name' ),
                    'id'   => $prefix . 'button_background_color',
                    'type' => 'color',
                ],
            ],
        ];
    
        return $meta_boxes;
    }

    Here's the code that shows just blank output:

    <?php
    $value = rwmb_meta( 'logo', ['object_type' => 'setting'], 'my_options' );
    echo $value['url'];
    ?>

    Any idea?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Author
    Replies
  • #26784
    Sridhar
    Participant

    Forgot to add that I see the fields on the settings page and populated them incl. uploading an image to the logo field.

    #26787
    Long Nguyen
    Moderator

    Hi Sridhar,

    Thank you for reaching out.

    There are some problems with your code when registering the settings page, meta box, and output the field image value, please follow these steps:

    • the settings page should have an ID
    $settings_pages[] = [
            'menu_title'  => __( 'Options', 'project-name' ),
            'option_name' => 'my_options',
            'id'        => 'my-options',
            'position'    => 25,
            'columns'     => 1,
            'icon_url'    => 'dashicons-admin-generic',
        ];
    • when registering the meta box, the key settings_page has to assign to option name
     $meta_boxes[] = [
            'title'          => __( 'Sitewide', 'project-name' ),
            'id'             => 'sitewide',
            'settings_pages' => 'my_options',
            'fields'         => [
                ...
            ],
        ];
    • the field logo has the prefix concatenated so the right ID of it is settings_logo
    • the field image is set to multiple, so you should use the loop to iterate through the array of images to get the image information. Get more details here https://docs.metabox.io/fields/image/#template-usage

    the code should be:

    $images = rwmb_meta( 'settings_logo', ['object_type' => 'setting'], 'my_options' );
    
    foreach ( $images as $image ) {
        echo '<a href="', $image['full_url'], '"><img src="', $image['url'], '"></a>';
    }
    #26788
    Sridhar
    Participant

    I am actually visually setting up the Settings page and the field group (and fields). The code is something I copied and pasted by clicking the "Get PHP Code" button.

    Settings page screenshot: https://d.pr/i/DLS3e6

    Field group screenshot: https://d.pr/i/RmnsoZ

    Settings page fields populated: https://d.pr/i/CMZMSs

    I currently have "Max number of files" set to 1 for the Image-type field.

    The following code isn't showing any output:

    $value = rwmb_meta( 'settings_logo', ['object_type' => 'setting'], 'my_options' );
    echo $value['url'];
    #26789
    Long Nguyen
    Moderator

    Hi,

    The setting "Max number of files" only limits the user to upload one file, the data still returns an array of images.

    You can use the function reset() to get the first-one array element.

    $images = rwmb_meta( 'settings_logo', ['object_type' => 'setting'], 'my_options' );
    $image = reset( $images );
    ?>
    <img src="<?php echo $image['url']; ?>">
    

    See more on this docs https://docs.metabox.io/fields/image/#template-usage.

    #26790
    Sridhar
    Participant

    Thanks, it is clear now.

    #26791
    Long Nguyen
    Moderator

    Let me know if you have any questions 🙂

    #26846
    Sridhar
    Participant
Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.