• Resolved nikonikoneko

    (@nikonikoneko)


    Hi,

    I would like to populate a (hidden) field from a shortcode parameter, e.g. like this: [forminator_form id="421" my_field="5"]. Is that possible? We have a child theme, so adding a filter or something like this would be easy enough.

    Background is that we want to reuse a form for subsequent events, so it would be convenient if we didn’t have to edit/copy the form for every event.

    Thanks in advance!
    Nikolas

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Imran – WPMU DEV Support

    (@wpmudev-support9)

    Hello @nikonikoneko !

    I hope you’re doing well.

    You can use the snippet below to achieve a similar result:

    add_filter( 'forminator_cform_render_fields', function( $wrappers, $form_id ){
    	$your_form_id = 123;
    	if( $form_id == $your_form_id ){
    		$_REQUEST[ 'query-var-hidden-1' ] = 'test1';
    		$_REQUEST[ 'query-var-hidden-2' ] = 'test2';
    	}
    	return $wrappers;
    }, 10, 2 );

    To configure it, please do the following:
    – change the $your_form_id to match the ID of your form (you can find the ID in the shortcode)
    – then you can set query variables – the “query-var-hidden-1” can be anything else, but please don’t use spaces there and the ‘test1’ is the value, so in your example it will be:

    add_filter( 'forminator_cform_render_fields', function( $wrappers, $form_id ){
    	$your_form_id = 421;
    	if( $form_id == $your_form_id ){
    		$_REQUEST[ 'my_field' ] = '5';
    	}
    	return $wrappers;
    }, 10, 2 );

    You can set as many of the REQUEST variables as you like.

    Next step would be to edit the field that should be filled in and the Settings tab fill in the query variable that will be used to populate the field:

    View post on imgur.com

    And that’s it 🙂

    Hope this helps!

    Kind regards,
    Pawel

    Thread Starter nikonikoneko

    (@nikonikoneko)

    Hey Pawel,

    hope you’re doing well and thank you for your reply!

    Unfortunately this is not what I need :/ I actually wanted adjust default values from the shortcode of a form without having to edit it, i.e. all from within a post.

    I tried adding a new shortcode using your $_REQUEST tinkering. The form shows up, but no luck with the query parameter yet :/

    add_shortcode('forminator_form_ext', 'do_forminator_form_ext');
    function do_forminator_form_ext( $atts ) {
    	$the_id = $atts['id'];
    	unset($atts['id']);
    	
    	add_filter('forminator_cform_render_fields', function( $wrappers, $form_id ) use ($the_id, $atts) {
    		if ( $form_id == $the_id ) {
    			foreach( $atts as $a ) {
    				$_REQUEST[$a] = $atts[$a];
    			}
    		}
    		return $wrappers;
    	}, 10, 2);
    	
    	return do_shortcode('[forminator_form id="' . $the_id . '"]');
    }

    If you happen to have any suggestions it would be greatly appreciated!

    Best regards,
    Nikolas

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @nikonikoneko

    I found this old script created by our developers that may help you.

    add_shortcode( 'wpmudev_forminator_form', function( $atts = array() ){
    	$atts = shortcode_atts( array(
    		'id' => null,
    		'field_values' => ''//"parameter_name1=value1&parameter_name2=value2"
    	), $atts );
    	if( ! empty( $atts['field_values'] ) ){
    		$atts['field_values'] = htmlspecialchars_decode( $atts['field_values'] );
    		$params = explode( '&', $atts['field_values'] );
    
    		foreach( $params as $param ){
    			list( $key, $value ) = explode( '=', $param );
    			$_REQUEST[ $key ] = $value;
    		}
    	}
    	return do_shortcode( sprintf('[forminator_form id="%d"]', $atts['id'] ) );
    } );

    But this only works if the Forminator > Form > Behaviour > Load form using AJAX is disabled.

    Let us know if this code helped you.
    Best Regards
    Patrick Freitas

    Thread Starter nikonikoneko

    (@nikonikoneko)

    Ahhhhh, it works 😌 Thank you so much! This was extremely helpful 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Populate field from shortcode parameter’ is closed to new replies.