• Resolved Mark T

    (@markascendglobal)


    Hey,

    So, I have a Forminator regsitration form that I want to change the redirect URL of, depending on the URL variable

    I copied and changed this code thinking it would work:

    <?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 = 380;
    
            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 ) {
    
            	$new_url = get_bloginfo('url').'/checkout';
    
            	if(isset($_GET['redirect_to']) && $_GET['redirect_to'] == $new_url) {
    				$redirect_url = esc_url($new_url);
            	}
    
                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 );
        }
    }

    The bit I edited is the calc_url() function, as that’s the bit in the original code that did the redirect URL change (I think)

    The login/reg page has the URL variable ?redirect_to=https://mydomainnamehere.com/checkout on the end which is needed for something else and can’t be removed

    Basically, I want the form with ID = 380 to redirect to that URL variable, but ONLY if it is present (hence the if statement in my calc_url() change above

    Can anyone see why this isn’t working?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Nebu John – WPMU DEV Support

    (@wpmudevsupport14)

    Hi @markascendglobal,

    Trust you are doing good and thank you for reaching out to us.

    The ticket that you mentioned is a bit old and as we have released many updates since then, the code might not work with the latest version of the plugin.

    Could you please explain the exact workflow that you are looking forward to?

    Please also share an export of your registration form so that we can help you further. You should share the export of the form using pastebin.com or Google Drive. Please find how to export a form in our documentation here: https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export

    We look forward to hearing back from you.

    Kind Regards,
    Nebu John

    Thread Starter Mark T

    (@markascendglobal)

    Hey

    So, its a WooCommerce site. I’ve replaced the standard WC register form with a Forminator registration form for multiple reasons.

    This is how the site currently works:

    • The site is set so you cannot buy anything without being a registered user
    • When you get to the checkout page, if you’re not logged in, WooCommerce redirects you to the standard login page
    • You either login to an account or register for a new one
    • The login form redirects you back to the checkout (custom code to bypass logging in to the account dashboard, which works fine)
    • As it stands, the register form allows you to register but doesn’t redirect back to the checkout like the login form
    • There is a custom ?redirect_to=[URL FOR CHECKOUT] URL parameter, which is part of the login redirect coding I made

    Basically, I want the register form to redirect you to the checkout like the login form, BUT ONLY if that ?redirect_to= URL parameter is present (eg, if it’s not there, register as normal)

    Pastebin of form export – https://pastebin.com/ygUwtsTk

    I hope that makes sense

    Thanks

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @markascendglobal

    Thank you for the information.

    I believe you can do that without any custom coding, Forminator has the hidden field and the conditionals after submissions, can you try this form?

    https://pastebin.com/kzEL3hLv

    I added a hidden field to catch the redirect URL https://monosnap.com/file/d4ugnIvEUEkWbpDLPlhVCfOGvaerLK

    Then added the field in the redirect URL https://monosnap.com/file/k2E5WJmTGkNzUfwMy17OfDg3gBhrp8 and two conditionals, one to redirect if the field is not null and not redirect if the field is null.

    Best Regards
    Patrick Freitas

    Thread Starter Mark T

    (@markascendglobal)

    Hey Patrick

    I knew it would be something a lot simpler!

    That’s done the job perfectly, thank you

    Consider this topic resolved

    Mark

    Plugin Support Saurabh – WPMU DEV Support

    (@wpmudev-support7)

    Hi @markascendglobal

    Thanks for confirming this ticket has been solved. If you need help with any additional request feel free to open a new ticket. Our team will be glad to help

    Kind regards

    Luis

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Variable redirect on User Registration’ is closed to new replies.