ACF Image
-
First off, this plugin has been a lifesaver. Thanks so much!
I have an ACF image field that returns the image ID. But when I use the upload file field to update the image it just removes the image and doesn’t replace it.
-
Hi cwoody1980, in last version I added some small stuff for compatibility for ACF. You can specify to save image as Post ID by adding this to the target field name in mapping:
field_name<post_id>
There is still gonna be a small issue if that is an ACF field, as format of WP meta and ACF fields aren’t fully compatible. I will release new version this week that allows to force saving in ACF field format. Please lookout for version 1.1.5, it will support field_name<post_id|acf> .I already have the code for my own projects, just need to find time to make WP release.
I’m looking forward to the update. Also, is there a way to prevent the content being updated each time?
@cwoody1980, i had a similar need to yours. Instead of using the plugin though, I used Gravity Form’s
gform_after_submissionhook.
Basically the code below grabs the file and post id from the submission, uploads the file to the media library, then updates the ACF field using ACF’supdate_fieldfunction.<?php add_action('gform_after_submission', 'update_acf_file_from_gravity_form_with_attachment', 10, 2); function update_acf_file_from_gravity_form_with_attachment($entry, $form) { // check if it's the correct form if ($form['id'] != 4) return; // Specify the ID of the file upload field $file_field_id = '21'; $file_url = $entry[$file_field_id]; // var_dump($entry); // Specify post ID field $post_id_field_id = '15'; $post_id = $entry[$post_id_field_id]; // Ensure a file URL and post ID are provided if (!empty($file_url) && !empty($post_id)) { // Proceed to upload the file to the WordPress Media Library $attachment_id = upload_file_to_media_library($file_url, $post_id); if ($attachment_id) { var_dump($attachment_id); var_dump($post_id); // Use the ACF update_field function to update the file field with the attachment ID $field_key = 'rental_pricing'; update_field($field_key, $attachment_id, $post_id); } } } function upload_file_to_media_library($file_url, $post_id) { require_once(ABSPATH . 'wp-admin/includes/image.php'); require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); // Download file to temp location $tmp = download_url($file_url); // Check for download errors if (is_wp_error($tmp)) { // Handle error return false; } $file_array = array(); // Set variables for storage // Extract filename and extension from the URL $file_array['name'] = basename($file_url); // Prepare an array for the attachment $file_array['tmp_name'] = $tmp; // Upload the file to the Media Library $id = media_handle_sideload($file_array, $post_id); // Check for handle sideload errors. if (is_wp_error($id)) { @unlink($file_array['tmp_name']); // Clean up return false; } return $id; // Return the attachment ID }Yeah that’s what I ended up doing. Thanks!
The topic ‘ACF Image’ is closed to new replies.