Embed Snippet on Your Site
Rewrite login and register URLs generated by WordPress
// 1. Rewrite login and register URLs generated by WordPress
add_filter( 'login_url', function ( $login_url, $redirect ) {
$url = home_url( '/login-2/' );
if ( ! empty( $redirect ) ) {
$url = add_query_arg( 'redirect_to', urlencode( $redirect ), $url );
}
return $url;
}, 10, 2 );
add_filter( 'register_url', function () {
return home_url( '/login-2/' );
} );
// 2. Catch direct visits to wp-login.php (login and register only)
add_action( 'login_init', function () {
$action = isset( $_REQUEST['action'] ) ? $_REQUEST['action'] : 'login';
// Leave logout, password reset, and POST requests alone
if ( 'POST' === $_SERVER['REQUEST_METHOD'] || ! in_array( $action, array( 'login', 'register' ), true ) ) {
return;
}
$url = home_url( '/login-2/' );
if ( ! empty( $_GET['redirect_to'] ) ) {
$url = add_query_arg( 'redirect_to', urlencode( $_GET['redirect_to'] ), $url );
}
wp_safe_redirect( $url );
exit;
} );
// 3. Fix hardcoded links and buttons in the page output
add_action( 'template_redirect', function () {
ob_start( function ( $html ) {
$login = home_url( '/login-2/' );
$html = preg_replace( '#https?://hardyheavy.com/wp-login.php?action=register#', $login, $html );
$html = preg_replace( '#https?://hardyheavy.com/wp-login.php(?redirect_to=[^"'s]*)?#', $login . '$1', $html );
$html = str_replace( 'https://hardyheavy.com/?page_id=1002', $login, $html );
return $html;
} );
} );
| |
Comments