Support Forum
Support › Meta Box Group › Trouble with file upload in a groupResolved
I'm having trouble using file upload in a group
My group meta is as follows:
array(
'name' => 'Right Hand Side Highlights', // Optional
'id' => 'overview_rhs_highlights',
'type' => 'group',
'clone' => true,
'sort_clone' => true,
// List of sub-fields
'fields' => array(
array(
'name' => 'Title',
'id' => 'overview_rhs_highlights_title',
'type' => 'text',
),
array(
'name' => 'Text',
'id' => 'overview_rhs_highlights_text',
'type' => 'textarea',
),
array(
'name' => 'Icon Upload',
'id' => 'overview_rhs_highlights_image',
'type' => 'file',
'force_delete' => true,
'max_file_uploads' => 1,
),
// Other sub-fields here
),
),
Which is creating the metaboxes on the front end.
However, when I upload a file, in this case and svg which is used as an icon, it looks like this in the DB:
a:2:{i:0;a:3:{s:29:"overview_rhs_highlights_title";s:5:"POWER";s:28:"overview_rhs_highlights_text";s:164:"The Rhoda AT comes with electric motor powered by our battery pack. With five levels of assist and the amazing cruise mode you’ll be sure to find your sweet spot.";s:36:"_index_overview_rhs_highlights_image";s:19:"_file_60ca3874b54af";}i:1;a:3:{s:29:"overview_rhs_highlights_title";s:5:"Range";s:28:"overview_rhs_highlights_text";s:105:"With more than enough range for your daily commute the Rhoda AT is the ideal way to get to and from work.";s:36:"_index_overview_rhs_highlights_image";s:19:"_file_60ca3874b575d";}}
It seems to have added '_index' to the ID
This is my current front end code, which I'm attempting to adopt from the examples in the documentation:
<?php
$highlights = rwmb_meta( 'overview_rhs_highlights' );
if ( ! empty( $highlights ) ) {
foreach ( $highlights as $highlight ) {
echo '<div class="contact">';
echo '<h4>', 'Contact info', '</h4>';
echo '<p><label>', 'Name:', '<label> ', $highlight['overview_rhs_highlights_title'], '</p>';
echo '<p><label>', 'Email:', '<label> ', $highlight['overview_rhs_highlights_text'], '</p>';
$files = $highlight['index_overview_rhs_highlights_image'];
echo $files;
foreach ( $files as $file ) {
?>
<a href="<?php echo $file['url']; ?>"><?php echo $file['name']; ?></a>
<?php
}
echo '</div>';
}
}
?>
The First fields are outputting fine, but I get nothing from the file upload.
Hi Lee,
The key _index_overview_rhs_highlights_image
is used to track the unique file. Please don't use it to get the field value, just the field ID.
$files = $highlight['overview_rhs_highlights_image'];
To get the file info in a group, please follow this documentation https://docs.metabox.io/extensions/meta-box-group/#sub-field-values
Hi,
Thanks for getting back to me.
Is there a problem with uploading SVG files as a file, not as an image?
When I try with a PDF or an image, it appears to work fine. But When i try with an SVG it doesn't.
Hi,
You can use this PHP code to allow uploading SVG file on your site
function cc_mime_types($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');
Get more details on this topic https://wordpress.stackexchange.com/questions/313951/how-to-upload-svg-in-wordpress-4-9-8
Thanks - I'll give that a go too!
Hi - I have made this work, but not exactly how described in the examples, so I thought I'd share just in case.
Since I only want one image i've used the 'single image' upload meta.
I found that this would output the image ID, so I used the following code to retreive and output it:
$highlights = rwmb_meta( 'overview_rhs_highlights' );
if ( ! empty( $highlights ) ) {
foreach ( $highlights as $highlight ) {
echo '<div class="col-md-12"><div class="d-row">';
$imageid = $highlight['overview_rhs_highlights_image'];
$image = wp_get_attachment_image_src( $attachment_id = $imageid );
echo '<div class="icon"><img class="img-fluid" src="', $image[0], '"></div>';
echo '<div class="text-group">';
echo '<h4>'.$highlight['overview_rhs_highlights_title'].'</h4>';
echo '<p>'.$highlight['overview_rhs_highlights_text'].'</p>';
echo '</div></div></div>';
}
}