Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Created April 22, 2024 07:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewlimaza/e34b0fefedd249ff67952f85306e9800 to your computer and use it in GitHub Desktop.
Save andrewlimaza/e34b0fefedd249ff67952f85306e9800 to your computer and use it in GitHub Desktop.
Redirect to a default page, unless redirecting to content [Paid Memberships Pro]
<?php
/**
* Redirect to a default "members" page unless redirecting back to referrer.
* Adjust the URLS for $skip_redirects which it bypasses.
*
* Add this code to your site by following this guide - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmpro_login_redirect_url( $redirect_to, $request, $user ) {
// Skip certain redirect_tos always
$skip_redirects = array(
site_url( '/login/' ),
site_url( '/wp-admin/' )
);
// If the redirect_to is a skippable URL, let's remove the redirect_to so we can redirect to the members page.
if ( ! empty( $redirect_to ) && in_array( $redirect_to, $skip_redirects ) ) {
$redirect_to = '';
}
//if logged in and a member, send to members page
if ( empty( $redirect_to ) && pmpro_hasMembershipLevel( NULL, $user->ID ) ) {
$redirect_to = site_url( '/members/' );
}
return $redirect_to;
}
add_filter( 'login_redirect', 'my_pmpro_login_redirect_url', 5, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment