Getting an Image from a Setting Page Value
Support › MB Settings Page › Getting an Image from a Setting Page Value
- This topic has 1 reply, 2 voices, and was last updated 5 years, 11 months ago by
Anh Tran.
-
AuthorPosts
-
May 16, 2019 at 10:40 PM #14592
Mark Tenney
ParticipantHello, Ahn!
I'm working on a settings page and would like to get a value from the settings page that I can display using a shortcode. I have the shortcode working properly when I manually input the image URL, but I can't get it to pull the image from the MB Settings Page. I think I just am not quite comfortable enough with PHP to know what I could be doing wrong. Here's what I have:
SHORTCODE:
// Shortcode for organization icon and name in dashboard sidebar [dgtl_admin_menu_profile] function dgtl_admin_menu_profile_function( $atts = array() ) { // set up default parameters extract(shortcode_atts(array( 'icon' => 'https://digitalchurchplatform.com/wp-content/uploads/2019/02/PlatformSquareIcon.svg' ), $atts)); $org = get_bloginfo( 'name' ); $icon = rwmb_meta( $logo, array( 'object_type' => 'setting' ), $dgtl_settings ); return "<img src=\"$icon\" alt=\"organization-icon\" width=\"64\" height=\"64\" class=\"dgtl-admin-menu-profile\" />$org"; } add_shortcode('dgtl_admin_menu_profile', 'dgtl_admin_menu_profile_function');
And here's the SETTINGS PAGE code:
// Register meta boxes and fields for settings page add_filter( 'rwmb_meta_boxes', 'prefix_options_meta_boxes' ); function prefix_options_meta_boxes( $meta_boxes ) { $meta_boxes[] = array( 'id' => 'general', 'title' => 'General', 'settings_pages' => 'dgtl_settings', 'tab' => 'general', 'fields' => array( array( 'name' => 'Logo', 'id' => 'logo', 'type' => 'file_input', 'std' => 'https://digitalchurchplatform.com/wp-content/uploads/2019/02/PlatformSquareIcon.svg' ), array( 'name' => 'Layout', 'id' => 'layout', 'type' => 'image_select', 'options' => array( 'sidebar-left' => 'https://i.imgur.com/Y2sxQ2R.png', 'sidebar-right' => 'https://i.imgur.com/h7ONxhz.png', 'no-sidebar' => 'https://i.imgur.com/m7oQKvk.png', ), ), ), ); $meta_boxes[] = array( 'id' => 'colors', 'title' => 'Colors', 'settings_pages' => 'dgtl_settings', 'tab' => 'design', 'fields' => array( array( 'name' => 'Heading Color', 'id' => 'heading-color', 'type' => 'color', ), array( 'name' => 'Text Color', 'id' => 'text-color', 'type' => 'color', ), ), ); $meta_boxes[] = array( 'id' => 'info', 'title' => 'Theme Info', 'settings_pages' => 'dgtl_settings', 'tab' => 'faq', 'fields' => array( array( 'type' => 'custom_html', 'std' => 'Having questions? Check out our documentation', ), ), ); return $meta_boxes; }
(Most of that settings page code is just what I've pasted from your example.)
Thank you for your help!
May 17, 2019 at 4:12 PM #14608Anh Tran
KeymasterHi Mark,
When you use an image field like
image_advanced
, the return value from the helper function is an array of full info for the image, such as URL, alt text, width and height. See this for full details.You need to change your code to:
return "<img src=\"{$icon['url']}\" alt=\"organization-icon\" width=\"64\" height=\"64\" class=\"dgtl-admin-menu-profile\" />$org";
-
AuthorPosts
- You must be logged in to reply to this topic.