Setting featured image upon form submit
-
Hi, I’m not a coder, so if you can help me with what code to write and where to put it, that would be great. I have created several ACF forms, and have gotten the forms to save and create new posts just fine… now all that is left is getting the featured images associated with the new posts. I just don’t know what coding I need to add to the SAVE action to accomplish this.
-
Hello,
Thanks for the feedback!
You can simply use the “Set as Featured Image” setting in the Image field that is displayed on the front-end form, it will assign the selected image to the post. See documentation.
Hope it helps!
Have a nice day!
Regards.
I don’t have an image field in the forms. They aren’t uploading anything, just filling out and submitting the form. So when they go view the resulting post, I need to have set a specific featured image from the media library already saved. I hadn’t thought about having a hidden image field with a fallback image already set. Can I do that?
Hello,
You should avoid using hidden fields when doing this kind of behind the scenes operations on the front-end, since hidden fields can be essentially altered by the visitor (which is not suitable). Instead you should write it in PHP.
If you’re using ACF Form, you can use the
acf/submit_formhook (undocumented). Usage example:add_action('acf/submit_form', 'my_acf_form_submit', 10, 2); function my_acf_form_submit($form, $post_id){ // Target "my-form" ID // Change this to your acf_form ID if($form['id'] !== 'my-form') return; // Your image attachment ID $attachment_id = 120; // Set post featured image update_post_meta($post_id, '_thumbnail_id', $attachment_id); }If you’re using ACFE Form, you can use the
acfe/form/submit/posthook (See documentation). Usage example:add_action('acfe/form/submit/post/form=my-form', 'my_acfe_form_submit', 10, 5); function my_acfe_form_submit($post_id, $type, $args, $form, $action){ // Your image attachment ID $attachment_id = 120; // Set post featured image update_post_meta($post_id, '_thumbnail_id', $attachment_id); }For further assistance with native ACF features like ACF Form, I would recommend to join the Official ACF Support forum. There is a large community of developers who may answer your questions here. You will also find useful topics & code snippets.
Hope it helps!
Regards.
Okay, making progress! Thank you for your patience! Don’t forget, I’m not a coder… so my first question is (blushing)… does this code go into the functions.php file or in the form actions in the form itself? Next… if I’m using acfe, so the second code you listed, I see I put in the image ID as the $attachment_id, but where do I specify the form? I have 15 forms, and each one has a different featured image I need to set. I will also join the form for future roadblocks. Thank you so much for your help with this!
Hello,
Yes those code snippets go into your theme
functions.phpfile. ACF Form & ACFE Form are different solutions, if you already implemented 15 forms with one solution, I would recommend to stick to it.If you’re using ACF Form, the form has to be targeted in the commented code:
// Target "my-form" ID // Change this to your acf_form ID if($form['id'] !== 'my-form') return;If you’re using ACFE Form, the form has to be targeted in the hook name:
add_action('acfe/form/submit/post/form=my-form-name-here', 'function', 10, 5);You can also use a wildcard hook like:
acfe/form/submit/postand just add conditions in it to avoid having 15 different functions. Usage example:// Wildcard hook: acfe/form/submit/post add_action('acfe/form/submit/post', 'my_acfe_form_submit', 10, 5); function my_acfe_form_submit($post_id, $type, $args, $form, $action){ if($form['name'] === 'my-form-1'){ $attachment_id = 120; update_post_meta($post_id, '_thumbnail_id', $attachment_id); }elseif($form['name'] === 'my-form-2'){ $attachment_id = 140; update_post_meta($post_id, '_thumbnail_id', $attachment_id); } // ... }As a side note, I would like to add that front-end form is an advanced & sensible web development concept. Far more complex that just publishing a web blog. As you’re not a developer, I would strongly recommend to read some beginners guides to PHP/WordPress development, since you need to understand important concepts like
hooksorPHP conditions.You shouldn’t blindly copy/paste code from forums (we all started there). Try to become a little more independent to be able to tweak & customize the code you found. It’s nothing impossible really, it just takes some trial & error, and all the knowledge is available for free on Internet!
Here are some resources you can begin with:
- Actions – WordPress Hooks Tutorial For Beginners
- Guide: All about Hooks in WordPress
- PHP Beginners Guide
You can also join the Stackoverflow WordPress community which will help you with most common WordPress & PHP questions.
If you don’t have the time to learn more about web development, and need quick answers, you could get in touch with an indie PHP developer who could mentor you with common questions and do the most advanced tasks you couldn’t achieve. There’s plenty of marketplaces with experienced developers who could fulfill that purpose, like Upwork or Codable.io to name a few.
Trust me, it’s for your good. I’m a self-taught developer myself π
Have a nice day!
Regards.
Yes! It’s working! Thank you so much for your time.
In my defense, I’ve actually been working in WordPress for 6 years and am proficient in Avada – something that doesn’t need prior PHP coding knowledge. But now I’m developing a pretty complex membership site using Elementor Pro, which I’ve never used before. I think you’d be impressed, given my lack of PHP knowledge! And I do have plans to dig in to coding in the next couple months… however, this project needs to be off my plate first π
Thank you again for all your help.
I’m glad to hear it now works!
Nice jump! I encourage you to keep going that way. PHP/WordPress dev is really a wonderful world, full of opportunities.
Godspeed Suz!
Thanks! And thank you for your fast and complete responses. It’s quite rare nowadays! Take care as well!
The topic ‘Setting featured image upon form submit’ is closed to new replies.