Home › Forums › Backend Issues (wp-admin) › acf_form() attachment to Media Gallery
Hi!
We use acf_form() for a front-end form that has attachments on it.
This works well but the attachment from it does not get “attached” on the post itself.
To see it attached, you can check by going to Media Gallery and check the column “Uploaded to” to the attachment you just uploaded. It will say that it is Unattached.
My question is :
How can I make the file uploaded from the acf_form() “attached” to the actual post?
To anyone having this problem, We did this and it works fine :
function acf_save_post( $post_id ) {
$file = get_field('upload_a_file', $post_id);
if( $file ){
$file_id = $file['ID'];
$post_to_update = array(
'ID' => $file_id,
'post_parent' => $post_id
);
wp_update_post( $post_to_update );
}
}
add_action('acf/save_post', 'acf_save_post', 20);
Replace upload_a_file with your correct acf field.
Basically we just modify the actual file post’s ‘post_parent’ to where it’s uploaded to. The attached file from acf will now show attached to the correct parent post.
Hi,
I added your code to my functions.php but I got this error on the site:
“Fatal error: Cannot declare acf_save_post() (previously declared in /home/…/plugins/advanced-custom-fileds-pro/core/input.php:436) in ……..
Did I put the code in the wrong place, could I just rename the function?
Thanks for your help 🙂
Philippe
Hi Philippe,
You might need to rename the actual function.
Try to change it to something like my_acf_save_post()
Don’t forget to update the add_action line as well.
Hi,
I did try renaming the function (also in the add_action) but it does nothing. When I select the image from the backend it does get attached by default but from the front end, it does not work. I mean, the image does get saved but it’s not attached to any post.
This is the code I used:
function pb3d_acf_save_post( $post_id ) {
$file = get_field('photo_athlete', $post_id);
if( $file ){
$file_id = $file['ID'];
$post_to_update = array(
'ID' => $file_id,
'post_parent' => $post_id
);
wp_update_post( $post_to_update );
}
}
add_action('acf/save_post', 'pb3d_acf_save_post', 20);
Thanks for your help 🙂
Philippe
Hi again,
This is the code I used to create the frontend form:
$options = array(
'id' => 'photo-athlete',
'fields' => array( 'photo_athlete' ),
'form' => true,
'submit_value' => __("Sauvegarder votre photo", 'acf'),
'instruction_placement' => 'field',
'updated_message' => false,
'uploader' => 'wp'
);
acf_form( $options );
Thanks
Philippe
Hi Philippe,
I forgot to tell you that this only works if the field is returning an array because it needs an id.
Your field should look like :
Try to change this on your acf field.
After this, you might need to change what’s on your page templates if you were using url or id before.
Hi,
Thanks so much for that. It now works perfectly. 🙂
Philippe
Hello.
This code works like a charm.
I use this code for the featured image.
How can i make this work for a repeater field in the same form?
The topic ‘acf_form() attachment to Media Gallery’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.