Skip Navigation

[Gelöst] Need to have 1 login form that redirects based on WP_user-role

This support ticket is created vor 6 Jahren, 5 Monaten. 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.

Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.

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

Supporter timezone: America/Jamaica (GMT-05:00)

Dieses Thema enthält 19 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von Shane vor 6 Jahren, 4 Monaten.

Assistiert von: Shane.

Author
Artikel
#907276

want to redirect to custom my-account page from one login form based on WP_user_role

No documentation available.

No examples.

versteckter Link

#907923

You can use the login_redirect filter to modify the redirect URL for a wpv-login-form shortcode:

function my_login_redirect( $redirect_to, $request, $user ) {
    if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied
        return $redirect_to;
    }
    //is there a user to check?
    if (isset($user->roles) && is_array($user->roles)) {
        //check for subscribers
        if (in_array('subscriber', $user->roles)) {
            // redirect them to another URL, in this case, the homepage
            $redirect_to =  '<em><u>versteckter Link</u></em>';
        }

        if (in_array('contributor', $user->roles)) {
            // redirect them to another URL, in this case, the search page
            $redirect_to = '<em><u>versteckter Link</u></em>';
        }

        // copy and paste the block above for each role
    }

    return $redirect_to;
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
#907976

Hi Christian,

Thanks so much for the code snippet. PHP newbie, hence use Toolset, but site is throwing a 500 server error when this code is added. No doing so when removed. Did I mess it up?

My PHP editor (WebStorm) is hinting 2 issues: "Required parameter $haystack missing" on both "if (in_array('seller&039;, $user->roles) ). Also says that $redirect_to is an "undefined variable".

Here's what I uploaded to my child theme's functions.php file:

// login redirect for buyers and sellers
function my_login_redirect( $redirect_to, $request, $user ) {
    if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied
        return $redirect_to;
    }
//is there a user to check?
    if (isset($user->roles) && is_array($user->roles)) {

//check for sellers
        if (in_array('seller', $user->roles)) {
// redirect to seller my-account page
            $redirect_to =  '<em><u>versteckter Link</u></em>'
}
//check for buyers
    if (in_array('buyer', $user->roles)) {

// redirect to my-buyer-account page

        $redirect_to = '<em><u>versteckter Link</u></em>'
// copy and paste the block above for each role
}
return $redirect_to;

Thanks

#908373

I see that you're missing semicolons after each $redirect_to variable, and there are some missing parentheses. Try this version:

// login redirect for buyers and sellers
function my_login_redirect( $redirect_to, $request, $user ) {
    if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied
        return $redirect_to;
    }
    //is there a user to check?
    if (isset($user->roles) && is_array($user->roles)) {

      //check for sellers
      if (in_array('seller', $user->roles)) {
        // redirect to seller my-account page
        $redirect_to =  '<em><u>versteckter Link</u></em>';
      }

      //check for buyers
      if (in_array('buyer', $user->roles)) {

        // redirect to my-buyer-account page
        $redirect_to = '<em><u>versteckter Link</u></em>';
      }
    }

return $redirect_to;
}
#908591

Pasted code in my child theme functions.php exactly as provided. Sitewide server error 500. Remove it and site returns to normal. Here is the code immediately preceding that provided and the code as I pasted in below that:

// Custom Popup Scripts
add_action( 'wp_footer', 'my_custom_popup_scripts', 500 );
function my_custom_popup_scripts()
{ ?>
    <script type="text/javascript">
        (function ($, document, undefined) {

            console.log('Here in the custom popup script');

        }(jQuery, document))
    </script><?php
}
// login redirect for buyers and sellers

function my_login_redirect( $redirect_to, $request, $user ) {

    if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied

        return $redirect_to;

    }

    //is there a user to check?

    if (isset($user->roles) && is_array($user->roles)) {



      //check for sellers

      if (in_array('seller', $user->roles)) {

        // redirect to seller my-account page

        $redirect_to =  '<em><u>versteckter Link</u></em>';

      }



      //check for buyers

      if (in_array('buyer', $user->roles)) {



        // redirect to my-buyer-account page

        $redirect_to = '<em><u>versteckter Link</u></em>';

      }

    }



return $redirect_to;

}

Thanks again Christian.

#908593
Server 500.PNG

Tried one more thing with the code provided. Turned off all other plugins. Only ones now turned on are all of Toolset, except Access and WooCommerce. Same result. Back out the code, site is working without server error. Thanks Christian.

#909032

Looks like the last line was omitted:

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

Please add it and check again. If you're still seeing 500 errors, please provide login credentials here and I will take a closer look.

#910672

Okay I see the problem. Here's an update:

function my_login_redirect( $redirect_to, $request, $user ) {

    if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied

        return $redirect_to;

    }

    //is there a user to check?

    if (isset($user->roles) && is_array($user->roles)) {

        //check for sellers

        if (in_array('seller', $user->roles)) {

            // redirect to seller my-account page

            $redirect_to =  '<em><u>versteckter Link</u></em>';

      }

    //check for buyers

    if (in_array('buyer', $user->roles)) {



        // redirect to my-buyer-account page

        $redirect_to = '<em><u>versteckter Link</u></em>';

      }
  }

return $redirect_to;
}
#911352

Hi Christian,
I have copied and pasted in your last snippet. Got the same result 500 Server Error. I also tried it by adding in the "add action" piece (not included) that you'd said was left out of an earlier version. That did not work either. At this point, this and one other issue you are helping us with, are the the only things holding up getting site this into production.

Since you have all the credentials, please login, make the change to fix the login problem and save the result. The only thing you need to check is to navigate to any page on the site. If you don't get a 500 Server error, that does it. I will take over and finish the testing of actually logging in as both Seller and Buyer to see that it is, in fact, directing the users to the appropriate account page in each instance.

Thanks for your continued support.

#911361

Hi Christian,

An additional question related to the Login issue. Is there a difference between 'add_action' and 'add-filter'. Your post before the last one said that it was omitted. Which makes sense, otherwise we're setting up a filter but not actually activating it. But when you said it should be 'add_action', should it not be 'add_filter', or is there no practical difference in this instance. Thanks.

#911541

But when you said it should be 'add_action', should it not be 'add_filter', or is there no practical difference in this instance.
No, I did not recommend using "add_action". Here are the two comments that show the code with "add_filter":
https://toolset.com/forums/topic/need-to-have-1-login-form-that-redirects-based-on-wp_user-role/#post-907923
https://toolset.com/forums/topic/need-to-have-1-login-form-that-redirects-based-on-wp_user-role/#post-909032

Perhaps you are confusing the code I provided with the existing code in functions.php for Custom Popup Scripts?

If you want to learn more about the difference between add_action and add_filter, please visit the WordPress codex:
https://developer.wordpress.org/reference/functions/add_action/
https://developer.wordpress.org/reference/functions/add_filter/

I pasted this exact code in your theme's functions.php file:

function my_login_redirect( $redirect_to, $request, $user ) {
 
    if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied
 
        return $redirect_to;
 
    }
 
    //is there a user to check?
 
    if (isset($user->roles) && is_array($user->roles)) {
 
        //check for sellers
 
        if (in_array('seller', $user->roles)) {
 
            // redirect to seller my-account page
 
            $redirect_to =  '<em><u>versteckter Link</u></em>';
 
      }
 
    //check for buyers
 
    if (in_array('buyer', $user->roles)) {
 
 
 
        // redirect to my-buyer-account page
 
        $redirect_to = '<em><u>versteckter Link</u></em>';
 
      }
  }
 
return $redirect_to;
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

There is no 500 error on the page I just visited here: versteckter Link
What am I misunderstanding about the 500 error and how you were adding this code?

#913790

Thanks Christian. No longer getting 500 Server errors with code snippet provided.

After creating a new Seller, I login and stay on the same Login page. In other words, the redirects are not working. Upon logging, both Buyers and Sellers should be redirected to their Account Pages.

The redirects are there in the php code, but not working. Don't know of any other way to fix this, nor how to troubleshoot why the code that appears to be correct is not working. Thanks.

#913800

Also, if this is any clue to what is happening, when I change the "What to show after submitting the form" in either Create Free Buyer Account or Create Free Seller Account to "Display a message instead of a form", both display the "Your Buyer/Seller Account has been created" message, leave the new Buyer/Seller on the "Create New Account Page" with the form no longer showing .. just Forgot Your Password.

On the Create Buyer/Seller Page in "What to show after ...", When I change it to the respective Buyer and Seller "Account pages", it shows "Please wait while being redirected" then goes to the correct account in the URL, but returns a 404.

Finally, the "What to show after ...", does not allow you to not select an action, so not sure which is taking over the result, since not of the obvious selection work, and neither does the php code redirections. Thanks.

Neue Threads, die von Christian Cox erstellt wurden und mit diesem verbunden sind, sind unten aufgeführt:

https://toolset.com/de/forums/topic/cred-forms-redirection-not-working-as-expected/

#914424

Your last comment has been split into a separate ticket. It sounds like you're talking about CRED redirection options there, but those are not the same as login redirection options. CRED can create or edit a User's account, but does not affect a User's logged-in status - that's a separate process. Let's use this ticket to discuss the login redirection issue.

After logging in as an Admin, I am able to visit the User profile page and see that the role slugs are not "seller" and "buyer", they are "ftm_puppy_seller" and "ftm_puppy_buyer". Try modifying the role slugs in the PHP code to match the role slugs in the select field.

#914608

Thanks Christian,
Made the change to the php code, adding the correct slug "ftm-puppy-seller" and "ftm-puppy-buyer" for the two roles.

// Login Redirect
function my_login_redirect( $redirect_to, $request, $user ) { 
    if( !isset( $user->user_login ) ){ // we only want this to run when credentials have been supplied
        return $redirect_to; 
    }
    //is there a user to check?
    if (isset($user->roles) && is_array($user->roles)) { 
        //check for sellers 
        if (in_array('ftm-puppy-seller', $user->roles)) { 
            // redirect to seller my-account page 
            $redirect_to =  '<em><u>versteckter Link</u></em>';
      }
    //check for buyers 
    if (in_array('ftm-puppy-buyer', $user->roles)) {
        // redirect to my-buyer-account page
        $redirect_to = '<em><u>versteckter Link</u></em>';
      }
  }
return $redirect_to;
}
add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

Created a new Seller using the "Create Free Seller Account" Form, with "After Submitting the Form" set to "Go To Page", which was a page with only the following 2 shortcodes in it:

<p>[wpv-login-form allow_remember="true"]</p><br>
<p>[wpv-forgot-password-form]</p>

I was sent to a page giving me the options to login, or a "Forgot My Password" link. Entering the Seller Credentials just created, I was send to a post/page that showed only the correct referrer form in the URL - only other thing on the post/page was the title Login. It should be the new Seller's My Account Page.

Then, if I create a "Login -Seller" Page with the correct redirection in a shortcode to the My Account Page, it works! Problem is this means I have to have two Login Pages: Login-Seller and Login Buyer, which I am trying to avoid. The reason to avoid the two-login-pages solution is that when I tested it with a Buyer and Seller, both with accounts. A Buyer could login to the Seller Account, as well as their own. The Seller could login to the Buyer Account, as well as their own.

All this seems to be saying that the PHP redirect code is not working, or I am applying it incorrectly. But the manual, dual-login approach will not work. for the reasons cited. And for security reasons, I don't want to redirect a new Buyer or Seller directly to their respective account page without having them use their new credentials to login to their accounts the first time.

Thanks for hanging in there to see this through to a solution.