Support Forum
Support › MB Settings Page › can't retrieve settings page, group field single image data/value/image url
Hi,
my settings page is as follows:
<?php
add_filter( 'mb_settings_pages', 'your_prefix_function_name' );
function your_prefix_function_name( $settings_pages ) {
$settings_pages[] = [
'menu_title' => __( 'EV Advertisements', 'your-text-domain' ),
'option_name' => 'ev_advertisements',
'position' => 71,
'style' => 'no-boxes',
'columns' => 1,
'tabs' => [
'Home Page' => 'Home Page Ads',
],
'icon_url' => 'dashicons-format-video',
];
return $settings_pages;
}
and custom fields as follows:
<?php
add_filter( 'rwmb_meta_boxes', 'your_prefix_function_name' );
function your_prefix_function_name( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Home Page Ads', 'your-text-domain' ),
'id' => 'home-page-ads',
'settings_pages' => ['ev-advertisements'],
'tab' => 'Home Page',
'fields' => [
[
'name' => __( 'Home Page Top Ad', 'your-text-domain' ),
'id' => $prefix . 'home_page_top_ad_group',
'type' => 'group',
'fields' => [
[
'name' => __( 'Home Page Top Ad (728x90)', 'your-text-domain' ),
'id' => $prefix . 'home_page_top_ad',
'type' => 'single_image',
'image_size' => 'large',
'tooltip' => [
'icon' => 'info',
'position' => 'top',
'content' => 'Please upload 728x90 size in png, jpg or gif format',
],
],
[
'name' => __( 'Home Page Top Ad URL', 'your-text-domain' ),
'id' => $prefix . 'home_page_top_ad_url',
'type' => 'url',
'required' => true,
'tooltip' => [
'icon' => 'info',
'position' => 'top',
'content' => 'Please add the Full URL (including "https://" ) that should redirect on click of the Ad Image',
],
],
],
],
[
'name' => __( 'Home Page Side Banner Ad', 'your-text-domain' ),
'id' => $prefix . 'home_page_side_banner_ad',
'type' => 'group',
'fields' => [
[
'name' => __( 'Home Page Side Banner Ad Image (300x250)', 'your-text-domain' ),
'id' => $prefix . 'home_page_side_banner_ad_image',
'type' => 'single_image',
'tooltip' => [
'icon' => 'info',
'position' => 'top',
'content' => 'Please upload 300x250 size in png, jpg or gif format',
],
],
[
'name' => __( 'Home Page Side Banner Ad URL', 'your-text-domain' ),
'id' => $prefix . 'home_page_side_banner_ad_url',
'type' => 'url',
'tooltip' => [
'icon' => 'info',
'position' => 'top',
'content' => 'Please add the Full URL (including "https://" ) that should redirect on click of the Ad Image',
],
],
],
],
[
'name' => __( 'Home Page Bottom Ad', 'your-text-domain' ),
'id' => $prefix . 'home_page_bottom_ad',
'type' => 'group',
'fields' => [
[
'name' => __( 'Home Page Bottom Ad (728x90)', 'your-text-domain' ),
'id' => $prefix . 'home_page_bottom_ad',
'type' => 'single_image',
'image_size' => 'large',
],
[
'name' => __( 'Home Page Bottom Ad URL', 'your-text-domain' ),
'id' => $prefix . 'home_page_bottom_ad_url',
'type' => 'url',
'required' => true,
],
],
],
],
];
return $meta_boxes;
}
Now I'm trying to get the URL of home_page_top_ad or home_page_bottom_ad or home_page_side_banner_ad_image like:
<?php $images = rwmb_meta( 'home_page_top_ad', ['size' => 'thumbnail'] ); ?>
if (is_array($values) || is_object($values)) {
foreach ( $images as $image ) {
echo '<img src="', $image['url'], '">';
}
}
I want each image URL so I can output these in different locations on the same page.
But it is not outputting any data, please help.
sorry in the last code the if part was given wrong
I was trying like follows:
<?php $images = rwmb_meta( 'home_page_top_ad', ['size' => 'thumbnail'] ); ?>
if (is_array($images) || is_object($images)) {
foreach ( $images as $image ) {
echo '<img src="', $image['url'], '">';
}
}
How can I get the image URL from the settings gape field group?
Hi,
To get the field value from the settings page, you can use this sample code
$value = rwmb_meta( $field_id, ['object_type' => 'setting'], $option_name );
echo $value;
To get a single image from the group, you can use this sample code
$group = rwmb_meta( 'group_id' );
$image_id = $group['image_key'];
$image = RWMB_Image_Field::file_info( $image_id, ['size' => 'thumbnail'] );
echo '<img src="' . $image['url'] . '">';
In your case, the code would be
$group = rwmb_meta( 'home_page_top_ad_group', ['object_type' => 'setting'], 'ev_advertisements' );
$image_id = $group['home_page_top_ad'];
$image = RWMB_Image_Field::file_info( $image_id, ['size' => 'thumbnail'] );
echo '<img src="' . $image['url'] . '">';
Read more on the documentation
https://docs.metabox.io/extensions/mb-settings-page/#getting-field-value
https://docs.metabox.io/extensions/meta-box-group/#sub-field-values
Sorry to bother you, but can you please give me the code to get the URL value also?
I want the home_page_top_ad_url also from the settings page group.
I'm trying in two ways, one is:
$group_value = rwmb_meta( 'home_page_top_ad_group' );
$value = isset( $group_value['home_page_top_ad_url'] ) ? $group_value['home_page_top_ad_url'] : '';
and
$url = rwmb_meta( $group['home_page_top_ad_url'], ['object_type' => 'setting'], 'ev_advertisements' );
In both ways, I'm not getting the value. Please help.
Hi,
You need to get the group value from the settings page first, and access the subfield later.
$group = rwmb_meta( 'home_page_top_ad_group', ['object_type' => 'setting'], 'ev_advertisements' );
$url = $group['home_page_top_ad_url'];