[Forminator Pro] Forminator Capability

Hi

Is It possible for Forminator Pro to do redirects based on conditional logic like Gravity Forms can?

For instance if a user picks a certain option I want them to upon submission redirect to page 1. If they pick option 2 then redirect them to page 2.

Please add this an feature request or welcome any workarounds. I’d like to save money and use Forminator instead of Gravity Form but I cant until I can do this with it

  • Patrick Freitas
    • FLS

    Hi Kieran

    I hope you are doing well.

    We do have plans to create a more complex redirect on the plugin for the submission behaviour, I sent this ticket to the plugin developers as one extra request.

    But you can create some dynamic URL using the select or any field value, for example:

    [attachments are only viewable by logged-in members]

    Add the page slug as the value, and enter this field on the behaviour > redirect option.

    [attachments are only viewable by logged-in members]

    To find more:
    https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#behavior-forms

    Let us know if you have any additional question.
    Best Regards
    Patrick Freitas

  • Patrick Freitas
    • FLS

    Hi Kieran

    Could you please try this code?

    <?php
    /**
     * Plugin Name: [Forminator] - Custom Form Redirect
     * Plugin URI: https://wpmudev.com/
     * Description: Adds custom redirect to Forms based on submitted data
     * Task: SLS-183
     * Version: 1.0.0
     * Author: Panos Lyrakis @ WPMUDEV
     * Author URI: https://wpmudev.com/
     * License: GPLv2 or later
     */
    if ( ! defined( 'ABSPATH' ) ) { 
    	exit; 
    } 
    if ( defined( 'WP_CLI' ) && WP_CLI ) { 
    	return; 
    }
    if ( ! class_exists( 'WPMUDEV_Forminator_CustomForm_Redirect' ) ) {
        class WPMUDEV_Forminator_CustomForm_Redirect {
        	private $form_id = 8863;
        	private $redirect_urls = array(
                'option-value-1' => 'https://your-site.com/thannk-you-1',
                'option-value-2' => 'https://your-site.com/thannk-you-2',
                'option-value-3' => 'https://your-site.com/thannk-you-3',
            );
            private static $_instance = null;
            public static function get_instance() {
                if( is_null( self::$_instance ) ){
                    self::$_instance = new WPMUDEV_Forminator_CustomForm_Redirect();
                }
                return self::$_instance;
            }
            private function __construct() {
            	add_filter( 'forminator_custom_form_submit_response', array( $this, 'handle_response' ), 20, 2 );
            	add_filter( 'forminator_custom_form_ajax_submit_response', array( $this, 'handle_response' ), 20, 2 );
            }
            public function handle_response( $response, $form_id ) {
            	if ( ! $this->is_valid_form( $form_id ) ) {
            		return $response;
            	}
                if( isset( $response['url'] ) ){
                    $response['url'] = $this->calc_url( $response['url'] );
                }
            	return $response;
            }
            private function calc_url( $redirect_url, $data = null ) {
            	$data = ! is_null( $data ) ? $data : $_POST;
            	$field_name = 'select-1';
            	$field_value = isset( $data[ $field_name ] ) ? $data[ $field_name ] : null;
                if( $field_value && isset( $this->redirect_urls[ $field_value ] ) ){
                    $redirect_url = esc_url( $this->redirect_urls[ $field_value ] );
                }
                return $redirect_url;
            }
            private function is_valid_form( $form_id ) {
            	return (int) $form_id === $this->form_id;
            }
        }
    	if ( ! function_exists( 'wpmudev_forminator_customform_redirect' ) ) {
        	function wpmudev_forminator_customform_redirect(){    		
        		return WPMUDEV_Forminator_CustomForm_Redirect::get_instance();
        	};
        	add_action( 'plugins_loaded', 'wpmudev_forminator_customform_redirect', 99 );
        }
    }

    Edit the private $form_id = 8863; using your Form ID:

    [attachments are only viewable by logged-in members]

    and update the value vs URL at:

    private $redirect_urls = array(
                'option-value-1' => 'https://your-site.com/thannk-you-1',
                'option-value-2' => 'https://your-site.com/thannk-you-2',
                'option-value-3' => 'https://your-site.com/thannk-you-3',
            );

    You can use the code as a mu-plugin:
    https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Best Regards
    Patrick Freitas

  • Kieran
    • The Crimson Coder

    Hi Patrick

    This reminds me of what I was doing with Gravity Forms a while back before they integrated into the plugin. Sadly its not working although what setting should the Submission Behaviour tab be set too?

    It doesn’t appear to be over riding that. I’m on a multi site set up too so not sure if that affects it in any way?

    If the code does work I think this would suit my needs though. However I will need it eventually to do multiple confirmations with conditional logic that can do OR/AND commands.

    For instance if Field 1 was X AND Field 2 was Y then go to page 1

    Thanks for response

    Kind Regards
    Kieran

  • Patrick Freitas
    • FLS

    Hi Kieran

    You can define any URL on that redirect because the code is going to replace it using the conditional defined on;

    private $redirect_urls = array(
                'option-value-1' => 'https://your-site.com/thannk-you-1',
                'option-value-2' => 'https://your-site.com/thannk-you-2',
                'option-value-3' => 'https://your-site.com/thannk-you-3',
            );

    Then the code should work as expected, unfortunately, as I told, it is limited to one field.

    Best Regards
    Patrick Freitas

  • Kieran
    • The Crimson Coder

    Hi Kasia

    Support is enabled.

    Test page is https://warringtonpainclinic.co.uk/forminator-pro-test/

    The form on the left is Forminator (not working) and the middle is gravity forms which is working

    Code is

    
    <?php
    /**
     * Plugin Name: [Forminator] - Custom Form Redirect
     * Plugin URI: https://wpmudev.com/
     * Description: Adds custom redirect to Forms based on submitted data
     * Task: SLS-183
     * Version: 1.0.0
     * Author: Panos Lyrakis @ WPMUDEV
     * Author URI: https://wpmudev.com/
     * License: GPLv2 or later
     */
    if ( ! defined( 'ABSPATH' ) ) { 
    	exit; 
    } 
    if ( defined( 'WP_CLI' ) && WP_CLI ) { 
    	return; 
    }
    if ( ! class_exists( 'WPMUDEV_Forminator_CustomForm_Redirect' ) ) {
        class WPMUDEV_Forminator_CustomForm_Redirect {
        	private $form_id = 111;
        	private $redirect_urls = array(
                '90' => 'https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-90-mins/',
                '60' => 'https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins/',
                '45' => 'https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-45-mins/',
            );
            private static $_instance = null;
            public static function get_instance() {
                if( is_null( self::$_instance ) ){
                    self::$_instance = new WPMUDEV_Forminator_CustomForm_Redirect();
                }
                return self::$_instance;
            }
            private function __construct() {
            	add_filter( 'forminator_custom_form_submit_response', array( $this, 'handle_response' ), 20, 2 );
            	add_filter( 'forminator_custom_form_ajax_submit_response', array( $this, 'handle_response' ), 20, 2 );
            }
            public function handle_response( $response, $form_id ) {
            	if ( ! $this->is_valid_form( $form_id ) ) {
            		return $response;
            	}
                if( isset( $response['url'] ) ){
                    $response['url'] = $this->calc_url( $response['url'] );
                }
            	return $response;
            }
            private function calc_url( $redirect_url, $data = null ) {
            	$data = ! is_null( $data ) ? $data : $_POST;
            	$field_name = 'Select Treatment Length';
            	$field_value = isset( $data[ $field_name ] ) ? $data[ $field_name ] : null;
                if( $field_value && isset( $this->redirect_urls[ $field_value ] ) ){
                    $redirect_url = esc_url( $this->redirect_urls[ $field_value ] );
                }
                return $redirect_url;
            }
            private function is_valid_form( $form_id ) {
            	return (int) $form_id === $this->form_id;
            }
        }
    	if ( ! function_exists( 'wpmudev_forminator_customform_redirect' ) ) {
        	function wpmudev_forminator_customform_redirect(){    		
        		return WPMUDEV_Forminator_CustomForm_Redirect::get_instance();
        	};
        	add_action( 'plugins_loaded', 'wpmudev_forminator_customform_redirect', 99 );
        }
    }
    
  • Kasia Swiderska
    • Support nomad

    Hello Kieran ,

    I’ve checked your code and there is one error there. As Patrick mentions here https://wpmudev.com/forums/topic/forminator-pro-forminator-capability/#post-3862347 $field_name needs to be updated. You have updated it to label name:
    $field_name = 'Select Treatment Length';
    and not to the field name, like in example:
    $field_name = 'select-1';

    [attachments are only viewable by logged-in members]

    You will need to correct that. I’ve tested the code with the correction on my site and it works fine.

    kind regards,
    Kasia

  • Kieran
    • The Crimson Coder

    Hi Kasia

    Sorry but I’m trying to do the redirect for more than one form. I’ve tried copying the entire code and creating another muplugin but that doesn’t work. How do I get it working with more than 1 form please

    Kind Regards
    Kieran

  • Rupok
    • Ex Staff

    Hi Kieran,

    If you need to use that code for multiple forms, please use this code:

    <?php
    /**
     * Plugin Name: [Forminator] - Custom Form Redirect Based On Submitted Form Data
     * Plugin URI: https://premium.wpmudev.org/
     * Description: Adds custom redirect to Forms based on submitted data
     * Task: SLS-1147
     * Version: 1.0.0
     * Author: Panos Lyrakis & Ohid @ WPMUDEV
     * Author URI: https://premium.wpmudev.org/
     * License: GPLv2 or later
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    if ( defined( 'WP_CLI' ) && WP_CLI ) {
    	return;
    }
    
    if ( ! class_exists( 'WPMUDEV_Forminator_CustomForm_Redirect' ) ) {
    
    	class WPMUDEV_Forminator_CustomForm_Redirect {
    
    		private $config_json = '{
    
                // This 38 is a sample form ID. Please use your own form ID.
                "38":[
                   {
    // Replace "select-1" with your target select field ID
                      "field":"select-1",
                      "values":[
                         {
    // "initial-consultation-29-for-30-minutes" is a sample form value and "http://superdev.test/redirect url-one" is the URL where you want the user to be redirected when that value is selected in the form.
                            "initial-consultation-29-for-30-minutes":"http://superdev.test/redirect-url-one",
                            "spiritual-guidance-200-for-60-minutes":"http://superdev.test/redirect-url-two",
                            "intuitive-guidance-coaching-pryzma-clarity-session-50000-2-hours":"http://superdev.test/redirect-url-three",
                            "pryzma-life-mapping-50000-2-hours":"http://superdev.test/redirect-url-four",
                            "pryzma-pathway-plan-97500-2-x-2hours-sessions":"http://superdev.test/redirect-url-five",
                            "30minute-power-session-with-keda-6000":"http://superdev.test/redirect-url-six",
                            "monthly-power-sessions-annual-19900":"http://superdev.test/redirect-url-seven",
                            "pilot-true-ii-mentorship-programme-250000":"http://superdev.test/redirect-url-eight"
                         }
                      ]
                   }
                ],
    
                // This 39 is a sample form ID. Please use your own form ID.
                "39":[
                   {
    // Replace "select-1" with your target select field ID
                      "field":"select-1",
                      "values":[
                         {
    // "initial-consultation-29-for-30-minutes" is a sample form value and "http://superdev.test/redirect url-one" is the URL where you want the user to be redirected when that value is selected in the form.
                            "initial-consultation-29-for-30-minutes":"http://superdev2.test/redirect-url-one",
                            "spiritual-guidance-200-for-60-minutes":"http://superdev2.test/redirect-url-two",
                            "intuitive-guidance-coaching-pryzma-clarity-session-50000-2-hours":"http://superdev2.test/redirect-url-three",
                            "pryzma-life-mapping-50000-2-hours":"http://superdev2.test/redirect-url-four",
                            "pryzma-pathway-plan-97500-2-x-2hours-sessions":"http://superdev2.test/redirect-url-five",
                            "30minute-power-session-with-keda-6000":"http://superdev2.test/redirect-url-six",
                            "monthly-power-sessions-annual-19900":"http://superdev2.test/redirect-url-seven",
                            "pilot-true-ii-mentorship-programme-250000":"http://superdev2.test/redirect-url-eight"
                         }
                      ]
                   }
                ]
                
             }';
    
    		private $config           = '';
    		private $redirect_url     = '';
    		private static $_instance = null;
    
    		public static function get_instance() {
    			if ( is_null( self::$_instance ) ) {
    				self::$_instance = new WPMUDEV_Forminator_CustomForm_Redirect();
    			}
    			return self::$_instance;
    
    		}
    
    		private function __construct() {
    
    			if ( ! class_exists( 'Forminator_Custom_Form_Model' ) ) {
    				return;
    			}
    			$this->config = json_decode( $this->config_json );
    
    			add_filter( 'forminator_custom_form_submit_response', array( $this, 'handle_response' ), 20, 2 );
    			add_filter( 'forminator_custom_form_ajax_submit_response', array( $this, 'handle_response' ), 20, 2 );
    
    		}
    
    		public function handle_response( $response, $form_id ) {
    			$form = Forminator_Custom_Form_Model::model()->load( $form_id );
    
    			if ( 'behaviour-redirect' !== $form->settings['submission-behaviour'] ) {
    				return $response;
    			}
    
    			if ( ! is_object( $this->config ) || ! property_exists( $this->config, $form_id ) ) {
    				return $response;
    			}
    
    			if ( ! ! $redirect_url = $this->calc_url( $form_id ) ) {
    				$response['url'] = $redirect_url;
    			}
    
    			return $response;
    		}
    
    		private function calc_url( $form_id = null, $data = null ) {
    			if ( is_null( $form_id ) || ! is_numeric( $form_id ) ) {
    				return false;
    			}
    
    			$form_id       = (int) $form_id;
    			$form_config   = $this->config->$form_id[0];
    			$config_field  = $form_config->field;
    			$config_values = (array) $form_config->values[0];
    			$data          = ! is_null( $data ) ? $data : $_POST;
    
    			return (
    					isset( $data[ $config_field ] ) && ! empty( $data[ $config_field ] ) &&
    					isset( $config_values[ $data[ $config_field ] ] ) && ! empty( $config_values[ $data[ $config_field ] ] )
    				) ?
    				$config_values[ $data[ $config_field ] ] :
    				false;
    		}
    
    	}
    
    	if ( ! function_exists( 'wpmudev_forminator_customform_redirect' ) ) {
    
    		function wpmudev_forminator_customform_redirect() {
    			return WPMUDEV_Forminator_CustomForm_Redirect::get_instance();
    		};
    
    		add_action( 'plugins_loaded', 'wpmudev_forminator_customform_redirect', 99 );
    	}
    }

    I’ve marked in the code above with comments where you should change the values. I’ve tested this code and it’s working fine at my end. Please let us know if you have any confusion or if this doesn’t work for you for any reason. We will be very glad to investigate that and resolve it for you.

    Regards,
    Rupok

  • Kieran
    • The Crimson Coder

    Hi Rupok

    I’ve installed the following code but it doesnt seem to be working. Can you double check on my site please

    <?php
    /**
    * Plugin Name: [Forminator] – Custom Form Redirect Based On Submitted Form Data
    * Plugin URI: https://premium.wpmudev.org/
    * Description: Adds custom redirect to Forms based on submitted data
    * Task: SLS-1147
    * Version: 1.0.0
    * Author: Panos Lyrakis & Ohid @ WPMUDEV
    * Author URI: https://premium.wpmudev.org/
    * License: GPLv2 or later
    */

    if ( ! defined( ‘ABSPATH’ ) ) {
    exit;
    }

    if ( defined( ‘WP_CLI’ ) && WP_CLI ) {
    return;
    }

    if ( ! class_exists( ‘WPMUDEV_Forminator_CustomForm_Redirect’ ) ) {

    class WPMUDEV_Forminator_CustomForm_Redirect {

    private $config_json = ‘{

    // This 38 is a sample form ID. Please use your own form ID.
    “111”:[
    {
    // Replace “select-1” with your target select field ID
    “field”:”select-1″,
    “values”:[
    {
    // “initial-consultation-29-for-30-minutes” is a sample form value and “http://superdev.test/redirect url-one” is the URL where you want the user to be redirected when that value is selected in the form.
    “90”:”https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-90-mins/&#8221;,
    “60”:”https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins/&#8221;,
    “45”:”https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-45-mins/&#8221;
    “pryzma-life-mapping-50000-2-hours”:”http://superdev.test/redirect-url-four&#8221;,
    “pryzma-pathway-plan-97500-2-x-2hours-sessions”:”http://superdev.test/redirect-url-five&#8221;,
    “30minute-power-session-with-keda-6000″:”http://superdev.test/redirect-url-six&#8221;,
    “monthly-power-sessions-annual-19900″:”http://superdev.test/redirect-url-seven&#8221;,
    “pilot-true-ii-mentorship-programme-250000″:”http://superdev.test/redirect-url-eight&#8221;
    }
    ]
    }
    ],

    // This 39 is a sample form ID. Please use your own form ID.
    “646”:[
    {
    // Replace “select-1” with your target select field ID
    “field”:”select-1″,
    “values”:[
    {
    // “initial-consultation-29-for-30-minutes” is a sample form value and “http://superdev.test/redirect url-one” is the URL where you want the user to be redirected when that value is selected in the form.
    “90”:”https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-90-mins/&#8221;,
    “60”:”https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-60-mins/&#8221;,
    “45”:”https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-45-mins/&#8221;,,
    “pryzma-life-mapping-50000-2-hours”:”http://superdev2.test/redirect-url-four&#8221;,
    “pryzma-pathway-plan-97500-2-x-2hours-sessions”:”http://superdev2.test/redirect-url-five&#8221;,
    “30minute-power-session-with-keda-6000″:”http://superdev2.test/redirect-url-six&#8221;,
    “monthly-power-sessions-annual-19900″:”http://superdev2.test/redirect-url-seven&#8221;,
    “pilot-true-ii-mentorship-programme-250000″:”http://superdev2.test/redirect-url-eight&#8221;
    }
    ]
    }
    ]

    }’;

    private $config = ”;
    private $redirect_url = ”;
    private static $_instance = null;

    public static function get_instance() {
    if ( is_null( self::$_instance ) ) {
    self::$_instance = new WPMUDEV_Forminator_CustomForm_Redirect();
    }
    return self::$_instance;

    }

    private function __construct() {

    if ( ! class_exists( ‘Forminator_Custom_Form_Model’ ) ) {
    return;
    }
    $this->config = json_decode( $this->config_json );

    add_filter( ‘forminator_custom_form_submit_response’, array( $this, ‘handle_response’ ), 20, 2 );
    add_filter( ‘forminator_custom_form_ajax_submit_response’, array( $this, ‘handle_response’ ), 20, 2 );

    }

    public function handle_response( $response, $form_id ) {
    $form = Forminator_Custom_Form_Model::model()->load( $form_id );

    if ( ‘behaviour-redirect’ !== $form->settings[‘submission-behaviour’] ) {
    return $response;
    }

    if ( ! is_object( $this->config ) || ! property_exists( $this->config, $form_id ) ) {
    return $response;
    }

    if ( ! ! $redirect_url = $this->calc_url( $form_id ) ) {
    $response[‘url’] = $redirect_url;
    }

    return $response;
    }

    private function calc_url( $form_id = null, $data = null ) {
    if ( is_null( $form_id ) || ! is_numeric( $form_id ) ) {
    return false;
    }

    $form_id = (int) $form_id;
    $form_config = $this->config->$form_id[0];
    $config_field = $form_config->field;
    $config_values = (array) $form_config->values[0];
    $data = ! is_null( $data ) ? $data : $_POST;

    return (
    isset( $data[ $config_field ] ) && ! empty( $data[ $config_field ] ) &&
    isset( $config_values[ $data[ $config_field ] ] ) && ! empty( $config_values[ $data[ $config_field ] ] )
    ) ?
    $config_values[ $data[ $config_field ] ] :
    false;
    }

    }

    if ( ! function_exists( ‘wpmudev_forminator_customform_redirect’ ) ) {

    function wpmudev_forminator_customform_redirect() {
    return WPMUDEV_Forminator_CustomForm_Redirect::get_instance();
    };

    add_action( ‘plugins_loaded’, ‘wpmudev_forminator_customform_redirect’, 99 );
    }
    }

  • Kieran
    • The Crimson Coder

    Sorry meant to post like this

    <?php
    /**
     * Plugin Name: [Forminator] - Custom Form Redirect Based On Submitted Form Data
     * Plugin URI: https://premium.wpmudev.org/
     * Description: Adds custom redirect to Forms based on submitted data
     * Task: SLS-1147
     * Version: 1.0.0
     * Author: Panos Lyrakis & Ohid @ WPMUDEV
     * Author URI: https://premium.wpmudev.org/
     * License: GPLv2 or later
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    if ( defined( 'WP_CLI' ) && WP_CLI ) {
    	return;
    }
    
    if ( ! class_exists( 'WPMUDEV_Forminator_CustomForm_Redirect' ) ) {
    
    	class WPMUDEV_Forminator_CustomForm_Redirect {
    
    		private $config_json = '{
    
                // This 38 is a sample form ID. Please use your own form ID.
                "111":[
                   {
    // Replace "select-1" with your target select field ID
                      "field":"select-1",
                      "values":[
                         {
    // "initial-consultation-29-for-30-minutes" is a sample form value and "http://superdev.test/redirect url-one" is the URL where you want the user to be redirected when that value is selected in the form.
                            "90":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-90-mins/",
                            "60":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins/",
                            "45":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-45-mins/"
                            "pryzma-life-mapping-50000-2-hours":"http://superdev.test/redirect-url-four",
                            "pryzma-pathway-plan-97500-2-x-2hours-sessions":"http://superdev.test/redirect-url-five",
                            "30minute-power-session-with-keda-6000":"http://superdev.test/redirect-url-six",
                            "monthly-power-sessions-annual-19900":"http://superdev.test/redirect-url-seven",
                            "pilot-true-ii-mentorship-programme-250000":"http://superdev.test/redirect-url-eight"
                         }
                      ]
                   }
                ],
    
                // This 39 is a sample form ID. Please use your own form ID.
                "646":[
                   {
    // Replace "select-1" with your target select field ID
                      "field":"select-1",
                      "values":[
                         {
    // "initial-consultation-29-for-30-minutes" is a sample form value and "http://superdev.test/redirect url-one" is the URL where you want the user to be redirected when that value is selected in the form.
                            "90":"https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-90-mins/",
                            "60":"https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-60-mins/",
                            "45":"https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-45-mins/",,
                            "pryzma-life-mapping-50000-2-hours":"http://superdev2.test/redirect-url-four",
                            "pryzma-pathway-plan-97500-2-x-2hours-sessions":"http://superdev2.test/redirect-url-five",
                            "30minute-power-session-with-keda-6000":"http://superdev2.test/redirect-url-six",
                            "monthly-power-sessions-annual-19900":"http://superdev2.test/redirect-url-seven",
                            "pilot-true-ii-mentorship-programme-250000":"http://superdev2.test/redirect-url-eight"
                         }
                      ]
                   }
                ]
                
             }';
    
    		private $config           = '';
    		private $redirect_url     = '';
    		private static $_instance = null;
    
    		public static function get_instance() {
    			if ( is_null( self::$_instance ) ) {
    				self::$_instance = new WPMUDEV_Forminator_CustomForm_Redirect();
    			}
    			return self::$_instance;
    
    		}
    
    		private function __construct() {
    
    			if ( ! class_exists( 'Forminator_Custom_Form_Model' ) ) {
    				return;
    			}
    			$this->config = json_decode( $this->config_json );
    
    			add_filter( 'forminator_custom_form_submit_response', array( $this, 'handle_response' ), 20, 2 );
    			add_filter( 'forminator_custom_form_ajax_submit_response', array( $this, 'handle_response' ), 20, 2 );
    
    		}
    
    		public function handle_response( $response, $form_id ) {
    			$form = Forminator_Custom_Form_Model::model()->load( $form_id );
    
    			if ( 'behaviour-redirect' !== $form->settings['submission-behaviour'] ) {
    				return $response;
    			}
    
    			if ( ! is_object( $this->config ) || ! property_exists( $this->config, $form_id ) ) {
    				return $response;
    			}
    
    			if ( ! ! $redirect_url = $this->calc_url( $form_id ) ) {
    				$response['url'] = $redirect_url;
    			}
    
    			return $response;
    		}
    
    		private function calc_url( $form_id = null, $data = null ) {
    			if ( is_null( $form_id ) || ! is_numeric( $form_id ) ) {
    				return false;
    			}
    
    			$form_id       = (int) $form_id;
    			$form_config   = $this->config->$form_id[0];
    			$config_field  = $form_config->field;
    			$config_values = (array) $form_config->values[0];
    			$data          = ! is_null( $data ) ? $data : $_POST;
    
    			return (
    					isset( $data[ $config_field ] ) && ! empty( $data[ $config_field ] ) &&
    					isset( $config_values[ $data[ $config_field ] ] ) && ! empty( $config_values[ $data[ $config_field ] ] )
    				) ?
    				$config_values[ $data[ $config_field ] ] :
    				false;
    		}
    
    	}
    
    	if ( ! function_exists( 'wpmudev_forminator_customform_redirect' ) ) {
    
    		function wpmudev_forminator_customform_redirect() {
    			return WPMUDEV_Forminator_CustomForm_Redirect::get_instance();
    		};
    
    		add_action( 'plugins_loaded', 'wpmudev_forminator_customform_redirect', 99 );
    	}
    }
  • Kieran
    • The Crimson Coder

    noticed a few strange comma’s. Have corrected that but still no joy. Sorry I cant seem to edit my own posts!?

    <?php
    /**
     * Plugin Name: [Forminator] - Custom Form Redirect Based On Submitted Form Data
     * Plugin URI: https://premium.wpmudev.org/
     * Description: Adds custom redirect to Forms based on submitted data
     * Task: SLS-1147
     * Version: 1.0.0
     * Author: Panos Lyrakis & Ohid @ WPMUDEV
     * Author URI: https://premium.wpmudev.org/
     * License: GPLv2 or later
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    if ( defined( 'WP_CLI' ) && WP_CLI ) {
    	return;
    }
    
    if ( ! class_exists( 'WPMUDEV_Forminator_CustomForm_Redirect' ) ) {
    
    	class WPMUDEV_Forminator_CustomForm_Redirect {
    
    		private $config_json = '{
    
                // This 38 is a sample form ID. Please use your own form ID.
                "111":[
                   {
    // Replace "select-1" with your target select field ID
                      "field":"select-1",
                      "values":[
                         {
    // "initial-consultation-29-for-30-minutes" is a sample form value and "http://superdev.test/redirect url-one" is the URL where you want the user to be redirected when that value is selected in the form.
                            "90":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-90-mins/",
                            "60":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins/",
                            "45":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-45-mins/",
                            "pryzma-life-mapping-50000-2-hours":"http://superdev.test/redirect-url-four",
                            "pryzma-pathway-plan-97500-2-x-2hours-sessions":"http://superdev.test/redirect-url-five",
                            "30minute-power-session-with-keda-6000":"http://superdev.test/redirect-url-six",
                            "monthly-power-sessions-annual-19900":"http://superdev.test/redirect-url-seven",
                            "pilot-true-ii-mentorship-programme-250000":"http://superdev.test/redirect-url-eight"
                         }
                      ]
                   }
                ],
    
                // This 39 is a sample form ID. Please use your own form ID.
                "646":[
                   {
    // Replace "select-1" with your target select field ID
                      "field":"select-1",
                      "values":[
                         {
    // "initial-consultation-29-for-30-minutes" is a sample form value and "http://superdev.test/redirect url-one" is the URL where you want the user to be redirected when that value is selected in the form.
                            "90":"https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-90-mins/",
                            "60":"https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-60-mins/",
                            "45":"https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-45-mins/",
                            "pryzma-life-mapping-50000-2-hours":"http://superdev2.test/redirect-url-four",
                            "pryzma-pathway-plan-97500-2-x-2hours-sessions":"http://superdev2.test/redirect-url-five",
                            "30minute-power-session-with-keda-6000":"http://superdev2.test/redirect-url-six",
                            "monthly-power-sessions-annual-19900":"http://superdev2.test/redirect-url-seven",
                            "pilot-true-ii-mentorship-programme-250000":"http://superdev2.test/redirect-url-eight"
                         }
                      ]
                   }
                ]
                
             }';
    
    		private $config           = '';
    		private $redirect_url     = '';
    		private static $_instance = null;
    
    		public static function get_instance() {
    			if ( is_null( self::$_instance ) ) {
    				self::$_instance = new WPMUDEV_Forminator_CustomForm_Redirect();
    			}
    			return self::$_instance;
    
    		}
    
    		private function __construct() {
    
    			if ( ! class_exists( 'Forminator_Custom_Form_Model' ) ) {
    				return;
    			}
    			$this->config = json_decode( $this->config_json );
    
    			add_filter( 'forminator_custom_form_submit_response', array( $this, 'handle_response' ), 20, 2 );
    			add_filter( 'forminator_custom_form_ajax_submit_response', array( $this, 'handle_response' ), 20, 2 );
    
    		}
    
    		public function handle_response( $response, $form_id ) {
    			$form = Forminator_Custom_Form_Model::model()->load( $form_id );
    
    			if ( 'behaviour-redirect' !== $form->settings['submission-behaviour'] ) {
    				return $response;
    			}
    
    			if ( ! is_object( $this->config ) || ! property_exists( $this->config, $form_id ) ) {
    				return $response;
    			}
    
    			if ( ! ! $redirect_url = $this->calc_url( $form_id ) ) {
    				$response['url'] = $redirect_url;
    			}
    
    			return $response;
    		}
    
    		private function calc_url( $form_id = null, $data = null ) {
    			if ( is_null( $form_id ) || ! is_numeric( $form_id ) ) {
    				return false;
    			}
    
    			$form_id       = (int) $form_id;
    			$form_config   = $this->config->$form_id[0];
    			$config_field  = $form_config->field;
    			$config_values = (array) $form_config->values[0];
    			$data          = ! is_null( $data ) ? $data : $_POST;
    
    			return (
    					isset( $data[ $config_field ] ) && ! empty( $data[ $config_field ] ) &&
    					isset( $config_values[ $data[ $config_field ] ] ) && ! empty( $config_values[ $data[ $config_field ] ] )
    				) ?
    				$config_values[ $data[ $config_field ] ] :
    				false;
    		}
    
    	}
    
    	if ( ! function_exists( 'wpmudev_forminator_customform_redirect' ) ) {
    
    		function wpmudev_forminator_customform_redirect() {
    			return WPMUDEV_Forminator_CustomForm_Redirect::get_instance();
    		};
    
    		add_action( 'plugins_loaded', 'wpmudev_forminator_customform_redirect', 99 );
    	}
    }
  • Rupok
    • Ex Staff

    Hi Kieran,

    We need FTP access so we can check the code and edit if required. Can you please send me a message with access credentials through our secure contact form here: https://premium.wpmudev.org/contact/#i-have-a-different-question

    Subject: “Attn: Rupok”
    – WordPress admin Username
    – WordPress admin Password
    – WordPress Login URL
    – FTP Host
    – FTP Port
    – FTP Username
    – FTP Password
    – Link back to this thread for reference
    – Any other relevant URLs

    The subject line ensures that it gets assigned to me.

    Note: Please DO NOT share login credentials here in forum tickets as our forum is public and anyone can check your forum tickets unless the ticket is made “Private” specifically.

    [attachments are only viewable by logged-in members]

    Please confirm here when you are done. We will be very glad to check this and modify it on your site if required.

    Regards,
    Rupok

  • Rupok
    • Ex Staff

    Hi Kieran,

    As my colleague Panos noticed, while I was trying to put the instructions by adding the comments for you, I actually made a mistake and added the comments inside the JSON. However, I’ve removed the comments and now it’s working fine. Please check here.

    https://warringtonpainclinic.co.uk/forminator-pro-test/

    And this is the working code:

    <?php
    /**
     * Plugin Name: [Forminator] - Custom Form Redirect Based On Submitted Form Data
     * Plugin URI: https://premium.wpmudev.org/
     * Description: Adds custom redirect to Forms based on submitted data
     * Task: SLS-1147
     * Version: 1.0.0
     * Author: Panos Lyrakis & Ohid @ WPMUDEV
     * Author URI: https://premium.wpmudev.org/
     * License: GPLv2 or later
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
    	exit;
    }
    
    if ( defined( 'WP_CLI' ) && WP_CLI ) {
    	return;
    }
    
    if ( ! class_exists( 'WPMUDEV_Forminator_CustomForm_Redirect' ) ) {
    
    	class WPMUDEV_Forminator_CustomForm_Redirect {
    
    		private $config_json = '{
                "111":[
                   {
                      "field":"select-1",
                      "values":[
                         {
                            "90":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-90-mins/",
                            "60":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins/",
                            "45":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-45-mins/",
                            "pryzma-life-mapping-50000-2-hours":"http://superdev.test/redirect-url-four",
                            "pryzma-pathway-plan-97500-2-x-2hours-sessions":"http://superdev.test/redirect-url-five",
                            "30minute-power-session-with-keda-6000":"http://superdev.test/redirect-url-six",
                            "monthly-power-sessions-annual-19900":"http://superdev.test/redirect-url-seven",
                            "pilot-true-ii-mentorship-programme-250000":"http://superdev.test/redirect-url-eight"
                         }
                      ]
                   }
                ],
                "646":[
                   {
                      "field":"select-1",
                      "values":[
                         {
                            "90":"https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-90-mins/",
                            "60":"https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-60-mins/",
                            "45":"https://warringtonpainclinic.co.uk/advanced-clinical-maternity-massage-booking-45-mins/",
                            "pryzma-life-mapping-50000-2-hours":"http://superdev2.test/redirect-url-four",
                            "pryzma-pathway-plan-97500-2-x-2hours-sessions":"http://superdev2.test/redirect-url-five",
                            "30minute-power-session-with-keda-6000":"http://superdev2.test/redirect-url-six",
                            "monthly-power-sessions-annual-19900":"http://superdev2.test/redirect-url-seven",
                            "pilot-true-ii-mentorship-programme-250000":"http://superdev2.test/redirect-url-eight"
                         }
                      ]
                   }
                ]
                
             }';
    
    		private $config           = '';
    		private $redirect_url     = '';
    		private static $_instance = null;
    
    		public static function get_instance() {
    			if ( is_null( self::$_instance ) ) {
    				self::$_instance = new WPMUDEV_Forminator_CustomForm_Redirect();
    			}
    			return self::$_instance;
    
    		}
    
    		private function __construct() {
    
    			if ( ! class_exists( 'Forminator_Custom_Form_Model' ) ) {
    				return;
    			}
    			$this->config = json_decode( $this->config_json );
    
    			add_filter( 'forminator_custom_form_submit_response', array( $this, 'handle_response' ), 20, 2 );
    			add_filter( 'forminator_custom_form_ajax_submit_response', array( $this, 'handle_response' ), 20, 2 );
    
    		}
    
    		public function handle_response( $response, $form_id ) {
    			$form = Forminator_Custom_Form_Model::model()->load( $form_id );
    
    			if ( 'behaviour-redirect' !== $form->settings['submission-behaviour'] ) {
    				return $response;
    			}
    
    			if ( ! is_object( $this->config ) || ! property_exists( $this->config, $form_id ) ) {
    				return $response;
    			}
    
    			if ( ! ! $redirect_url = $this->calc_url( $form_id ) ) {
    				$response['url'] = $redirect_url;
    			}
    
    			return $response;
    		}
    
    		private function calc_url( $form_id = null, $data = null ) {
    			if ( is_null( $form_id ) || ! is_numeric( $form_id ) ) {
    				return false;
    			}
    
    			$form_id       = (int) $form_id;
    			$form_config   = $this->config->$form_id[0];
    			$config_field  = $form_config->field;
    			$config_values = (array) $form_config->values[0];
    			$data          = ! is_null( $data ) ? $data : $_POST;
    
    			return (
    					isset( $data[ $config_field ] ) && ! empty( $data[ $config_field ] ) &&
    					isset( $config_values[ $data[ $config_field ] ] ) && ! empty( $config_values[ $data[ $config_field ] ] )
    				) ?
    				$config_values[ $data[ $config_field ] ] :
    				false;
    		}
    
    	}
    
    	if ( ! function_exists( 'wpmudev_forminator_customform_redirect' ) ) {
    
    		function wpmudev_forminator_customform_redirect() {
    			return WPMUDEV_Forminator_CustomForm_Redirect::get_instance();
    		};
    
    		add_action( 'plugins_loaded', 'wpmudev_forminator_customform_redirect', 99 );
    	}
    }

    Please let us know if you have any further queries. We will be very glad to assist further.

    Regards,
    Rupok

  • Panos
    • SLS

    Hi Kieran ,

    Could you try replacing this part :

    add_filter( 'forminator_custom_form_submit_response', array( $this, 'handle_response' ), 20, 2 );
    add_filter( 'forminator_custom_form_ajax_submit_response', array( $this, 'handle_response' ), 20, 2 );

    with :

    add_filter( 'forminator_form_submit_response', array( $this, 'handle_response' ), 20, 2 );
    add_filter( 'forminator_form_ajax_submit_response', array( $this, 'handle_response' ), 20, 2 );

    Then let us know if it still is’t working.

    Kind regards!

  • Patrick Freitas
    • FLS

    Hi Kieran

    It seems you downgraded the plugin version, in this case, the hook is: forminator_custom_form_submit_response

    In case you use the 1.14.11 it is forminator_form_submit_response

    The difference is on forminator_custom_form_ was replaced with forminator_form_ on some hooks.

    Let us know if this worked for you now.
    Best Regards
    Patrick Freitas

  • Kieran
    • The Crimson Coder

    Hi Panos,

    Thanks for the update. That makes sense. I’ve not testes it yes as I’m still stuck on version 1.14.09 until the css bug is fixed

    I do have another slight tweak I’m desperate for

    Basically I want to be able to redirect to a different page for “Select-2” in the same form. I’ve tried embedding it within the code. I’ve also tried copying the block like this

    "111":[
                   {
                      "field":"select-1",
                      "values":[
                         {
                            "90Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-90-mins/",
                            "60Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins/",
                            "45Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-45-mins/"
                         }
                      ]
                   }
                ],

    Changed to

    "111":[
                   {
                      "field":"select-1",
                      "values":[
                         {
                            "90Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-90-mins/",
                            "60Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins/",
                            "45Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-45-mins/"
                         }
                      ]
                   }
                ],
    "111":[
                   {
                      "field":"select-3",
                      "values":[
                         {
                            "60Blossom":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins-parents-in-blossom/"
                         }
                      ]
                   }
                ],
  • Kieran
    • The Crimson Coder

    https://gist.github.com/Kirzman/df1080b67f4f13a9faa4b4e29fa68a60

    Hi Patrick,

    This is current code which all works fine in version 1.14.11. But I want to tweak so Select-3 values in From 111 also redirects me to another page.

    Essentially the form is “111”, the field is “select-3” the value I’ve set to “90Blossom” and web page is
    https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins-parents-in-blossom/

    I want my select-1 redirects on Form 111 to still work also though. One of them seems to override the other

    Kind Regards
    Kieran

  • Panos
    • SLS

    I’ve updated the snippet here :
    https://gist.github.com/Kirzman/df1080b67f4f13a9faa4b4e29fa68a60
    but I couldn’t do it without changing the json format. So you would have to adapt the json to meet the new format unfortunately. I think it’s pretty straightforward since you have been already working with previous snippet.

    What you need to keep in mind is that the field that is met first is the one that will provide the redirect url. So if you set select-2 field first and the submitted value exists in the values set in json, it will return that url to redirect too. Else it will continue to next field set in the json, if there is any.

    Please let me know if something in the snippet or my description is unclear, I’ll try explain better. Also better try this out in a testing site first.

    Kind regards!

  • Kieran
    • The Crimson Coder

    Hi Panos

    Although technically it works, it only works the first time. If I pick “select-3″ for example it redirects me to the right page. But if I go back to make another booking (selection), its remembering that selection so I cant pick any other selection. Is there a way for it to not remember what you”ve selected?

    Kind Regards
    Kieran

  • Panos
    • SLS

    Hi Kieran!

    First I’d like to clarify that you don’t need these “field” query vars in the urls in links.

    "90Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-90-mins/?field=select-1",
    "60Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins/?field=select-1",
    "45Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-45-mins/?field=select-1"

    you don’t really need ?field=select-1 . I used those only to make sure the redirects work ok.

    Moving on, I tried that snippet you shared and it worked fine on my test. I suppose that the form id is 111 as this is where you have both select-1 and select-3 in the form. Keep in mind that if you have selected in select-1 field the option 60Padgate and for select-3 field the 60Bloom option, then select-1 has higher priority than select-3, so it will redirect to “https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins/?field=select-1&#8221;

    If you want to have higher priority on select-3 field then you can move the select-3 part above the select-1 part in the json code:

    "111":{
    	"fields":{
    		"select-3":{
    			"60Bloom":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins-parents-in-bloom/?field=select-3"
    		},
    		"select-1":{
    			"90Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-90-mins/?field=select-1",
    			"60Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-60-mins/?field=select-1",
    			"45Padgate":"https://warringtonpainclinic.co.uk/advanced-clinical-massage-booking-45-mins/?field=select-1"
    		}
    	}
    },

    Kind regards!

  • Kieran
    • The Crimson Coder

    Hi Panos

    I think the problem I have is Select-1 is automatically shown for the user on my booking page

    https://warringtonpainclinic.co.uk/bookings/

    So even when they change the location to reveal “select 3” then technically “select 1” has already been chosen so its not working.

    Gravity Forms has the functionaility in the confirmations part. You can set conditional logic to redirect if ALL of the conditions are matched.

    But before it was inbuilt I had the same problem on my other sites. The code to get it working was something like below if I remember right but I’m not sure if it’s possible to rework the Forminator code in such a way.

    <?php
    $agree1 = $_GET['agree1'];
     
            switch ($agree1)
            {
     
                    case "Padgate":
                    header("Location: https://xyz1.com";
                    break;
     
                    case "Bloom":
                    header("Location: http://xyz2.com");
                    break;
     
            }
    
    $agree2 = $_GET['agree2'];
     
            switch ($agree2)
            { 
    
    etc
    ?>

    [attachments are only viewable by logged-in members]

    It works without remembering other selections and ideally I’d like to continue using Forminator instead of gravity forms.

    Thanks for the help so far. I think it would be a very welcomed addition to the plugin to get you close to the market leader

    Kind Regards
    Kieran

  • Kieran
    • The Crimson Coder

    Sorry Panos,

    I remember now the code above only worked in Gravity Forms as I was able to set up a simple conditional logic rule first in the form then redirect it to the individual php to add an extra step.

    Its a shame Forminator remembers the selection I might have to just build an extra page during the booking process

    Kind Regards
    Kieran

  • Patrick Freitas
    • FLS

    Hi Kieran

    I hope you are doing well.

    I see on the shared code it uses a header redirect which is a different approach and could cause issues on the submission if redirected at the incorrect time.

    The code shared from Panos uses our built-in functions, I pinged him to check your reply and see if we are missing anything.

    Its a shame Forminator remembers the selection I might have to just build an extra page during the booking process

    We do have it planned and I added this ticket as an extra case to our developers.

    Best Regards
    Patrick Freitas

  • Kieran
    • The Crimson Coder

    Just to update. I’ve installed 1.15.14 and the code still works. But it stops working between 1.15.14 and 1.16

    I had to get support agent Rakib to send me old versions and it seems you have some kind of customer restrictions in downloading versions further back than 1.16.

  • Kieran
    • The Crimson Coder

    Hi Prashant,

    Its half working. It’s not working where there are hidden fields. I’m almost certain that we need to tweak to check a field’s visibility and skip if it’s hidden. That’s the issue we had last time.

    Thanks for the efforts and quick turnaround so far!

    Kind Regards
    Kieran