custom_slug = ! empty( $options['custom_login_slug'] ) ? sanitize_title( $options['custom_login_slug'] ) : ''; // If no custom slug is set, do not alter core behavior. if ( empty( $this->custom_slug ) ) { return; } // 1. Intercept requests to the new custom URL add_action( 'init', array( $this, 'intercept_custom_login' ), 1 ); // 2. Block direct access to the default wp-login.php add_action( 'init', array( $this, 'block_default_login' ), 2 ); // 3. Block unauthenticated access to /wp-admin/ BEFORE auth_redirect() fires add_action( 'init', array( $this, 'block_wp_admin_for_guests' ), 3 ); // 4. Filter core WordPress login URLs to point to our custom slug add_filter( 'login_url', array( $this, 'filter_login_url' ), 10, 3 ); add_filter( 'logout_url', array( $this, 'filter_logout_url' ), 10, 2 ); } /** * Route traffic from the custom slug directly into the login routines. */ public function intercept_custom_login() { $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; $parsed_path = wp_parse_url( $request_uri, PHP_URL_PATH ); $trimmed_path = trim( $parsed_path, '/' ); if ( $trimmed_path === $this->custom_slug ) { // Set a constant so our block_default_login function knows this is a legitimate request. if ( ! defined( 'TT24_CUSTOM_LOGIN_ACTIVE' ) ) { define( 'TT24_CUSTOM_LOGIN_ACTIVE', true ); } // Require the core login file directly, bypassing the need for physical file rewrites. global $error, $interim_login, $action, $user_login; require_once ABSPATH . 'wp-login.php'; exit; } } /** * Redirect any direct access to wp-login.php if the custom slug is active. */ public function block_default_login() { $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; // If accessing wp-login.php directly and NOT via our custom router... if ( strpos( $request_uri, 'wp-login.php' ) !== false && ! defined( 'TT24_CUSTOM_LOGIN_ACTIVE' ) ) { // Allow logout requests to pass through momentarily to prevent getting trapped in a logged-in state. if ( isset( $_GET['action'] ) && 'logout' === sanitize_text_field( wp_unslash( $_GET['action'] ) ) ) { return; } // Silently redirect attackers to the homepage. wp_safe_redirect( home_url() ); exit; } } /** * Block unauthenticated users from accessing /wp-admin/ and discovering the login slug. */ public function block_wp_admin_for_guests() { // Only trigger if the user is NOT logged in if ( is_user_logged_in() ) { return; } // Only trigger if the request is targeting the admin area if ( ! is_admin() ) { return; } // Always allow AJAX and CRON requests to function normally if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { return; } $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; // Allow admin-post.php and admin-ajax.php (frontend form submissions often use these) if ( strpos( $request_uri, 'admin-post.php' ) !== false || strpos( $request_uri, 'admin-ajax.php' ) !== false ) { return; } // If we reach this point, a guest is actively trying to access /wp-admin/ // Silently redirect them to the homepage. wp_safe_redirect( home_url() ); exit; } /** * Rewrite the default login URL returned by wp_login_url(). * * @param string $login_url The default login URL. * @param string $redirect The path to redirect to on login. * @param bool $force_reauth Whether to force reauthorization. * @return string The obfuscated login URL. */ public function filter_login_url( $login_url, $redirect, $force_reauth ) { $custom_url = home_url( '/' . $this->custom_slug . '/' ); if ( ! empty( $redirect ) ) { $custom_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $custom_url ); } if ( $force_reauth ) { $custom_url = add_query_arg( 'reauth', '1', $custom_url ); } return esc_url( $custom_url ); } /** * Rewrite the default logout URL. * * @param string $logout_url The default logout URL. * @param string $redirect The path to redirect to on logout. * @return string The obfuscated logout URL. */ public function filter_logout_url( $logout_url, $redirect ) { $custom_url = home_url( '/' . $this->custom_slug . '/?action=logout' ); if ( ! empty( $redirect ) ) { $custom_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $custom_url ); } // Nonce is required for secure logouts $custom_url = wp_nonce_url( $custom_url, 'log-out' ); return esc_url( $custom_url ); } }