Gravity forms user registration and redirect

Hi;
I'm using the gravity forms user registration add on to register new users on a wpmu site and would like to redirect them to the admin of the user's newly registered site once the form is submitted. The add on bypasses the wordpress login filter and also will not accept any variables in the redirect url. Does anyone have a solution for this? What I have in mind is to redirect to a php file (a plugin, that would be) and redirect by extracting data from the $user object and get the blog id and name for the registered user (and compose the redirect url).

  • kpince

    No, but I believe I managed to find a quick solution to this. I found a very old thread on this topic which hasn't been resolved so I will post my solution here for those interested.

    I used Peter's login redirect as I found out it has an extra php file (wplogin_redirect_control.php) which handles plugins that bypass the wordpress login filter. This is an option at the very bottom of the plugin settings page. The plugin allows the usage of variables in the redirect URL's. The only problem was that it did not have the blog url as a variable so I added it to the case statement in the main plugin file (wplogin_redirect.php) which handles the variable names and values:

    case 'blogurl':
                            $primary_blog_id = get_usermeta($user->ID, 'primary_blog');
    			$blog_details = get_blog_details($primary_blog_id);
    			$redirect_url = $blog_details->siteurl;
                            $variable_value = $redirect_url;
                            break;

    This is the full code with the above lines added in (so people will know where to place the extra code):

    /*
            A generic function to return the value mapped to a particular variable
        */
        static function rul_get_variable( $variable, $user )
        {
            $variable_value = apply_filters( 'rul_replace_variable', false, $variable, $user );
            if( !$variable_value )
            {
                // Return the permalink of the post ID
                if( 0 === strpos( $variable, 'postid-' ) )
                {
                    $post_id = str_replace( 'postid-', '', $variable );
                    $permalink = get_permalink( $post_id );
                    if( $permalink )
                    {
                        $variable_value = $permalink;
                    }
                }
                else
                {
                    switch( $variable )
                    {
                        // Returns the current user's username (only use this if you know they're logged in)
                        case 'username':
                            $variable_value = rawurlencode( $user->user_login );
                            break;
                        // Returns the URL of the WordPress files; see http://codex.wordpress.org/Function_Reference/network_site_url
                        case 'siteurl':
                            $variable_value = network_site_url();
                            break;
                        // Returns the URL of the site, possibly different from where the WordPress files are; see http://codex.wordpress.org/Function_Reference/network_home_url
                        case 'homeurl':
                            $variable_value = network_home_url();
                            break;
                        // Returns the login referrer in order to redirect back to the same page
                        // Note that this will not work if the referrer is the same as the login processor (otherwise in a standard setup you'd redirect to the login form)
    		    case 'blogname':
                            $primary_blog_id = get_usermeta($user->ID, 'primary_blog');
    			$blog_details = get_blog_details($primary_blog_id);
    			$redirect_url = $blog_details->siteurl;
                            $variable_value = $redirect_url;
                            break;
                        // Returns the blog name so we can redirect the user to the admin or his own site homepage after registration
                        case 'http_referer':
                            $http_referer_parts = parse_url( $_SERVER['HTTP_REFERER'] );
                            if( $_SERVER['REQUEST_URI'] != $http_referer_parts['path'] )
                            {
                                $variable_value = $_SERVER['HTTP_REFERER'];
                            }
                            else
                            {
                                $variable_value = '';
                            }
                            break;
                        default:
                            $variable_value = '';
                            break;
                    }
                }
            }
            return $variable_value;
        }

    In the redirect url on the plugin settings page use [variable]blogurl[/variable] to redirect to the home page or add [variable]blogurl[/variable]/wp-admin to redirect to the admin. And this code snippet I found on another thread here to be added to the functions.php file of your theme to log the user in automatically after submitting the gravity form:

    add_action( 'gform_user_registered', 'pi_gravity_registration_autologin', 10, 4 );
    /**
     * Auto login after registration.
     */
    function pi_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
    	$user = get_userdata( $user_id );
    	$user_login = $user->user_login;
    	$user_password = $password;
    
        wp_signon( array(
    		'user_login' => $user_login,
    		'user_password' =>  $user_password,
    		'remember' => false
        ) );
    
    }

    Works just fine!

  • jon_bennett

    Hello,

    I'm trying to redirect user to their blog page after registration using Gravity form. I used your codes, but it's not working. I added following code to wplogin_redirect.php

    case 'blogurl':
                            $primary_blog_id = get_usermeta($user->ID, 'primary_blog');
    						$blog_details = get_blog_details($primary_blog_id);
    						$redirect_url = $blog_details->siteurl;
                            $variable_value = $redirect_url;
                            break;

    And I added following code to functions.php file.

    add_action( 'gform_user_registered', 'pi_gravity_registration_autologin', 10, 4 );
    /**
     * Auto login after registration.
     */
    function pi_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
    	$user = get_userdata( $user_id );
    	$user_login = $user->user_login;
    	$user_password = $password;
    
        wp_signon( array(
    		'user_login' => $user_login,
    		'user_password' =>  $user_password,
    		'remember' => false
        ) );
    
    }

    And after that I added the url to "Post-registration" and "All other users" to [variable]blogurl[/variable]/wp-admin

    And also I made "Use external redirect file. Set this to "Yes" if you are using a plugin such as Gigya that bypasses the regular WordPress redirect process (and allows only one fixed redirect URL). Then, set the redirect URL in the other plugin to
    http://www.yoursite.com/wp-content/plugins/peters-login-redirect/wplogin_redirect_control.php" to YES, which is in the Peter's login redirect Settings options (option above last option).

    But it's not redirecting the newly registered user to their dashboard blog url. After registration users redirect to confirmation message page. Hope you can help me on this. Thanks.

  • Shayne

    Hi,

    I thought I'd quickly post an update to this thread in case anyone else was trying to make this work... I struggled for a few hours with this until I had that "duh" moment... the OP didn't actually explicitly state one of the steps which I sorta missed. The solution that kpince originally posted works perfectly and with the combination of Peter's redirect, its perfect for what I was trying to accomplish without any other custom code. Once this is implemented, you can redirect pretty much every type of user after they've registered to any page on your site or referings urls. Thank you for this. This saved me a ton of time.

    Anyways, in case this helps anyone, here are the steps i followed.

    1. add case blogurl snippet to Peter's redirect plugin (posted above)
    2. add the gravity forms auto login snippet to my child theme (posted above)

    this is one step i was a little confused about
    3. In the settings of Peter's redirect, way at the end, set "Use external redirect file" to Yes. This needs to be enabled, so set this to Yes.

    here are the steps i missed:

    4. in gravity forms, on the confirmation of your page, set to redirect. Redirect URL needs to be set to http://www.yoursite.com/wp-content/plugins/peters-login-redirect/wplogin_redirect_control.php

    My last problem was the my Gravity Forms User Registration was set to activate manually. So change this so that Activate Registration is unchecked in the feed.

    That's pretty much it.. took me about 5 mins to set all this up on my prod site once I figured out what i was doing.

    I know this is an old thread, but maybe this will help another newby like myself and save you a few hours of fiddling around. After looking and playing around with every possible login/registration plugin out there, gravity forms + peter's redirect is by far easiest one to implement and it actually works.

    Thanks again for this.
    Shayne