Created
February 4, 2017 23:00
-
-
Save wpbullet/49fd1e4e2f96ea1835cbe13f88bfcba4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined( 'ABSPATH' ) or die( 'No Access' ); | |
/** | |
* Plugin Name: Force Redirects | |
* Description: Allows admins default behavior and all other users get redirected to where they came from or to the homepage | |
* Author: Mike Andreasen | |
*/ | |
/** | |
* Redirect user after successful login. | |
* | |
* @param string $redirect_to URL to redirect to. | |
* @param string $request URL the user is coming from. | |
* @param object $user Logged user's data. | |
* | |
* @return string | |
*/ | |
function ma_force_login_redirect( $redirect_to, $request, $user ) { | |
if ( isset( $user->roles ) && is_array( $user->roles ) ) { | |
// check for an admin role | |
if ( in_array( 'administrator', $user->roles ) ) { | |
// check for admin's default redirect, which is the homepage for some reason and send them to wp-admin instead | |
if( $redirect_to === home_url() ) { | |
return get_admin_url(); | |
} else { | |
// redirect to where they came from | |
return $redirect_to; | |
} | |
} | |
} | |
// default redirect_to is /wp-admin/, detect it and forward to home instead | |
if( ma_detect_default_redirect( $redirect_to ) ) { | |
return home_url(); | |
} | |
return $redirect_to; | |
} | |
add_filter( 'login_redirect', 'ma_force_login_redirect', 9999999, 3 ); | |
/** | |
* Determine if we are using the default redirect | |
* A subscriber's default appears to be /wp-admin/, but membership2 is directing them to /account/ | |
* | |
* @param string $redirect_to URL to redirect to. | |
* | |
* @return bool True|False | |
*/ | |
function ma_detect_default_redirect( $redirect_to ) { | |
if( $redirect_to === get_admin_url() ) { | |
return true; | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment