Skip Navigation

[Resolved] Dynamically Redirect users after login using url parameter

This support ticket is created 5 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by erinJ 5 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#1435225

I am trying to:
I have a login page, built with Elementor that uses the [wpv-login-form] shortcode to produce a login form. I'm trying to capture a dynamic redirect URL from a get parameter (redirect_to). I've tried several different methods to no avail:

First I followed this method: https://toolset.com/forums/topic/redirect-after-login-3/
[wpv-login-form redirect_url='[redirect_url]']

I thought this might be a long shot since the form isn't displayed in a view so I also tried a custom shortcode function:
[wpv-login-form redirect_url='[wpv-search-term param="redirect_to"]']

function cms_redirect_url(){
    if( isset($_GET['redirect_to']) ){
            $redirect_to = $_GET['redirect_to'];
    } else {
            $redirect_to = get_site_url() . '/app/' ; 
    }
    return $redirect_to;
}
add_shortcode('redirect_url','cms_redirect_url');

When that didn't work, I skipped the shortcode attribute altogether and used this filter:

function cms_redirect_on_login( $redirect_to, $requested_redirect_to, $user ){
    if( $user && is_object( $user ) && is_a( $user, 'WP_User' ) ) {
        if( $user->has_cap( 'administrator') ) {
            $redirect_to = admin_url();
        } elseif( isset($_GET['redirect_to']) ){
            // dynamically get redirect url from url parameter 
            //not working 
            $redirect_to = $_GET['redirect_to'];
        }
            else { //redirect to app dashboard. This works. 
            $redirect_to = get_site_url() . '/app/' ; 
        }
    }
    return $redirect_to;
}
add_filter('login_form', 'cms_redirect_on_login', 10, 3 );

As mentioned, that will work if no parameter is set, but if a parameter is set it's not grabbing it either. Is there an easier method of dynamically redirecting users after login? Please help. Thanks!

#1435463

Hello,

What do you mean "When that didn't work"?
Are you passing URL parameter "redirect_url" with value of other website URLs?
If it is, please try these:
Dashboard-> Toolset-> Settings-> Front-end Content:
1) in section "Third-party shortcode arguments", add your custom shortcode name into it, for example: redirect_url
2) in section "Safe redirects", add those website's domain names, and test again.

More help:
https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-login-form
An absolute URL
If redirect_url attribute is set, visitors will be redirected to the specified URL. The URL used must be an absolute URL within the same website or within the allowed redirect hosts.

#1436149

I've already added the shortcode to the third party shortcode arguments and tested. Just to be thorough, I disabled the partially working filter ( add_filter('login_redirect', 'cms_redirect_on_login', 10, 3 ); ), checked my settings, and tested again. It's still not working.

By "it didn't work" I mean, it's not redirecting to the URL that is passed in the url parameter (a URL for the same domain as my WordPress install). It's just logs the user in and reloads the page. No redirection happens at all.

And, when I use the shortcode in a shortcode like this, [wpv-login-form redirect_url="[redirect_to]"] , it shows the form, double quotes, and a bracket like this "]

#1436589

My issue is resolved now.

I had to revert to the native WordPress Gutenberg block Editor and use the native shortcode widget instead of using the shortcode widget in the Elementor editor.

Thanks for your help!