Form Data to Post and Custom Fields
-
Hi guys
I am creating a Create Post form and all is working perfectly but I need help with something.
Currently I have a Custom Post Type created with ACF and it has 3 Custom Post Taxonomies attached to this CPT. As you can see on this screenshot https://prnt.sc/DwruzQw-QrXkOn the current Post Data field, those categories are listed on a select field (https://prnt.sc/NmGOjhnM6ZRK), but my question is if it is possible to map that information using text fields, so the person that is registering on my site could fill the City name instead of select one.
Thanks in advance for your help.
Best regards,
DanielThe page I need help with: [log in to see the link]
-
Hi @somosgrow,
Could you please check and see whether the following snippet helps?
<?php add_action( 'forminator_post_data_field_post_saved', function( $post_id, $field, $data ) { $form_data = Forminator_CForm_Front_Action::$prepared_data; if ( 1006 !== intval( $form_data['form_id'] ) ) { return; } $city = $form_data['text-1']; wp_set_object_terms( $post_id, $city, 'city' ); },10,3);
In the above snippet, it’ll fetch the city entered from input field ie text-1 and assign to the post.
The city in the following will be the category:
wp_set_object_terms( $post_id, $city, 'city' );
You’ll need to update that to your category, ie for example to assign to default post category it would be as:
wp_set_object_terms( $post_id, $city, 'category' );
You’ll also have to make sure to update the code with your form ID in here:if ( 1006 !== intval( $form_data['form_id'] ) ) {
Suppose the form ID is 123, then the above line will change as:
if ( 123 !== intval( $form_data['form_id'] ) ) {
Could you please check and see whether the above helps?
You can implement the above code using mu-plugins. Please check this link on how to implement the above code as a mu-plugins:
https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-pluginsPlease do let us know how that goes.
Kind Regards,
Nithin
Hello Nithin
Thanks a lot for your help. The snippet worked perfectly (as you can see https://prnt.sc/bX8GYVg5qI7L).
Now, I have another inquiry. Currently on the form that appears on the link I have 2 Upload fields (https://prnt.sc/BMdsCsLFTgo9), one for a Logo and the other one for a Header bg image.
I tried to map the images in the image fields of my CPT (https://prnt.sc/RK399jn5JT7q) but when I see the Post created, the images do not appear loaded in the expected fields (https://prnt.sc/XwTDegEuE28G).. Is there any way to achieve this?
The idea is that the two images are uploaded from the form and appear loaded in the corresponding ACF fields in the CPT.Thanks again for your valuable help.
Kind regards.
Hi @somosgrow
Please follow Adam reply here which should help you in that matter: https://www.ads-software.com/support/topic/create-post-image-upload-to-acf/#post-16778598
Kind regards,
KrisHi Kris.
Thank you for your quick response. I will give it a try and if something come up I will reach you again.
Hope you have a great day.
Hi again Kris.
I want to let you know that my form now is working as expected, thanks a lot for your help.I want to ask a last thing, is there a way to restrict the width and height of an image that is uploaded on the form? Currently the clients are uploading images of 1200px width when the form says that only 300px.
Thanks in advance.Best regards,
DanielHi @somosgrow,
I want to ask a last thing, is there a way to restrict the width and height of an image that is uploaded on the form? Currently the clients are uploading images of 1200px width when the form says that only 300px.
I’m afraid, this might require custom code. Is this for all the upload fields or with any specific upload field? However, could you please share the form export to have a better idea regarding how the fields are used to see if there is any workaround that could be suggested?
Please check the following doc on how to export a form:
https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export
If you are concerned about any sensitive information in the form, then you can duplicate your form, remove any sensitive information, and then export it.
You can share the export file via Google Drive, Dropbox or any cloud service in the next reply.
Looking forward to your response.
Best Regards,
Nithin
Hello Nithin
Here’s the link when you can find the form export and the ACF post type and fields that are attached to the form: https://drive.google.com/drive/folders/1cCrE63MaJGYW0-kwqZmw2qqOX8cV9eX5?usp=drive_link
The idea is restrict the 2 upload fields that appear on the form. One with 300x300px, and the other one with 600x450px.
Customers are uploading big images.
Thanks for your help.Hey @somosgrow
I hope you’re well today!
I gave it a shot came up with this code that may help:
https://gist.github.com/adczk/f4916c36188039efe6b1580061041189
It should be added to the site as MU plugin but please note:
1. it will only let you set one width/height – not separately for different fields; if it works for you I could probably help extend it to allow different sizes for upload fields but first, please test it
2. it will ONLY work if upload field is set to “single” upload; I noticed that on your form they are set this way already so it should be fine.
You’ll need to configure the code in this part also but I believe it’s pretty self-explanatory:
## CONFIG ## $form_ids = array( 3010, 123 ); // form IDs - one or more; if more - separate numbers with commas $fields_ids = array( 'upload-1', 'upload-2' ); // IDs of fields to check, one or more, if more - separate with commas $max_width = 500; // max image width in px $max_height = 500; // max image height in px $err_msg = 'Maximum image width/height: 150x150px!'; // custom error message for width/height $no_image_msg = 'Error fetching image sizes!'; // other error e.g. if it's not image file or doesn't exist due to upload issues ## END OF CONFIG; DO NOT EDIT BELOW ##
Best regards,
AdamHi Adam
Thanks again for your invaluable help.
I tested the code and it works as expected. I think that if you can help me extending the code to making sizes independent for each upload field it would be awesome.
Hope you have a great day.
Best regards
DanielHi @somosgrow
I’m glad I could help!
I gave it a shot and came up with updated version that will support setting different size limits per upload field, as well as a “fallback” size.
I’ve updated my gist so you’ll find full new code at the same link here
https://gist.github.com/adczk/f4916c36188039efe6b1580061041189
Once you update code on the site you’ll also need to adjust configuration again as this configuration code block has changed slightly:
## CONFIG ## $form_ids = array( 3010, 123 ); // form IDs - one or more; if more - separate numbers with commas $fields_ids = array( 'upload-1', 'upload-2', 'upload-3' ); // IDs of fields to check, one or more, if more - separate with commas // max image sizes per each upload field (note no comma after last upload field line // if size not defined here but field defined in $fields_ids, a "fallback" value below // will be used $max_sizes = array( 'upload-1' => array( 'width'=>500, 'height'=>500 ), 'upload-2' => array( 'width'=>150, 'height' => 200 ) ); // fallback sizes, if not defined per field above $max_width = 20; // max image width in px - fallback if not defined per field $max_height = 50; // max image height in px - fallback if not defined per field $err_msg = 'Maximum image width/height: [wdt] x [hdt]!'; // custom error message for width/height; [wdt] and [hdt] will be replaced with max width/height accordingly. $no_image_msg = 'Error fetching image sizes!'; // other error e.g. if it's not image file or doesn't exist due to upload issues ## END OF CONFIG; DO NOT EDIT BELOW ##
It’s similar but important changes are:
1. you now define default sizes here
// fallback sizes, if not defined per field above $max_width = 20; // max image width in px - fallback if not defined per field $max_height = 50; // max image height in px - fallback if not defined per field
They will be applied to fields that are listed in
$fields_ids
but don’t have individual sizes defined in$max_sizes
.2. here you define “per field” individual limits
$max_sizes = array( 'upload-1' => array( 'width'=>500, 'height'=>500 ), 'upload-2' => array( 'width'=>150, 'height' => 200 ) );
Note, fields here must still be defined in
$fields_ids
. I mean – if field is listed here but not in fields_ids, size restriction will not be applied. If field is listed in fields_ids but doesn’t have size defined here, the default “fallback” size limit will be applied to it. I hope it makes sense.3. and now in your error message here
$err_msg = 'Maximum image width/height: [wdt] x [hdt]!'
if you use those [wdt] and [hdt] placeholders (including square brackets!) they’ll be automatically replaced with actual maximum width/height allowed for particular upload field.
I hope that this together with comments in the code is clear enough so you’ll be able to set it up but in case you’d have any additional questions, let us know (note though: if it comes to code extension, that’s as far as we can go with it).
Best regards,
AdamHi Adam
I must say you’re the best support team ever. Everything worked perfectly and I’m glad for it.
Thanks a lot for your help and patience with this process.
Now I proceed to close this ticket.Have a great day.
Best regards,
Daniel
- You must be logged in to reply to this topic.