• Resolved tomicakorac

    (@tomicakorac)


    Hi everyone,

    I have a simple page with one and the same CF7 form in two places, at top and bottom. The clients require redirecting to another page after submission, but so that the top form should redirect to one page, and the bottom form should redirect to another.

    I know I could just create two identical forms and solve this, but I would like to see if it’s possible to detect which of the forms was submitted, and then have a conditional redirection based on that?

    Thanks in advance.

    https://www.ads-software.com/plugins/contact-form-7/

Viewing 1 replies (of 1 total)
  • Thread Starter tomicakorac

    (@tomicakorac)

    I found a solution to my problem myself. If enyone else needs it, here it is:

    1. Install and activate ‘Contact Form 7 Modules’ plugin: https://www.ads-software.com/plugins/contact-form-7-modules/
    2. Using the CF7 Modules plugin insert a hidden field in the form.
    3. Insert this javascript in the form page template:

    <script type="text/javascript">
    jQuery(document).ready(function($) {
    	$('#first-form-id #hidden-field-id').each(function(i) {
    	    $(this).attr('id', 'hidden-field-id-first');
    	    $(this).attr('value', 'first-form');
    	});
    	$('#bottom-form #redirect-hidden-field').each(function(i) {
    	    $(this).attr('id', 'hidden-field-id-second');
    	    $(this).attr('value', 'second-form');
    	});
    });
    </script>

    4. In your functions.php insert this code:

    /**
     * Redirect Contact Form 7 submissions
     */
    function wpcf7_redirections ($contact_form) {
    
    	$submission = WPCF7_Submission::get_instance();
    
    	$redirect_field_value = $submission->get_posted_data( 'redirect-field-name' );
    
    	// Conditional redirects
    	if ( $submission ) {
    
    		if ( $redirect_field_value == 'first-condition' ) {
    
    			$redirect_url = 'https://first-redirect.com';
    
    		} else {
    
    			$redirect_url = 'https://second-redirect.com';
    
    		}
    
    		$contact_form->set_properties( array( 'additional_settings' => "on_sent_ok: \"location = '" . $redirect_url . "';\"" ) );
    
    	}
    
    }
    
    add_action("wpcf7_before_send_mail", "wpcf7_redirections");

Viewing 1 replies (of 1 total)
  • The topic ‘Same form twice on the same page, but two different redirects’ is closed to new replies.