Skip Navigation

[Resolved] CRED user form conditional redirect

This thread is resolved. Here is a description of the problem and solution.

Problem:
CRED user form conditional redirect

Solution:
The thing is that you were using user forms so instead of get_post_meta() function we need to use get_user_meta() function.

You can find proposed solution, in this case, with the following reply
https://toolset.com/forums/topic/cred-user-form-conditional-redirect/#post-956463

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect

This support ticket is created 6 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by andrewS-10 6 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#955728

I have created a CRED user form. On submission, I would like to redirect to a URL conditional on the selection of radio buttons.

This is the code I am using in my theme's functions.php:

add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data)
{
    if ($form_data['id'] == 22651 ){ // this is my form ID
		
        $url = '<em><u>hidden link</u></em>';
        if ( $_REQUEST['wpcf-sub_type'] == 2 ){
            $url = '<em><u>hidden link</u></em>';
        }
    }
    return $url;
}

Here's the code used in the form:

[creduserform class='cred-user-form cred-keep-original']

	[cred_field field='form_messages' value='']

	<div class="form-group">
		<label>Email</label>
		[cred_field field='user_email' value='' urlparam='']
	</div>

	<div class="form-group">
		<label>First Name</label>
		[cred_field field='first_name' value='' urlparam='']
	</div>

	<div class="form-group">
		<label>Last Name</label>
		[cred_field field='last_name' value='' urlparam='']
	</div>

[cred_generic_field field='sub_type' type='radio' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[
{"value":"1","label":"UK"},
{"value":"2","label":"US"}
]
}
[/cred_generic_field]

	[cred_field field='form_submit' value='Submit' urlparam='']

[/creduserform]

The problem is that the form does NOT redirect correctly (google.com) when the radio button value "2" is selected. The form redirects to bbc.co.uk regardless of which radio button is selected.

#956358

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - the issue looks like here is that you are using wrong field name. For generic field - there is no need to prefix the field name with wpcf-

Could you please try to use following code - it should work.

add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data)
{
    if ($form_data['id'] == 22651 ){ // this is my form ID
         
       $type = get_post_meta($post_id,'sub_type',true);
        $url = '<em><u>hidden link</u></em>';
        if ( $type == 2 ){
            $url = '<em><u>hidden link</u></em>';
        }
    }
    return $url;
}
#956366

Hi Minesh. Many thanks for the reply.

I've tried your amended code, but the problem is still persisting, unfortunately. The form still redirects to hidden link even when the radio button, value ==2, is selected.

#956421

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I need to check whats going wrong there.

Could you please share problem URL where you added the form and access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#956449

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I think you forget to share the problem URL where you placed the CRED form. Could you please share it.

#956450

Oops. Here you are:

hidden link

#956463

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I've added the attribute "persist":1, to your generic filed as given under:

[cred_generic_field field='sub_type' type='radio' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"persist":1,
"options":[
{"value":"1","label":"UK"},
{"value":"2","label":"US"}
]
}
[/cred_generic_field]

The thing is that you were using user forms so instead of get_post_meta() function we need to use get_user_meta() function.

I've adjust the code as given under:

add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data){
    if ($form_data['id'] == 22651 ){ // this is my form ID
		
       $type = get_user_meta($post_id,'sub_type',true);
	$url = '<em><u>hidden link</u></em>';
        if ( $type == 2 ){
            $url = '<em><u>hidden link</u></em>';
        }
    }
    return $url;
}

I can see its working fine. Could you please confirm.

#956487

It works! Thank you Minesh. Problem fixed.