Getting field values from Settings page
Support › MB Settings Page › Getting field values from Settings pageResolved
- This topic has 7 replies, 2 voices, and was last updated 4 years ago by
Sridhar.
-
AuthorPosts
-
March 29, 2021 at 8:34 AM #26783
Sridhar
ParticipantI 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?
March 29, 2021 at 8:41 AM #26784Sridhar
ParticipantForgot to add that I see the fields on the settings page and populated them incl. uploading an image to the logo field.
March 29, 2021 at 10:54 AM #26787Long Nguyen
ModeratorHi 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 issettings_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>'; }
March 29, 2021 at 11:04 AM #26788Sridhar
ParticipantI 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'];
March 29, 2021 at 11:32 AM #26789Long Nguyen
ModeratorHi,
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.
March 29, 2021 at 12:05 PM #26790Sridhar
ParticipantThanks, it is clear now.
March 29, 2021 at 12:08 PM #26791Long Nguyen
ModeratorLet me know if you have any questions 🙂
April 1, 2021 at 5:48 AM #26846Sridhar
ParticipantAll good. Covered this in detail at https://wpdevdesign.com/settings-page-in-oxygen-using-meta-box/.
-
AuthorPosts
- You must be logged in to reply to this topic.