Trouble with file upload in a group
- This topic has 5 replies, 2 voices, and was last updated 3 years, 10 months ago by
leebazCDS.
-
AuthorPosts
-
June 17, 2021 at 2:01 AM #28952
leebazCDS
ParticipantI'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.
June 17, 2021 at 9:37 PM #28967Long Nguyen
ModeratorHi 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
June 18, 2021 at 12:12 AM #28973leebazCDS
ParticipantHi,
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.June 18, 2021 at 5:11 PM #28983Long Nguyen
ModeratorHi,
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
June 19, 2021 at 2:58 AM #28996leebazCDS
ParticipantThanks - I'll give that a go too!
June 19, 2021 at 3:29 AM #28997leebazCDS
ParticipantHi - 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>'; } }
-
AuthorPosts
- You must be logged in to reply to this topic.