Skip Navigation

[Resolved] Go to Specific Page based on Form Field Value

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

Problem:

The issue here is that the user wanted to redirect their form submissions based on a custom field value.

Solution:

We can actually do this with our redirect hook in CRED.

Add the following to your Toolset custom code and activate it. The toolset custom code section can be found in Toolset -> Settings -> Custom Code.

add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data)
{
    if ($form_data['id']==12 && isset($_POST['wpcf-custom-field'])){
       if($_POST['wpcf-custom-field'] == 'some value'){
        return 'http://newlocation';
}
   } 
    return $url;
}

The code above is the basic way to do a redirect based on the custom field value. What you need to do is to replace the 'wpcf-custom-field' with the slug of your custom field keeping the wpcf- prefix.

This support ticket is created 5 years, 7 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 6 replies, has 2 voices.

Last updated by shawnW-3 5 years, 7 months ago.

Assisted by: Shane.

Author
Posts
#1244144
form-toolset.JPG

Tell us what you are trying to do?
When the user clicks submit, they go to specific page BASED on how they answer a question. The option "Go to a Specific Page" already exists, I just need to make that specific to be determined by how the user answers the question.

Is there any documentation that you are following?
Not able to find anything specific to this.

Is there a similar example that we can see?
Not with Toolset.

#1244211

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Shawn,

Thank you for contacting our support forum. We can actually do this with our redirect hook in CRED.

Add the following to your Toolset custom code and activate it. The toolset custom code section can be found in Toolset -> Settings -> Custom Code.


add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data)
{
    if ($form_data['id']==12 && isset($_POST['wpcf-custom-field'])){
       if($_POST['wpcf-custom-field'] == 'some value'){
        return '<em><u>hidden link</u></em>';
}
   } 
    return $url;
}

The code above is the basic way to do a redirect based on the custom field value. What you need to do is to replace the 'wpcf-custom-field' with the slug of your custom field keeping the wpcf- prefix.

Please try this and let me know if it helps.
Thanks,
Shane

#1244214

Sounds like a good fix. I'll test it shortly.

One question though, Since it's going in the custom code section of the Settings area, I'm thinking it won't be exported if I make a module. Is this correct?

#1244215

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Shawn,

Actually no it won't be exported.

#1244233
supportformpath.JPG
postsettings.JPG

So here's the code I added as a snippet to the toolset custom code in settings, and below that is the form. When it didn't work the first time I changed the id from 12 to 1056 as that's the number of the form.

<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.
add_filter('cred_success_redirect', 'custom_redirect',10,3);
function custom_redirect($url, $post_id, $form_data)
{
if ($form_data['id']==1056 && isset($_POST['solve_issue_director'])){
if($_POST['solve_issue_director'] == 'problemsolved'){
return 'hidden link';
}
if($_POST['solve_issue_director'] == 'problempersists'){
return 'hidden link';
}
}
return $url;
}
?>

--------------------------

FORM Code

[credform]
[cred_field field='form_messages' class='alert alert-warning']
<div class="form-group">
<label>Troubleshooting Support Checklist Title</label>
[cred_field field='post_title' class='form-control' output='bootstrap']
</div>
<div class="form-group">
<label>Troubleshooting Support Checklist Content</label>
[cred_field field='post_content' output='bootstrap']
</div>
<div class="form-group">
<label>Have you tried turning it off and on again?</label>
[cred_field field='off-on-example' force_type='field' class='form-control' output='bootstrap']
</div>

[cred_field field='recaptcha' class='form-control' output='bootstrap']
[cred_generic_field type='radio' field='solve_issue_director']
{
"required":0,
"default":[],
"options":[{"value":"problemsolved","label":"Problem Solved"},{"value":"problempersists","label":"Problem Persists"}]
}

[/cred_generic_field]
[cred_field field='form_submit' output='bootstrap' value='Submit' class='btn btn-primary btn-lg']
[/credform]

#1244248

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Shawn,

Try it like this.


[cred_field field='recaptcha' class='form-control' output='bootstrap']
[cred_generic_field type='radio' field='solve_issue_director']
{
"required":0,
"persist":1,
"default":[],
"options":[{"value":"problemsolved","label":"Problem Solved"},{"value":"problempersists","label":"Problem Persists"}]
}

[/cred_generic_field]
[

Noticed i've added a persist:1 attribute.

Also please note this disclaimer.

Please note that for the redirection hooks to work, the form must be set to redirect to a specific page or a post after the submission. If for example, the form options are set to “Keep displaying this form” or “Display a message instead of the form” after submission, the redirection hooks will not work.

If this still doesn't work please let me know .

Thanks,
Shane

#1246018

That did it! Thank you!