• Resolved giandani

    (@giandani)


    Hi Support,
    would it be possible to prefill some numerical fields with the data retrieved from the post that called the form?
    In details… I’ve several courses, all of them have a different cost: at the moment I’ve some conditions in a numeric field of the form related to the amount to be paid, the point is that each time there’s a new course I’ve to update the form.
    If possible I would like the form the get that amount directly from a custom field of the article.

    Many thanks,
    Giandani

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @giandani

    I hope you are doing well.

    “If possible I would like the form the get that amount directly from a custom field of the article.”

    From your description, I guess it is saved as a post meta value, in this case, we can create a custom code for it.

    – Get the ID of your Post Meta, the easiest way for this is by inspecting the page: https://monosnap.com/file/B3PpBWoJKCcVb9IbCZsM7TTdbz7IRL

    More about post meta: https://designmodo.com/wordpress-post-meta/

    – Get your Number field ID https://monosnap.com/file/0upXUEWpDQZPzY0hFDGYJABbIum0mn

    – Edit it and set the default value for this number field as 0.

    – Place the below code using a mu-plugin:

    <?php
    
    add_filter( 'forminator_field_number_markup', function( $html, $id ) {
    
    	$post_meta = (int) get_post_meta( get_queried_object_id(), 'field_name', true );
    
    	if ( $post_meta && $id = 'forminator-field-number-1' ) {
    		$html = str_replace( 'value="0"', 'value="' . $post_meta . '"', $html );
    	}
    	return $html;
    
    }, 10, 2 );

    You can find more about mu-plugin https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Update the ‘field_name’ adding your cost field key
    Update the forminator-field-number-1 by adding your number ID, for example if your number field is 5 it would be forminator-field-number-5.

    Now when the form loads on your post/page it will use this custom post meta to populate the number field.

    Best Regards
    Patrick Freitas

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @giandani ,

    We haven’t heard from you for a while now, so it looks like you don’t have more questions for us.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

    Thread Starter giandani

    (@giandani)

    I haven’t had the occasion to test the fix…
    I’ll let you know if I’ll need some more help on this.

    Many thanks,
    Giandani

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘prefill numeric fields with data from the post’ is closed to new replies.