Forminator conditional logic for the redirect URL

Can you please help with adding conditional logic for redirect URL?

  • Patrick Freitas
    • FLS

    Hi Dave

    Hope you are doing well.

    Unfortunately, this feature is not possible at this moment using Forminator, but we already have this feature requested.

    However, we escalated this request to our Second Line Support to check if we can create a code for this.

    Note, the second line support team deal with a more complex situation, and it can cause a delay on the response. Thank you for understanding.

    Best Regards
    Patrick Freitas

  • Panos
    • SLS

    Hi Dave !

    Apologies but it wasn’t very clear what you need to achieve here. I’m not sure if you need some redirection when a quiz is submitted or a form.

    For quiz you could try out the following snippet:

    add_action( 'wp_footer', function(){
    
    	global $post;
    
    	if ( ! $post instanceof WP_Post || ! has_shortcode( $post->post_content, 'forminator_quiz' ) ) {
    		return;
    	}
    
    	$score = 3;
    	$redirect_to = 'http://site.com/success_page';
    
    	?>
    	<script type="text/javascript">
    
    		(($,d)=>{
    
    			if ( window.forminator_quiz_result_monitor ) {
    				return;
    			}
    
    			window.forminator_quiz_result_monitor = {
    
    				init: function() {
    
    					$(d).ajaxComplete( function( event, xhr, settings ) {
    
    						let _this = forminator_quiz_result_monitor;
    
    						if ( 'forminator_submit_form_quizzes' !== _this.get_query_var( 'action', unescape( settings.data ) ) ) {
    							return;
    						}
    
    						let response_text = JSON.parse( xhr.responseText ),
    							score_limit = <?php echo $score; ?>,
    							redirect_to = '<?php echo $redirect_to; ?>',
    							correct_answers = 0,
    							incorrect_answers = 0;
    
    						if ( 
    							typeof response_text.data === 'undefined' ||
    							typeof response_text.data.result === 'undefined'
    						) {
    							return;
    						}
    
    						for( var key in response_text.data.result ) {
    
    							if ( response_text.data.result[key]['isCorrect'] ) {
    								correct_answers++;
    							} else {
    								incorrect_answers++;
    							}
    
    						}
    
    						if ( correct_answers >= score_limit ) {
    							window.location.replace( redirect_to );
    						}
    
    					});                    
    				},
    
    				get_query_var: function( name, url ) {
    					if ( ! url ) {
    						return false;
    					}
    					name = name.replace(/[\[\]]/g, "\\$&");
    					var regex = new RegExp(name + "(=([^&#]*)|&|#|$)"),
    						results = regex.exec(url);
    					//if (!results) return false;
    					if (!results[2]) return false;
    					return decodeURIComponent( results[2].replace(/\+/g, " ") );
    				}
    
    			};
    
    			$(d).ready( function(){
    				forminator_quiz_result_monitor.init();
    			} );
    
    		})(jQuery,document);
    
    	</script>
    	<?php
    }, 9999 );

    where you would need to edit these lines :

    	$score = 3;
    	$redirect_to = 'http://site.com/success_page';

    In case you need a redirect on a form, could you specify what the logic would be? For example a number or calculations field should be bigger than some number?

    If you like to test the snippet shared above, you can paste it in a mu-plugin. If you are not familiar with mu-plugins you can read about them here:
    https://wpmudev.com/docs/getting-started/download-wpmu-dev-plugins-themes/#create-the-mu-plugin-file

    I would strongly recommend to first try this out in a staging/testing site.

    In case you wanted the redirect to be applied on a form, just share some more info and we’ll try figure something out :slight_smile:

    Kind regards!