• Resolved FAOU

    (@faou)


    First of all, thank you for making Forminator great! Appreciate your help!

    How do I put the value of a meta field/custom field into one of the Forminator fields? I’m using Gutenberg. A Forminator form is automatically added to each page of a custom post type I made with Advanced Custom Fields (ACF). I am trying to put some hidden custom field values (corresponding to each individual page) about the project a submission corresponds to in hidden fields of a form, so that the form stays short, accurate, and avoids redundancy.

    In a previous support thread, the support team gave a very brief and obscure answer that a meta field could be retrieved using the following PHP code:

    $custom_field_value = get_post_meta(get_the_ID(), 'custom_field_name', true);

    The answer did not explain where to put this code. I tried putting it in the hidden form field, using the meta key shown in ACF. I tried entering it without PHP tags and with PHP tags, but neither returned the proper value when I did test submissions. I tried some other Forminator form fields too, but none worked.

    How do I get this dynamic (relative to the form, static relative to the individual post) content submitted with the form?

    • This topic was modified 3 months, 4 weeks ago by FAOU.

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @faou,

    I hope you are keeping well and thank you for reaching out to us.

    The PHP code provided in the mentioned topic is intended to fetch post metadata. It won’t work if you directly place it inside a hidden field. However, you should be able to build a workaround with the help of a developer.

    Let me help you with an example code that can help populate a hidden field with a post meta.

    <?php
    add_filter( 'forminator_field_hidden_field_value', 'wpmudev_populate_hidden_field_date', 10, 4 );
    function wpmudev_populate_hidden_field_date( $value, $saved_value, $field, $hidden_field ) {
    	if ( ! is_user_logged_in() ) {
    		return $value;
    	}
    
    	if ( 'custom_value' == $saved_value && '{profile_val}' == $value ) {
    		$value = get_user_meta( get_current_user_id(), 'custom_field_name', true );
    	}
    	
    	return $value;
    }

    You need to update the “custom_field_name” with your custom post field name in the above code. Further, in the hidden field you need to select custom value as the default value, and then in value need to use {profile_val} macro as in the following screenshot: https://ibb.co/Wn7SkCJ

    The workaround could be added using a mu-plugin, please find more details in this article here: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    I hope that helps.

    Kind Regards,
    Nebu John

    Thread Starter FAOU

    (@faou)

    Thank you for the quick response!

    Respectfully, you did not really answer my questions. I tried to be as clear as possible, but I think you have misunderstood. My issue is a very similar to the one in the thread that I linked. Like I said, “I am trying to put some hidden custom field values (corresponding to each individual page) about the project a submission corresponds to in hidden fields of a form.” Whether a user is logged in is irrelevant, so your code does not seem relevant. Correct me if I am wrong though. Maybe you could explain it a bit better. I am no master of PHP, but I should not need a developer. I can put something basic in a code snippet if there’s no way to put it into the form directly. The PHP for this should be relatively simple. The issue is getting the PHP to either work inside Forminator form fields or passing data to some intermediary from which Forminator can obtain the custom field values. How can that be done?

    Does Forminator support some kind of “magic” tags (that’s what some programs call the {custom_field_value} type tags)? If Forminator has a format for entering custom field variables (like the {profile_val} entry that you mentioned) without PHP that would be great, but when I tried putting in {profile_val} in the hidden field and submitting the form, it literally just returned “{profile_val}” in the form submission. I also tried this with the custom fields on the page and had similar results. It did not work when I put it in without the brackets either.

    If {custom_field_name} worked in the hidden field (with custom value selected), that would make additional PHP unnecessary. Any plans to update Forminator to make that possible? It would be great if Forminator had a dynamic content option that could just pull the info from the post. Just to reiterate, I am trying to pull “dynamic (relative to the form, static relative to the individual post) content” from the post. The same form is on multiple posts.

    Are you all working on adding dynamic features to Forminator? It seems like every website today has dynamic content and could benefit from the capability to bring that data into the form.

    Thanks!

    Plugin Support Laura – WPMU DEV Support

    (@wpmudevsupport3)

    Hi @faou,

    Hope this message finds you well.

    Whether a user is logged in is irrelevant, so your code does not seem relevant.

    Assuming you are referring to this code:

    if ( ! is_user_logged_in() ) {
         return $value;
    }

    This is because my colleague shared a base code created by our devs previously to get Custom Fields from the User Profile, the code needs to validate that the user is logged before getting the values to avoid a fatal error in the site. Since this is an example/base code, this is why he mentioned:

    you should be able to build a workaround with the help of a developer.

    About this:

    The PHP for this should be relatively simple. The issue is getting the PHP to either work inside Forminator form fields or passing data to some intermediary from which Forminator can obtain the custom field values. How can that be done?

    Let me explain a little what the code does:

    The filter forminator_field_hidden_field_value allows to manipulate any data before the hidden field renders in the browser. You will find more information about using filters in WordPress on this link.

    This part of the code will create the {profile_val} tag and add the value from the custom field “in it”.

    if ( 'custom_value' == $saved_value && '{profile_val}' == $value ) {

    In this example, the $value for that tag uses the get_user_meta() function to get the value from the user meta. To get custom fields from a post the function would be get_post_meta() function, just like the topic you shared:

    $custom_field_value = get_post_meta(get_the_ID(), 'custom_field_name', true);

    Are you all working on adding dynamic features to Forminator? It seems like every website today has dynamic content and could benefit from the capability to bring that data into the form.

    Each request we receive is noted and considered as part of our ongoing efforts to improve and evolve our product. In this case, we have added your topic as a +1 to highlight its significance and to acknowledge the demand for this particular feature already requested.

    While we cannot guarantee immediate implementation of every feature request due to various considerations and priorities in our development roadmap, please be assured that your input is crucial in contributing to the future development and enhancement of our plugin.

    Let us know if you require additional information.

    Best regards,
    Laura

    Thread Starter FAOU

    (@faou)

    It seems like your message mostly reworded what I said. I don’t feel like I’ve gotten any help with this yet. I am glad that Forminator is at least considering adding this feature. It seems like less of a need to add a feature as almost a bug. Don’t get me wrong; I still think Forminator is a fantastic plugin, and I am grateful to you all for it. Still, I don’t understand why any of this custom coding is necessary. I understand now (from my own hours researching how to do this, not that you all ever even explained this) that having input in form fields run as PHP would probably be bad from a security standpoint. It does not seem like that point is strong enough to warrant not allowing any shortcodes (including shortcodes like Pods that could print values from custom fields) to work in the fields, especially because the shortcodes could run before the user interacted with the form. I tried the code suggestion in your documentation, but your code there seems to only allow shortcodes to work in HTML fields, not input fields.

    add_filter( 'forminator_replace_variables', 'do_shortcode' );

    I feel like the responses you all have sent have been to say that I can’t possibly understand and need a developer. Yes, I have a very slim and new understanding of php, but that really is not the issue. Even a developer would have questions here. The official documentation does not include info about Forminator’s hooks, and the closest thing that I could find is a list of Forminator hooks on Github that is not very descriptive.

    I have spent hours and hours trying to get this to work. I have tried countless versions of code, but I haven’t had any success. I redirected my efforts to just trying to get shortcodes to prepopulate the data, for example, setting default values to custom field values using shortcodes, as opposed to getting ACF custom field keys to work directly in the form. I wrote the simple code below and put it on the relevant page using a WPCode snippet for PHP. Why is this not working?

    <?php
    function custom_forminator_replace_form_data($content, $data, $fields) {
    // Process shortcodes in the content
    $content = do_shortcode($content);

    // Process shortcodes in the data
    foreach ($data as &$value) { // Pass by reference to modify the original array
    if (is_string($value)) { // Only process if the value is a string
    $value = do_shortcode($value); // Process the shortcode
    }
    }

    // Process shortcodes in the fields
    foreach ($fields as &$field) { // Pass by reference to modify the original array
    if (isset($field['value']) && is_string($field['value'])) { // Ensure 'value' is set and is a string
    $field['value'] = do_shortcode($field['value']); // Process the shortcode
    }
    }

    // Return all modified parameters
    return [$content, $data, $fields];
    }
    add_filter('forminator_replace_form_data', 'custom_forminator_replace_form_data', 10, 3);
    Plugin Support Nithin – WPMU DEV Support

    (@wpmudevsupport11)

    Hi @faou,

    There isn’t any native feature for the workflow you are looking for other than implementing custom coding.

    You can refer to this example code for reference:

    https://www.ads-software.com/support/topic/hidden-field-getting-data-from-acf-2/#post-16520021

    This is a tweaked snippet which should work when tested:

    <?php

    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_custom_hidden_field_data', 10, 3 );
    function wpmudev_custom_hidden_field_data( $entry, $module_id, $field_data_array ) {
    $form_ids = array( 1298 ); // Please change the form ID.
    if ( ! in_array( intval( $module_id ), $form_ids, true ) ) {
    return;
    }

    foreach ( $field_data_array as $key => $value ) {
    if ( false !== strpos( $value['name'], 'hidden-' ) && false !== strpos( $value['value'], 'acf_' ) ) {
    $field_keys = explode( 'acf_', $value['value'] );
    $post_id = Forminator_CForm_Front_Action::$prepared_data['page_id'];

    Forminator_CForm_Front_Action::$info['field_data_array'][ $key ]['value'] = get_field( $field_keys[1], $post_id );
    }
    }
    }

    The above should help with dynamically populating the ACF values to the Hidden fields in the form.

    You’ll need to update line 1298 in the above snippet with your form ID. You can implement the given snippet 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 note that the form should be added in a post/page where the Custom fields from ACF are present, so that it can pull the content and you’ll have to make sure the Hidden Field has the “Custom Value” set to the ACF field ID, ie suppose the ACF field is testing, then the custom value would be “acf_testing” ie as shown in the following screenshot:

    View post on imgur.com

    I can confirm the above snippet does work when tested.

    Kind Regards,

    Nithin

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @faou

    I hope you are doing well

    We haven’t heard from you in a while, I’ll go and mark this thread as resolved. If you have any additional questions or require further help, please let us know!

    Best Regards
    Patrick Freitas

    Thread Starter FAOU

    (@faou)

    Please do not mark this as resolved. My issue was not addressed, so it was not resolved. I talked about using ACF custom fields early on, but I explained in subsequent messages that after struggling with that, I was trying to get shortcodes to work, which goes well beyond just ACF custom fields in Forminators hidden fields. You all did not address what hook to use or why the code I spent a lot of time writing and testing is not working. Fluent Forms has a specific field for shortcodes and other ways to integrate dynamic content. I don’t want to spend all the time switching our forms over, but I’m wondering if it will save us a lot of time at this point. Are there any plans to add functionality to have shortcodes available in all fields?

    Plugin Support Laura – WPMU DEV Support

    (@wpmudevsupport3)

    Hi @faou,

    As explained by Patrick, even if the topic is marked as solved you will be able to reply.

    You all did not address what hook to use or why the code I spent a lot of time writing and testing is not working.

    I would like to clarify that while we are committed to providing support and assistance with issues related to our plugins, analyzing custom code is not within the realm of our support services.

    Still, the hook provided in Nithin’s reply:

    forminator_custom_form_submit_before_set_fields

    Is triggered before the fields are rendered, is during that function you might need to add any value to in this case a hidden field.

    Best regards,
    Laura

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