I’m trying to allow users to create an event (custom post type) on the front end of a website. Everything is updating, but I want to let them upload images and attach them to the ACF Gallery field for that event.
So rar I have this:
<input type="file" name="upload_attachment[]" class="files" size="50" multiple="multiple" />
and:
if (!function_exists('wp_generate_attachment_metadata')) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}
if ($_FILES) {
$files = $_FILES['upload_attachment'];
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]
);
$attachment_id = media_handle_upload($key, $post_id);
if ( is_wp_error( $attachment_id ) ) {
echo('error <br/>');
} else {
echo('success!<br />');
}
var_dump($attachment_id);
}
}
But, it doesn’t seem to be working. Basically I want the image to be uploaded to the media library, and then insert the image into the ACF Gallery Field.
But I think the first step is getting it into the media library.
Okay, I managed figure it out, incase anyone else needs to do something like this.
On the front end:
And in the php file I post the form to, I have this:
This script now allow multiple files to be uploaded to the server, attached to the post being created and added to the ACF Gallery field.