Published on April 3, 2021

Getting URL of a Group’s image field from Settings page in Meta Box

Sridhar Katakam

In the previous tutorial I showed how we can get the URL of an image field from a Settings page when using Meta Box. This tutorial shows how the same can be done when the image field is inside a group.

FYI: Meta Box is currently running a LTD (lifetime deal) sale at Appsumo. The complete package for unlimited sites for lifetime access with support costs $150. Get Meta Box (affiliate link)

Option name of the Settings page: my_options

Field ID Prefix set for the field group attached to the Settings page: sitewide_

ID of the Group field: branding

ID of the Single Image-type field inside the branding group: logo

Here’s the custom function:

/* Get the URL of group's logo image from the settings page */
function wpdd_get_mb_settings_group_logo_url() {
	// Array of sitewide_branding custom field of the my_options Settings page.
	// Ex.: Array ( [sitewide_logo] => 372 )
	$group_value = rwmb_meta( 'sitewide_branding', ['object_type' => 'setting'], 'my_options' );
	// Value of the sitewide_logo field (image ID in this case).
	// Ex.: 372
	$logo = isset( $group_value['sitewide_logo'] ) ? $group_value['sitewide_logo'] : '';
	// Get the array from image ID.
	// Ex.:
	$logo = RWMB_Image_Field::file_info( $logo );

	// Return URL of the logo image.
	return $logo ? $logo['url'] : 'https://oxygen.local/wp-content/uploads/2021/03/placeholder-logo.png';

When using Oxygen, this can be used via Dynamic Data > PHP Function Return value by setting wpdd_get_mb_settings_group_logo_url as the Function Name.

Replace https://oxygen.local/wp-content/uploads/2021/03/placeholder-logo.png with the URL of a fallback image from your media library.