I’m developing a theme, and I always want the user to land on the custom login page if it is defined…so I want to redirect the user to the login page from wp-login.php.
Here is the code that I’m added for my purposes…your mileage may vary (I didn’t include the UI toggle, obviously…)
/**
* Redirect the user to the Download Manager's login page instead of wp-login.php.
*/
add_action( 'login_form_login', array( $this, 'redirect_to_custom_login' ) );
function redirect_to_custom_login() {
if ( $_SERVER['REQUEST_METHOD'] == 'GET' ) { //Only when the form is being called, not posted
if ( is_user_logged_in() ) {
wp_redirect( self::$dashboard_url );
} else {
wp_redirect( self::$login_url );
}
}
}
/**
* Set a static constant for the login URL so I don't have to do it on every single tpl page...
*/
function set_login_url() {
$login_id = get_option('__wpdm_login_url');
if( $login_id > 0)
$login_url = get_permalink( $login_id );
else $login_url = wp_login_url();
$redirect_to = ( isset($_GET['redirect']) && 'referer' == $_GET['redirect'] ) ? $_SESSION['HTTP_REFERER'] : null;
$redirect_to = isset( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : $redirect_to;
if ( !empty( $redirect_to ) ) {
$login_url = add_query_arg( 'redirect_to', $redirect_to, $login_url );
}
self::$login_url = $login_url;
}