• Resolved kgmservizi

    (@kgmservizi)


    Hi, is it possible to retrieve a custom field from the current post where the form is inserted and populate a hidden field?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter kgmservizi

    (@kgmservizi)

    We can use this action:

    add_action( 'forminator_before_field_render', 'my_field_prefill', 10, 1 );
    function my_field_prefill( $field ) {
    
    // get the pre-fill key from field configuration
    $code = sanitize_text_field( Forminator_Field::get_property( 'prefill', $field ) );
    
    // check if it's not empty (is actually set) and if it wasn't already provided via URL
    if ( ! empty( $code ) && ! isset( $_REQUEST[ $code ] ) ) {   
        $_REQUEST[ $code ] = 'test'; 
    }
    }

    And code as query parameter of hidden field.

    Best regards

    Plugin Support Adam – WPMU DEV Support

    (@wpmudev-support8)

    Hi @kgmservizi

    I hope you’re well today!

    That’d be one way to do it if you can/want set the query var. Another way – without using query vars (meaning not even allowing them for the field), would be to use a code like this instead:

    add_action( 'forminator_custom_form_submit_before_set_fields', 'wpmudev_set_custom_field_to_hidden_field', 10, 3 );
    function wpmudev_set_custom_field_to_hidden_field( $entry, $module_id, $field_data_array ) {
    		
    	$forms = array( 123, 3010 ); // list of forms IDs to apply it to
    	
    	if ( in_array( $module_id, $forms ) ) {
    		
    		foreach ( $field_data_array as $key => $value ) {
    			if ( strpos( $value['name'], 'hidden-' ) !== false ) { 
    			
    				if ( strpos( $value['value'], '{cmf_' ) !== false ) { 
    				
    					$cmf_name = str_replace( '{cmf_', '', $value['value'] );
    					$cmf_name =str_replace ( '}', '', $cmf_name );
    					
    					$prep_data = Forminator_CForm_Front_Action::$prepared_data;
    					if ( ! empty( $prep_data['page_id'] ) ) {
    						$post_id = $prep_data['page_id'];
    					
    						$cmf_value = get_post_meta( $post_id, $cmf_name, true );
    
    						Forminator_CForm_Front_Action::$info['field_data_array'][ $key ]['value'] = sanitize_text_field( $cmf_value );
    
    					
    					}
    				}
    				
    			}
    		}
    	
    	}
    }

    In this line

    $forms = array( 123, 3010 ); // list of forms IDs to apply it to

    you need to list IDs of the forms it should work with.

    Then for every hidden field that you want to have custom field value put into it you just edit such hidden field and

    – set its “Default value (optional)” setting to “Custom Value” option
    – and put a macro like this in the custom value

    {cmf_myfield}

    replacing myfield part with the name of your custom field. For example, when testing I used custom field named footnotes so I used

    {cmf_footnotes}

    Best regards,
    Adam

    Plugin Support Dmytro – WPMU DEV Support

    (@wpmudevsupport16)

    Hello @kgmservizi,

    I hope the above snippet helped you with retrieving the custom field values.

    Since we haven’t heard from you for a while, I’m marking this thread as resolved. Please feel free to reply if you still have any questions, or need further assistance.

    Best Regards,
    Dmytro

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