In this code recipe, we showcase two code-based methods to set up login redirection for your membership site.
The firsts recipe shows you how to change the default login behavior in PMPro to send all members to the same landing page. The second shows you how to send different users to different landing pages based on level.
If you aren’t comfortable using code, check out the Member Homepages Add On for a more user-friendly method to set up redirects. We talk about all these methods, the benefits of login redirection, and a few other ways to set up redirects in our guide on How to Redirect Members on Login and Why You Should.
Code Recipe #1: Redirect All Members to Same Page
This recipe will show you how to redirect all members, regardless of their level, to a specific page after logging in.
Tweak this code to your needs by changing the $redirect_to
URL on line 18.
Code Recipe #2: Login Redirects Based On Membership Level
This recipe will show you how to redirect members to a specific page based on their membership level after logging in.
Tweak this code to your needs by changing the membership level IDs in the pmpro_hasMembershipLevel()
checks and the URLs in the home_url()
calls.
In this example we are redirecting users as follows:
- Members of Level ID 1 are sent to a page at
yourdomain.com/level-one/
- Members of Level ID 2 are sent to a page at
yourdomain.com/level-two/
- Members of Level ID 3 are sent to a page at
yourdomain.com/level-three/
- All other members as well as non-members are sent to the homepage.
Adding the Recipe to Your Website
You can add this recipe to your site by creating a custom plugin or using the Code Snippets plugin available for free in the WordPress repository. Read this companion article for step-by-step directions on either method.
Free Course: Membership Site Development—The Basics
Develop a deeper understanding of membership site development in this beginner-level course. Learn how to make your site work better, save yourself time and money, and improve your site's performance.