[Forminator Pro] Using select field value as a URL parameter

Hi,

I am trying to create a custom URL link that gets sent to my customers so they can place another order and have most of the form pre-filled for them.

This is the link I have done so far;

https://premierbites.co.uk/business-and-office-catering/order-process/?menu={select-10}&opt1={select-6}&usname={name-1}&coname={text-2}&usnumber={phone-1}&usemail={email-1}#order

This link is sent automatically as part of the user email order confirmation.

I cannot get the first 2 parameters to work that use a “select” option – the link gets pre-filled with the “Select Label”, but I need it to use the “Select Value” as this is what the “Pre-Populate” option looks for;

[attachments are only viewable by logged-in members]

[attachments are only viewable by logged-in members]

[attachments are only viewable by logged-in members]

Now, I could re-name all the “Value” settings so they match the “label” – but I have lots of visibilty settings that will then get messed up along with internal links that are have the URL parameters filled in using the existing values.

 

Is there a workaround here to send the select value not label??

 

Ta

 

David

  • Zafer Oz
    • Ex Staff

    Hi David Pearce ,

    Select field’s pre-populate function already uses the field value instead of label.

    Please check the following example, yoururl/?myselect=two will select the second option onload.

    [attachments are only viewable by logged-in members]
    [attachments are only viewable by logged-in members]

    Kind regards,
    Zafer

  • David Pearce
    • Site Builder, Child of Zeus

    Hi Zafer,

    Thanks, yes it does when you add the URL parameter with the required value.

    What I am trying to do is create a link that is sent when the user submits the form, so if they click on this link various form fields will be pre-filled using existing data.

    If you look at the link I added above, the URL parameter for a select box is “&opt1={select-6}”
    The {select-6} part needs to contain the “value” not the “label” for the link to work correctly.

    Take a look at the form here;
    https://premierbites.co.uk/business-and-office-catering/order-process/?menu=PLB1&opt1=2#order
    If you complete this form and submit it you will receive the usual order confirmation email – within this is a “Re-order link”.
    Click this link and the 2 select fields do not get pre-filled as the data in the URL has the “label” and not the “value”
    [attachments are only viewable by logged-in members]

    URL parameter from an internal link using “value”
    [attachments are only viewable by logged-in members]

    URL parameter from a “re-order” link created during form submission using “label”
    [attachments are only viewable by logged-in members]

    hopefully this explains what I am trying to do and where the problem is.

    Thanks

    David

  • Nithin Ramdas
    • Support Wizard

    HI David Pearce ,

    Just to be sure we are on the same page, you are looking to add the value in the email for the select field but at the moment the label is what’s getting added, right?

    I checked regarding this but I’m afraid there isn’t any easy solution that I could be suggested at the moment. I’m bringing this with our Forminator team’s attention to see if there is any workaround that could be formulated.

    Will keep you posted once I get further feedback asap.

    Kind Regards,
    Nithin

  • Nithin Ramdas
    • Support Wizard

    Hi David Pearce ,

    Could you please try this snippet and then check whether it works? The snippet should help with changing the label to value. So the link should work out of the box once the code is added.

    <?php
    
    add_filter( 'forminator_replace_form_data', 'wpmudev_get_fields_value' ,10 ,3);
    
    function wpmudev_get_fields_value($content, $data, $fields){
    	$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
    	$data_field = '';
    	foreach($data as $key => $value){
        	if ( strpos( $key, 'radio' ) !== false
    				|| strpos( $key, 'select' ) !== false
    				|| strpos( $key, 'checkbox' ) !== false
    				) {
        		$values = '';
        		$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
    	    	if ( ! is_null( $field_value ) ) {
    	    		$fields_slugs  = wp_list_pluck( $form_fields, 'slug' );
    				$field_key     = array_search( $key, $fields_slugs, true );
    				$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
    						? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
    						: array();
    
    				$selected_values = is_array( $field_value ) ? $field_value : array( $field_value );
    				$values           = implode( ', ', array_keys( array_intersect( array_flip( $field_options ), $selected_values ) ) );
    				
    			}
    
    			if ( is_array( $values ) ) {
    				$values = implode( ', ', $values );
    			}
    			
    			if( is_array( $data[$key] ) ) {
    				$data_field = implode( ', ', $data[$key] );
    			} else {
    				$data_field = $data[$key];
    			}
    			
    			$content = str_replace( 'And_', 'Android', $content );
    			$content = str_replace( 'IOS_', 'iOS', $content );
    			
    			$content = str_replace( $values, $data_field, $content );
    		}
    	}
    
    	$pos = strpos('Android', $content);
    	if ($pos !== false) {
    	    $content = substr_replace($content, 'And_', $pos, strlen('Android'));
    	}
    
    	$position = strpos('iOS', $content);
    	if ($position !== false) {
    	    $content = substr_replace($content, 'IOS_', $position, strlen('iOS'));
    	}
    
        return $content;
    }

    The above code can be added as mu-plugins. Please check this link on how to implement the above code as a mu-plugins:
    https://premium.wpmudev.org/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Kind Regards,
    Nithin

  • David Pearce
    • Site Builder, Child of Zeus

    Hello Nithin,

    Thanks – this works perfectly for the link:thumbsup_tone4:

    unfortunately, this has created an unwanted side effect and all the form data sent in the email is now using the “value” not the “label” text

    new email;
    [attachments are only viewable by logged-in members]

    previous email
    [attachments are only viewable by logged-in members]

    I’m not really sure this is fixable, unless I can specify when to use label or value.
    for example, my existing form sends label by default, but I can change it so it sends the value by adding “_value” to the field name.
    So – {select-6} would add the label, but {select-6_value} would add the value.

    I think the easiest way to fix this would be for me to change all the “value” to match the “label” and update all my existing internal links.

    unless you have some more magic:wink:

    Thank you

    David

  • Nithin Ramdas
    • Support Wizard

    Hi David Pearce ,

    I see what you meant, I tried to tweak the code further but it doesn’t help much with targeting only a select field. However, this should be possible I’m pinging our developer to see if there is a workaround that could be suggested based on your request.

    Will keep you posted once I get further feedback asap.

    Kind Regards,
    Nithin

  • David Pearce
    • Site Builder, Child of Zeus

    Thanks Nithin,

    I’ll look at updating the “value” settings to match the “label” – I think this is going to be the easiest solution…

    If a workaround is possible that would be great.

    Thank you for your help with this:thumbsup_tone3:

    David

  • Prashant
    • Staff

    Hi David Pearce ,

    I hope you are well and safe there.

    We have tweaked the code to make it work only for the select fields. Could you please replace the previously shared code with the following code and check if it’s solving your issue?

    add_filter( 'forminator_replace_form_data', 'wpmudev_get_fields_value' ,10 ,3);
    function wpmudev_get_fields_value($content, $data, $fields){
    	$form_fields = Forminator_API::get_form_fields( $data['form_id'] );
    	$data_field = '';
    	foreach($data as $key => $value){
        	if ( strpos( $key, 'select' ) !== false ) {
        		$values = '';
        		$field_value = isset( $data[ $key ] ) ? $data[ $key ] : null;
    	    	if ( ! is_null( $field_value ) ) {
    	    		$fields_slugs  = wp_list_pluck( $form_fields, 'slug' );
    				$field_key     = array_search( $key, $fields_slugs, true );
    				$field_options = false !== $field_key && ! empty( $form_fields[ $field_key ]->raw['options'] )
    						? wp_list_pluck( $form_fields[ $field_key ]->options, 'label', 'value' )
    						: array();
    				$selected_values = is_array( $field_value ) ? $field_value : array( $field_value );
    				$values           = implode( ', ', array_keys( array_intersect( array_flip( $field_options ), $selected_values ) ) );
    			}
    			if ( is_array( $values ) ) {
    				$values = implode( ', ', $values );
    			}
    			if( is_array( $data[$key] ) ) {
    				$data_field = implode( ', ', $data[$key] );
    			} else {
    				$data_field = $data[$key];
    			}
    			$content = str_replace( $values, $data_field, $content );
    		}
    	}
        return $content;
    }

    Kind regards
    Prashant

  • David Pearce
    • Site Builder, Child of Zeus

    Thanks Prashant,

    Unfortunately this still has the same side effect with the form data – the link works perfectly, but all the form field data also shows the value.
    [attachments are only viewable by logged-in members]

    I think I am going to have to change the value in these fields to match the label.

    Ta

    David

  • David Pearce
    • Site Builder, Child of Zeus

    Thanks Nebu,

    I’m going to hold off re-doing all my select values as I have so many internal links and visibility settings already set-up and if I change the values then I will have to re-do all of these.

    I am working on a temporary fix where I have (hidden) HTML fields set-up that will have the “select” label and I will then use this as in the email to show the correct information.
    Not ideal, but early testing looks to work – this just means I will have to manually configure the email content instead of just including {all_non_empty_fields}.

    Ta

    David

  • Prashant
    • Staff

    Hi David Pearce ,

    We have checked further and found a workaround for your issue.

    Please replace the select field macros in the URL like this: https://premierbites.co.uk/business-and-office-catering/order-process/?menu={value-select-10}&opt1={value-select-6}&usname={name-1}&coname={text-2}&usnumber={phone-1}&usemail={email-1}#order . You can see here we have replaced {select-6} with {value-select-6} and {select-10} with {value-select-10}. After this please replace the previously given code with the following code:

    add_filter( 'forminator_replace_form_data', 'wpmudev_replace_select_value' ,10 ,3);
    function wpmudev_replace_select_value( $content, $data, $fields ){
    	if ( strpos( $content, '{value-select-6}' ) !== false && strpos( $content, '{value-select-10}' ) !== false) {
    		$content = str_replace( '{value-select-6}', $data['select-6'], $content );
    		$content = str_replace( '{value-select-10}', $data['select-10'], $content );
    	}
        return $content;
    }

    Hope this will fix your issue.

    Kind regards
    Prashant

  • David Pearce
    • Site Builder, Child of Zeus

    Hi Prashant,

    Thank you for this, I have just sent a test order and it works perfectly:thumbsup_tone3:

    On this form I have various other fields that are going to need to send their value;
    These are all “select” fields, can I amended the code above adding their field names?

    I may also need to do something similar with “radio” fields – could something similar work for these?

    Thank you

    David

  • Nebu John
    • FLS

    Hi David Pearce ,

    On this form I have various other fields that are going to need to send their value;
    These are all “select” fields, can I amended the code above adding their field names?

    Yes, that will work, you just need to tweak the above code with additional select fields.

    I may also need to do something similar with “radio” fields – could something similar work for these?

    As same as the above, just tweak the code with radio field names, that should work.

    Feel free to get back to us if you need any further assistance with this.

    Kind Regards,
    Nebu John

  • David Pearce
    • Site Builder, Child of Zeus

    Thanks Nebu,

    I’m struggling to make this work once I have added an additional “select” field to the code – could you check if I am doing this correctly please?

    I have added “select-1”

    <?php
    
    add_filter( 'forminator_replace_form_data', 'wpmudev_replace_select_value' ,10 ,3);
    function wpmudev_replace_select_value( $content, $data, $fields ){
    if ( strpos( $content, '{value-select-6}' ) !== false && strpos( $content, '{value-select-10}' ) !== false && strpos( $content, '{value-select-1}' ) !== false) {
    $content = str_replace( '{value-select-6}', $data['select-6'], $content );
    $content = str_replace( '{value-select-10}', $data['select-10'], $content );
    $content = str_replace( '{value-select-1}', $data['select-1'], $content );
    }
    return $content;
    }

    I have different email notifications set-up and {select-1} is on a different email notification to {select-6}. they both have {select-10}

    the 1st email notification has this link;
    https://premierbites.co.uk/business-and-office-catering/order-process/?menu={value-select-10}&opt1={value-select-6}&usname={name-1}&coname={text-2}&usnumber={phone-1}&usemail={email-1}#order

    the 2nd email notification has this link;
    https://premierbites.co.uk/business-and-office-catering/order-process/?menu={value-select-10}&opt1={value-select-1}&usname={name-1}&coname={text-2}&usnumber={phone-1}&usemail={email-1}#order

    The first email notification worked fine with your code, but it no longer works with my updates – it just sends the following “?menu=%7bvalue-select-10%7d&opt1=%7bvalue-select-6%7d”

    Thanks

    David

  • Nebu John
    • FLS

    Hi David Pearce ,

    Since support access to your website isn’t enabled, I wasn’t able to give a closer look at the form. Could you please grant support staff access so we could give a closer look at the form and help you with it?

    You can grant access from WPMU DEV >> Support >> Support Access >> Grant Access, or check this manual: https://premium.wpmudev.org/docs/getting-started/getting-support/#chapter-5

    Please let us know once you enable access so that we could get this sorted.

    Kind Regards,
    Nebu John

  • David Pearce
    • Site Builder, Child of Zeus

    Hi Nebu,

    Support access is granted.

    I’m not sure what info you need;

    The form is call “Business Lunch Enquiry”

    I will need all the various “select” fields to send their “value” in links – but these links will be different as this form deals with quite a few different submission options.

    If you look in the email notifications the “Order Email – Packed-Lunch” worked fine with your code and the “Order Email – Set-Menu” is the one I have added – neither now work!

    The “Re-Order Link” in the “Body” of the notification is where the link text is.

    Ta

    David

  • Nebu John
    • FLS

    Hi David Pearce ,

    I am afraid, I wasn’t able to give a closer look at this as the Defender blocked my IP from accessing your website.

    [attachments are only viewable by logged-in members]

    Can you please disable the location block at WP Dashboard >> Defender Pro >>Firewall >> IP Banning so that we can check this further?

    Looking forward to hearing back from you.

    Kind Regards,
    Nebu John

  • Olivija Guzelyte
    • WP Core Meltdown

    Hi there David Pearce

    Thank you for your response!

    I had a look at this and I experienced a lot of unexpected behavior. I exported the form onto my own lab site and inspected the ‘Text’ version of the email body text and saw that the link is actually added with some extra characters in the middle:

    https://premierbites.co.uk/business-and-office-catering/order-process/?menu={value-select-10}&opt1={value-select-6}&usname={name-1}&coname={text-2}&usnumber={phone-1}&usemail={email-1}#order

    notice the ‘&’ character instead of just &. I thought that was causing the issue, however, it was working without issues for one of the notification emails. I tried to remove those additional characters but realized they would still be added in even after saving the form. I then tried to copy over the link URL and add it as an additional link into the email body field. After that the link got fixed and it lead me to the correct place i.e.:

    https://premierbites.co.uk/business-and-office-catering/order-process/?menu=PLB1&opt1=2&usname=admin&coname=test&usnumber=000&usemail=test@test.com#order

    However, then the other email notification broke. I was only able to get it to work consistently when I removed the if statement as such:

    add_filter( 'forminator_replace_form_data', 'wpmudev_replace_select_value' ,10 ,3);
    function wpmudev_replace_select_value( $content, $data, $fields ){
    
    		$content = str_replace( '{value-select-6}', $data['select-6'], $content );
    		$content = str_replace( '{value-select-10}', $data['select-10'], $content );
                     $content = str_replace( '{value-select-1}', $data['select-1'], $content );
    
        return $content;
    }

    However, I then discovered that the email is still showing the value and not the label for all the email fields, ref. below:

    [attachments are only viewable by logged-in members]

    regardless of the changes we make to this new code snippet. Therefore, I have forwarded this to our developers once more. We will inform you on the ticket as soon as there’s any progress regarding this.

    Kind regards,
    Olivija

  • David Pearce
    • Site Builder, Child of Zeus

    Hello Olivija,

    Thank you for your time and effort looking in to this – it is greatly appreciated:thumbsup_tone4:

    I will leave this with you and hopefully a “fix” can be found as this initial code was very promising.

    Thank you

    David

  • Olivija Guzelyte
    • WP Core Meltdown

    Hi there David Pearce

    Thank you for your response!

    Indeed, it does look promising! The complexity, I reckon, is changing the field value just for the URL, as it seems to still change the field value for the text itself.

    Our SLS team is looking into this now. Thank you for your patience while we are checking this for you.

    Have a nice day!

    Kind regards,
    Olivija

  • Prashant
    • Staff

    Hi David Pearce ,

    I hope you are well and safe there.

    Could you please share the FTP and WordPress login details of the site so that we can check what’s happening there?

    Note: Don’t leave your login details in this ticket.
    Instead, you can send me your details using our contact form https://premium.wpmudev.org/contact/#i-have-a-different-question and the template below:

    Subject: "Attn: Prashant"
    - Admin login:
    	- Admin username
    	- Admin password
    	- Login url
    - FTP or cPanel credentials (host/username/password)
    - Folder path to site in question
    - Link back to this thread for reference

    Kind regards
    Prashant