[Forminator Pro] I need to know how I can pass values into a form

Hello! I’m currently in a trial period with forminator pro. I need to be able to pass information from the_post() – the page we’re on – into form fields, as the “default value”.

For example, I have a post that describes a class I want to sell people. The post includes custom fields for the class_date, class_cost, class_location. I would like to create a form for my site visitor to be able to register one or more students for this class. I would like to have a field on that form for the class name. Since the class name is the title of the post, I would have the form field’s default value be “page_title”.

I would also like a form field for class date. The default value for that form field should be automatically filled with the custom field “class_date”.

Another form field might be for the class cost, and that form field’s default value should be the value of the custom field “class_cost”.

I have used WP Forms for similar purposes, as indicated in the attached screenshots. The screenshots illustrate inserting something like the page title or url. The WP Forms documentation for this can be found: https://wpforms.com/docs/how-to-use-smart-tags-in-wpforms/

Is there a similar functionality that Forminator can offer? I would LOVE to be able to ditch all of the various services I have and just get a membership, but honestly, I can’t justify the cost if Forminator cannot handle something like this. Thanks so much!

  • Patrick Freitas
    • FLS

    Hi Sam

    I hope you are doing well.

    On the Forminator the Macros are used on the plugin settings, for example, to configure a confirmation email you can use {email-1}

    We do have the pre-populate option, but this is passed to the form as Query String:

    /form?course=”my-class”

    [attachments are only viewable by logged-in members]

    https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#pre-populate-form-field-values

    Some values for example Page Title or Page URL can be added using the Hidden field https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#hidden-field

    But I also pinged our developers to check if we have any filter that can help you on this.

    We will update once hearing back from the team.
    Best Regards
    Patrick Freitas

  • Patrick Freitas
    • FLS

    Hi Sam

    I hope you are doing well.

    For this would require a custom code, for example:

    <?php
    add_filter( 'forminator_field_create_input', function( $html, $attr ){
    	static $list_fields = [];
    	if( ! $list_fields ){
    		global $post;
    		// $post = [post_id];
    		// $post = 7568;
    		$post_id = is_numeric( $post ) ? $post : $post->ID;
    		$class_date = get_post_meta( $post_id, 'class_date', true ); //m/d/Y => 01/16/2020 => the format should be matched with the settings.
    		$class_cost = get_post_meta( $post_id, 'class_cost', true );
    		$list_fields = array(
    			'text-1' => get_the_title( $post ),
    			'date-1' => $class_date
    			'text-2' => $class_cost,
    		);
    		// $list_fields['date-1'] = '01/16/2020';
    	}
    	if( $list_fields && isset( $list_fields[ $attr['name'] ] ) ){
    		$html = str_replace('value="'. $attr['value'] .'"', 'value="'. $list_fields[ $attr['name'] ] .'"', $html );
    	}
    	return $html;
    },10, 2 );

    The get_post_meta is similar to the get_field from ACF,

    This code can be used as a mu-plugin, in case you are not familiar with mu-plugin, you can find documentation at:

    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    We also sent this thread as a feature request to our Developers.

    Let us know if you have any additional question.
    Best Regards
    Patrick Freitas

  • Samantha
    • Flash Drive

    Hello, Patrick! Thanks for getting back to me. I’ve been trying to figure out how to implement a solution using the example you provided, but I am hitting a wall. Can I get any additional input? (I understand how to implement mu plugins, so you can skip that part.)
    I’m not using acf, just the on-the-fly wordpress custom fields. How can I get the value of the custom field passed into a form? I’ve gone through the that snippet a few times, and looked at the filter forminator_field_create_input in abstract-class-field.php. I can get it to show me true and false for the individual if statements – or, because I’m completely juvenile, I can get it to print a different swear word for each if statement. I’m hoping I’m just missing something simple, but I don’t see how that filter should do what I want it to do.
    To clarify: I want to create a form with a field named “Class Cost”. I want to embed that form in a post. On that post, I have a custom field named “class_cost” which I have set to $50. When I visit that post and scroll down to the form, I want the form field named “Class Cost” to be automatically populated with $50.
    Thanks!
    [attachments are only viewable by logged-in members]

  • Patrick Freitas
    • FLS

    Hi Sam

    Would it be possible to send some temporary credentials and we can take a closer look on your page and check if we need to update the filter?

    This filter works when the HTML is generated, so it would append the value for those fields:

    list_fields = array(
    			'text-1' => get_the_title( $post ),
    			'date-1' => $class_date
    			'text-2' => $class_cost,
    		); 

    In case your field ID is different than text-1 you can update this array.

    However, if it is possible to send some credentials would be great so we can check the final filter code for you.

    Note: Please don’t share any sensitive information (i.e credentials) in the Support Forum.

    Instead, you can send us your details using our contact form https://wpmudev.com/contact/#i-have-a-different-question:

    Subject: “Attn: Patrick Freitas”

    – Site login URL:

    – WordPress admin username:
    – WordPress admin password:

    – FTP/SFTP credentials

    Host:
    Username:
    Password:
    Port:

    – cPanel credentials

    Host:
    Username:
    Password:

    – Folder path to the site in question:

    – Link back to this thread for reference

    – Any other relevant URLs/info:

    Please, reply to the ticket once you have sent the information.
    Best Regards
    Patrick Freitas

  • Samantha
    • Flash Drive

    Hi, Patrick,
    I’m developing locally, so I won’t be able to hook you up like that, but let me walk you through what I have and what is (isn’t) happening.

    First, the snippet you gave me, adjusted for my field ids:

    add_filter( 'forminator_field_create_input', function( $html, $attr ){
    	static $list_fields = [];
    	if( ! $list_fields ){
    		global $post;
    		// $post = [post_id];
    		// $post = 7568;
    		$post_id = is_numeric( $post ) ? $post : $post->ID;
    		// $class_date = get_post_meta( $post_id, 'class_date', true ); //m/d/Y => 01/16/2020 => the format should be matched with the settings.
    		$class_cost = get_post_meta( $post_id, 'class_cost', false );
    		$list_fields = array(
    			'name-2' => get_the_title( $post ),
    			// 'date-1' => $class_date
    			'text-2' => $class_cost
    		);
    		// $list_fields['date-1'] = '01/16/2020';
    	}
    	if( $list_fields && isset( $list_fields[ $attr['name'] ] ) ){
    		$html = str_replace('value="'. $attr['value'] .'"', 'value="'. $list_fields[ $attr['name'] ] .'"', $html );
    	}
    	return $html;
    },10, 2 );
    

    My custom field, as assigned in the wp editor for this post, is class_cost:
    [attachments are only viewable by logged-in members]

    And my form is set us as so:
    [attachments are only viewable by logged-in members]

    A closer look at form field {name-2} which I’ve labeled “Post Title”
    [attachments are only viewable by logged-in members]

    And at the form field {text-2} which I’ve labeled “Cost”
    [attachments are only viewable by logged-in members]

    What actually happens on the front side when we look at that post:
    [attachments are only viewable by logged-in members]

    And, while I know this is a less-than-ideal way to look at this, I thought you might find it useful to see what the inspector shows. I must be at my limit for what I can attach so I’ll add another message with that screenshot.

  • Tho Bui
    • SLS

    Hey there Sam ,

    I checked in your custom code. There is only one issue in the function retrieve the post meta value:
    $class_cost = get_post_meta( $post_id, 'class_cost', false );
    This will return an array, so it should be:
    $class_cost = get_post_meta( $post_id, 'class_cost', true );
    But I’m not sure why it didn’t work for you. I tested this on my site, and it worked fine.
    If this doesn’t help, can you try to log the value, make sure these values exist instead of empty?

    Kind Regards,
    Tho Bui

  • Samantha
    • Flash Drive

    Hey Tho,
    Sorry about that. I do have it set to true, and did for all but one test, which is where I copied from.
    Mine looks like:
    $class_cost = get_post_meta( $post_id, 'class_cost', true );

    For the sake of simplifying everything, I have stripped out everything in the $list_fields array except for that line. So we’re only working with class_cost.

    When I console.log the $list_fields, it shows only the key without the value
    {"text-2":false}

    When I console.log the variable $html I get this:
    <label for="forminator-field-text-2" class="forminator-label">Cost</label><input type="text" name="text-2" value="" placeholder="{text-2}" id="forminator-field-text-2" class="forminator-input forminator-name--field" data-required="" />

    But I am getting the value of the custom fields on the post using this on my template page (within the loop)
    Class cost:<?php $key="class_cost"; echo get_post_meta($post->ID, $key, true); ?>

    And I am getting the correct value ($50) in the console when I put this on my template page (within the loop)
    <?php echo "<script>console.log('" . get_post_meta($post->ID, 'class_cost', true) . "');</script>";?>

    Any thoughts?