I'm working with the Metabox plugin in a headless Wordpress setup with a React frontend, utilizing cloneable group fields inside a custom fields to create like an array of data (in this case an image field and text field). I'm able to achieve this without any code and just using the wordpress UI. It works fine but as common with the Metabox Group, retreiving the image url from the endpoint returns an ID instead of the actual image url.
With some research, I've tried retrieving the url by passing this function in my functions.php file :
add_action( 'rest_api_init', function() {
register_rest_route( 'custom/v1', '/image-urls', array(
'methods' => 'GET',
'callback' => 'get_image_urls',
) );
} );
function get_image_urls() {
$post_id = get_page_by_path( 'services-page' )->ID;
$image_urls = array();
// Get the meta value of the group field inside the custom field
$group_field_value = rwmb_meta( 'services_scroll_field', array(), $post_id );
// Check if the group field value is not empty
if ( ! empty( $group_field_value ) ) {
// Loop through the group field value
foreach ( $group_field_value as $group_field_item ) {
// Get the meta value of the image advanced field inside the group field
$image_field_value = $group_field_item['icon_select'];
// Check if the image field value is not empty
if ( ! empty( $image_field_value ) ) {
// Get the image URL
$image_url = wp_get_attachment_image_url( $image_field_value, 'full' );
// Add the image URL to the array
$image_urls[] = $image_url;
}
}
}
return $image_urls;
}
The problem with this implementation, is when I test the endpoint, I receive an array of "false" boolean which is the same number of expected items in the array so I know I'm on the right track. Rather, the expected result is to receive an array containing the image urls