• Resolved 6n15

    (@6n15)


    Hi. I was able to implement an important feature on my homepage with Forminator. Thank you very much.
    I have a couple of questions while using it.

    I created a form that does post submission on the front-end, which submits a Custom Post Type with a Custom Taxonomy (created using the ACF plugin). Inside this form, I have a Hidden Field with the custom taxonomy ID value and mapped it to the Post Data Custom Field so that the custom taxonomy is applied to the submitted post.

    However, after submitting the form, the taxonomy is not being applied to the post properly. Despite the “post custom data” field inside the post being just well inserted the form value and even the taxonomy checkbox of the post was already checked on the submitted taxonomy ID value.

    Meanwhile, If I go into the editor directly from the WP admin panel and save or update the submitted post without making any changes, then it is applied properly. In other words, if I update the post manually on the post editor page without any changes, everything works fine.

    I contacted ACF plugin first and they suggested a couple of things to fix the issue, assuming it’s an update_post_meta function issue.

    So,
    Is there any way to update the taxonomy field using ACF plugin’s update_field() function instead of WordPress update_post_meta function?
    Or, is there any way to have the save_post action fired after the form submission?

    • This topic was modified 2 months, 3 weeks ago by 6n15.
Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Support Zafer – WPMU DEV Support

    (@wpmudevsupport15)

    Hi @6n15,

    I hope you are doing well today!

    We are checking your request with our developers if it is possible to provide a workaround or custom code to achieve this and will inform you accordingly.

    Thanks for the patience while we are looking into the issue.

    Kind regards,
    Zafer

    Thread Starter 6n15

    (@6n15)

    Thanks for your effort,
    I have a snippet that they provided when I contacted ACF, and I’m attaching it in case it helps.

    The code below is what they told me I could use to try to update the field if the forminator has a filter. (I’m not a coder, I know just basic HTML/CSS. I just hope this helps a bit)

    /**
    * Updates top level array based field to ensure it has field key save in post meta
    *
    * @param mixed $field_name the name of the top level field to be updated
    * @param mixed $post_id the id of the post or WP object
    * @return void
    */
    function reupdate_array_based_field_with_its_value($field_name,$post_id){
    $field_value=get_field($field_name, $post_id, false);//third parametr to false to avoid formating
    update_field($field_name,array(),$post_id);//update the field with empty value so that we can update it with the right value. The change of value will ensure the field key is saved.
    update_field($field_name,$field_value,$post_id);//now update with the real value
    }
    //usage: reupdate_array_based_field_with_its_value('field_name',123);
    Plugin Support Williams – WPMU DEV Support

    (@wpmudev-support8)

    Hi @6n15

    Thanks for response!

    If it’s about the actual custom taxonomy, please try this code instead:

    <?php 
    add_action(
    	'forminator_post_data_field_post_saved',
    	function ( $post_id, $field, $data ) {
    		$form_data = Forminator_CForm_Front_Action::$prepared_data;
    		$category  = $form_data['hidden-1']; // Please change hidden-1 to the field ID that has the term ID.
    		wp_set_object_terms( $post_id, $category, 'my_tax' ); // Please change category to your custom taxonomy name.
    	},
    	10,
    	3
    );

    Here is how to add it to the site:

    1. access your site’s WordPress install using FTP, cPanel “File Manager” or similar method that lets you access files directly

    2. go to “/wp-content/mu-plugins” folder; if there’s no “mu-plugins” folder in “wp-content”, just create an empty one first

    3. inside that folder create an empty file with a .php extension (e.g. “forminator-acf-hidden-taxonomy-update.php”)

    4. copy above code and paste it into that file

    5. make these two changes in the code

    a) in this line

    $category = $form_data['hidden-1'];

    replace hidden-1 with your hidden field ID (if it’s different, e.g. hidden-2 or hidden-3)

    b) in this line

    wp_set_object_terms( $post_id, $category, 'my_tax' );

    replace my_tax word with the name of your custom taxonomy

    6. save the file

    That’s it. If we are talking about the actual custom taxonomy, this should work out of the box.

    —-

    However, I have some doubts here. You mentioned some checkbox on the form and updating data using ACF’s “update_field()” function. The code that you share also is for updating custom field rather than custom taxonomy. So I’m wondering – maybe you mean something else like, for example, trying to set a value of ACF’s standard “checkbox” field based on number in hidden field instead?

    Kind regards,
    Adam

    Thread Starter 6n15

    (@6n15)

    Thanks for your thoughtful response.

    The example you gave at the end seems similar to what I’m trying to do, and I was wrong about the checkboxes, sorry.

    To clarify,
    I have mapped a hidden field in the form by Forminator to a custom field by ACF (-this custom field is taxonomy type and its appearance is single-select, not checkbox), and I have written a custom taxonomy id value (ex.34) in the hidden field.

    After submission, if I verify the post directly on the editor page, the custom field in the ACF(that is single-select type) has already been applied to match the submitted taxonomy ID value. However, the checkbox inside the meta box for the custom taxonomy is not checked. And when I update the post, the taxonomy that matches the submitted taxonomy ID is checked by itself.

    Is this a completely different issue than before?
    I haven’t tried your code yet. And again, sorry for the misinformation.

    • This reply was modified 2 months, 3 weeks ago by 6n15.
    • This reply was modified 2 months, 3 weeks ago by 6n15.
    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @6n15

    Is this a completely different issue than before?

    The code shared before only helps with assigning taxonomy, however, based on your latest response it sounds like you are looking to map a hidden field to a custom field which is of taxonomy type.

    If yes, then I’m afraid it’ll require further custom changes in the code to support such a setup. However, to have a better idea regarding your setup, possible to provide the form export and also the ACF export for the post type/custom tax/custom field, so that we could have a better idea?

    The export for ACF can be generated under the ACF > Tools page. For the Forminator form export 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

    Thread Starter 6n15

    (@6n15)

    link for downlaod

    I’ve exported and uploaded the data.

    The idea was to create 3 forms for each type and implement them as mentioned above (with each id value in a hidden field in each form)

    Because of the Korean language, I’ll explain a bit for quick understanding,
    Custom Post Type = Patients Messages
    Custom Taxonomy = Message Types
    (There are three types)
    -> msg-pregnancy, id value=34
    -> msg-birth, id value=35
    -> msg-thanks, id value=36

    Originally, I could’ve made the form user allow to specify the message type within the post data as a basic feature of Forminator, but I chose to do it by custom field because different message types require different fields to be visible. If I could apply conditional logic based on the message type to set the visibility of the fields, I wouldn’t need to map them as hidden fields.

    If this is a simpler way to do, I would really appreciate any help on this. But if it’s not, just forget what I said ??
    It’s been so much more than I expected and I’m very grateful for your response.

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @6n15,

    Thanks for providing form export, the workflow you have mentioned would require custom coding, so I’m escalating this further to our developers to see if there is any workaround that could be suggested.

    Please be advised that our developers will first ascertain if the request falls within our support scope. Should it necessitate extensive custom coding, it may unfortunately fall outside our support capabilities.

    Will soon get back to you via the ticket once we get further feedback asap.

    Kind Regards,

    Nithin

    Thread Starter 6n15

    (@6n15)

    Thank you very much. I look forward to your response.

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @6n15,

    It appears the default behaviour of the form submission does indeed work when tested ie

    View post on imgur.com

    That’s the “Message Type from Form” does get assigned the mapped custom field, where “testone” in the above screenshot would be taxonomy which I have already created in my test site under Patients Messages > Message Type.

    Could you please share a screenshot of what you notice on your side so that we can have a better idea? Are you using the correct taxonomy ID for the “hidden-1” field?

    I do see you are referring to checkbox, could you please share a screenshot regarding those aspects to make sure we have a better idea.

    Looking forward to your response.

    Kind Regards,

    Nithin

    Thread Starter 6n15

    (@6n15)

    Yes, I am using the correct taxonomy id.
    To help you understand the exact issue, I’ve uploaded the screenshot video to the Google Drive folder I shared earlier. I hope this helps ??

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @6n15

    I can’t see any link or attachments in your last reply, it seems the link is removed for some reason or you forgot to send it with your message.

    We still need a screenshot or recording from your end to better understand the issue so please resend the links.

    Looking forward to hearing from you.

    Best Regards,
    Amin

    Thread Starter 6n15

    (@6n15)

    link

    I’ve uploaded the screenshots to the Google Drive folder I shared with you when I sent you the export data. The link above is the same link I shared with you. I guess I didn’t explain enough in my last reply.
    Inside the Google Drive folder in the link above, there is a video called “issue recording”, which you can refer to.
    Thanks.

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hello @6n15

    Hope you’re doing well today!

    Thank you for the screen recording. That certainly helps to understand the issue better.

    I have shared this further with our developers for further review. We will keep you posted on this as soon as we have further insights from them.

    Kind Regards,
    Saurabh

    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @6n15,

    Could you please try this snippet and then check whether it works fine?

    <?php

    add_action(
    'forminator_post_data_field_post_saved',
    function ( $post_id, $field, $data ) {
    $form_data = Forminator_CForm_Front_Action::$prepared_data;
    if ( 46566 !== intval( $form_data['form_id'] ) ) { // Please change 2960 to your form's ID.
    return;
    }
    $message_type = $form_data['hidden-1']; // Please change hidden-1 to the field ID that has the message type term ID.
    wp_set_object_terms( $post_id, intval( $message_type ), 'message-type' ); // Please change message-type to your custom taxonomy name.

    $category = $form_data['hidden-3']; // Please change hidden-1 to the field ID that has the category term ID.
    wp_set_object_terms( $post_id, intval( $category ), 'category' ); // Please change category to your custom taxonomy name.
    },
    10,
    3
    );

    You’ll have to make sure to edit the following line in the above code with your form ID:

    if ( 46566 !== intval( $form_data['form_id'] ) ) {

    Ie suppose your form ID is 123, then the above line will change as:
    if ( 123 !== intval( $form_data['form_id'] ) ) {

    Also make sure to update the code with the correct taxonomy names and hidden field ID, in the following lines:

    $message_type = $form_data['hidden-1']; 
    wp_set_object_terms( $post_id, intval( $message_type ), 'message-type' ); // Please change message-type to your custom taxonomy name.

    $category = $form_data['hidden-3']; // Please change hidden-1 to the field ID that has the category term ID.

    wp_set_object_terms( $post_id, intval( $category ), 'category' ); // Please change category to your custom taxonomy name.

    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-plugins

    Please do let us know how that goes.

    Kind Regards,

    Nithin

    Thread Starter 6n15

    (@6n15)

    Thank you so much for the snippet.
    But due to my special situation, I can’t ftp access the wp-content folder or install the mu-plugin in any way, is it possible to use the code you gave me with a snippet plugin like “WP Code”?

Viewing 15 replies - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.