php - How to programmatically add photos to cmb2 meta field type file_list from front-end with multiple file inputs
Get the solution ↓↓↓My code to create the CMB2 field
$cmb->add_field( array(
'name' => 'Qualification List',
'desc' => 'Candidate qualifications',
'id' => 'qualification_file_list',
'type' => 'file_list',
'preview_size' => arraay( 100, 100 ), // Default: array( 50, 50 )
'text' => array(
'add_upload_files_text' => 'Upload Qualifications', // default: "Add or Upload Files"
'remove_image_text' => 'Remove Image', // default: "Remove Image"
'file_text' => 'File', // default: "File:"
'file_download_text' => 'Download', // default: "Download"
'remove_text' => 'Remove', // default: "Remove"
),
) );
HTML code
<div class="form-group">
<label for="avatar">Upload your certificates:</label>
<input type="file" id="qualifications" name="qualifications[]" accept="image/png, image/jpeg" multiple><br>
<?php wp_nonce_field( 'qualifications', 'my_image_upload_nonce' ); ?>
<span class="qualification-error-message"></span>
</div>
My code to upload images to the media library and attach images to the CMB2 file list
if ( ! empty( $_FILES['qualifications'] ) ) {
$files = $_FILES['qualifications'];
$new_file_img = [];
foreach ($files['name'] as $key => $value){
if ($files['name'][$key]){
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
}
$_FILES = array("qualifications" => $file);
$i=1;
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
foreach ($_FILES as $file => $array) {
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) __return_false();
$attachment_id = media_handle_upload($file, $acceptanceId);
$new_file_img = wp_get_attachment_image( $attachment_id );
$i++;
}
echo $new_file_img;
update_post_meta($acceptanceId, 'qualification_file_list', $new_file_img);
}
}
I was able to get the files to upload to the WordPress media library where it says the file was uploaded to the post but the file is not attached to the field type.
image uploading using the code above
Answer
Solution:
the solution is :
Array :
$files[$attach_id] = $movefile["url"];
and update like this :
update_post_meta( $post_id, 'gallery', $files);
I found the solution here https://github.com/CMB2/CMB2/issues/1321
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the requested url was not found on this server. xampp
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.