• Resolved joecam

    (@joecam)


    Hi,

    I would need a snippet to pre-populate number field in the form, from ACF custom field registered with the custom post type. The from displayed on the detailed single page of the custom post type.

    Thank you for your support!

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

    (@wpmudevsupport12)

    Hi @joecam

    I hope you are doing well.

    There are some hook available for that, just before sharing the code, could you let us know:

    – Is it a fixed post/page that you have the ID, OR
    – Is it dynamic we need to get an ID on the fly?

    Best Regards
    Patrick Freitas

    Thread Starter joecam

    (@joecam)

    Hi Patrick,

    Thank you for your response.

    It is a dynamic page for every CPT detailed page. I would need to get and populate a number field in the form based on the CPT’s custom field, to be able to fill payment field with value.

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi @joecam

    I pinged our SLS Team to review this and we will get back with some update as soon this is possible.

    Kind Regards,
    Kris

    Plugin Support Kris – WPMU DEV Support

    (@wpmudevsupport13)

    Hi again @joecam

    This snippet should help. In the code?2910?should be changed to your form ID and?number-1?should be changed to your number field’s ID (need to change on multiple places).

    add_action( 'forminator_before_form_render', 'wpmudev_number_field_value_form', 10, 5 );
    function wpmudev_number_field_value_form( $id, $form_type, $post_id, $form_fields, $form_settings ) {
    	if ( 2910 == $id ) {
    		add_filter( 'forminator_field_number_markup', 'wpmudev_add_value_field_cpt', 10, 5 );
    	}
    }
    
    function wpmudev_add_value_field_cpt( $html, $id, $required, $placeholder, $value ) {
    	$meta_val = '';
    	if ( strpos( $id, 'forminator-field-number-1' ) !== false ) {
    		$meta_val = get_post_meta( get_the_ID(), 'your_meta_key', true );
    		$html = str_replace( 'name="number-1"', 'name="number-1" value="'.$meta_val.'"', $html );
    	}
    	
    	return $html;
    }

    He is a guide on how to install mu-plugin:
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Kind Regards,
    Kris

    Thread Starter joecam

    (@joecam)

    Thank you so much, it is working as expected.

    Note for other users looking for the same solution: need to replace your_meta_key in the code with the key of the custom field from ACF.

    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Pre-populate number field from CPT custom field’ is closed to new replies.