'string', 'sanitize_callback' => 'sanitize_title', 'default' => 'secure-login', ] ); register_setting( 'adlhpti_settings_group', 'adlhpti_enabled', [ 'type' => 'boolean', 'sanitize_callback' => 'rest_sanitize_boolean', 'default' => false, ] ); } public function adlhpti_render_settings_page() { $slug = get_option('adlhpti_custom_login_slug', 'secure-login'); $custom_login_url = site_url($slug); // No nonce for public URL $template = plugin_dir_path(__FILE__) . '../templates/admin-template.php'; if (file_exists($template)) { include $template; } else { echo '

' . esc_html__('Settings page template not found.', 'admin-login-hide-pti') . '

'; } } public function adlhpti_admin_enqueue_assets($hook) { if ($hook !== 'settings_page_adlhpti-settings') { return; } $base_url = plugin_dir_url(__FILE__) . '../assets/'; $base_path = plugin_dir_path(__FILE__) . '../assets/'; $css_path = $base_path . 'css/admin.css'; if (file_exists($css_path)) { wp_enqueue_style( 'adlhpti-admin-css', $base_url . 'css/admin.css', [], filemtime($css_path) ); } $js_path = $base_path . 'js/admin.js'; if (file_exists($js_path)) { wp_enqueue_script( 'adlhpti-admin-js', $base_url . 'js/admin.js', ['jquery'], filemtime($js_path), true ); } } public function adlhpti_custom_login_redirect() { $is_enabled = get_option('adlhpti_enabled', false); if (!$is_enabled) { return; } $custom_slug = trim(get_option('adlhpti_custom_login_slug', 'secure-login'), '/'); $request_uri_raw = isset($_SERVER['REQUEST_URI']) ? sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])) : ''; $parsed_request_uri = wp_parse_url($request_uri_raw); $request_path = isset($parsed_request_uri['path']) ? trim($parsed_request_uri['path'], '/') : ''; $parsed_site_url = wp_parse_url(site_url()); $site_path = isset($parsed_site_url['path']) ? trim($parsed_site_url['path'], '/') : ''; if (!empty($site_path) && strpos($request_path, $site_path) === 0) { $request_path = trim(substr($request_path, strlen($site_path)), '/'); } // Prevent direct wp-login.php unless POST or valid action if (strpos($request_path, 'wp-login.php') === 0) { $allowed_actions = ['logout', 'lostpassword', 'rp', 'resetpass', 'register']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only use of $_GET for display logic, not modifying data $action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only use of $_GET for display logic $logged_out = isset($_GET['loggedout']) ? sanitize_text_field(wp_unslash($_GET['loggedout'])) : ''; $method = strtoupper(sanitize_text_field(wp_unslash($_SERVER['REQUEST_METHOD'] ?? ''))); if ($method !== 'POST' && !in_array($action, $allowed_actions, true) && !$logged_out) { wp_die( esc_html__('Access denied. Please use the custom login URL.', 'admin-login-hide-pti'), '', ['response' => 403] ); } if ($method === 'GET' && in_array($action, ['lostpassword', 'rp', 'resetpass'], true)) { $redirect_url = site_url($custom_slug); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only use of $_GET for redirection logic. $redirect_url = add_query_arg($_GET, $redirect_url); wp_safe_redirect($redirect_url); exit; } } if ($request_path === $custom_slug) { if ($_SERVER['REQUEST_METHOD'] === 'POST') { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce is verified for custom actions (lostpassword, resetpass) below; default login is handled by WordPress core. $post_action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : 'login'; if ($post_action === 'lostpassword' || $post_action === 'retrievepassword') { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_key(wp_unslash($_POST['_wpnonce'])), 'adlhpti_lostpassword_nonce')) { wp_die(esc_html__('Nonce verification failed.', 'admin-login-hide-pti')); } $errors = retrieve_password(); if (is_wp_error($errors)) { wp_safe_redirect(add_query_arg('error', $errors->get_error_code(), site_url($custom_slug . '?action=lostpassword'))); exit; } else { wp_safe_redirect(site_url($custom_slug . '?checkemail=confirm')); exit; } } if ($post_action === 'resetpass' || $post_action === 'rp') { if (!isset($_POST['_wpnonce']) || !wp_verify_nonce(sanitize_key(wp_unslash($_POST['_wpnonce'])), 'adlhpti_resetpass_nonce')) { wp_die(esc_html__('Nonce verification failed.', 'admin-login-hide-pti')); } // Logic from wp-login.php for resetpass $rp_key = isset($_POST['rp_key']) ? sanitize_text_field(wp_unslash($_POST['rp_key'])) : ''; $rp_login = isset($_POST['rp_login']) ? sanitize_text_field(wp_unslash($_POST['rp_login'])) : ''; $user = check_password_reset_key($rp_key, $rp_login); if (!is_wp_error($user)) { if (isset($_POST['pass1'], $_POST['pass2']) && $_POST['pass1'] !== $_POST['pass2']) { wp_safe_redirect(add_query_arg(['action' => 'rp', 'key' => $rp_key, 'login' => $rp_login, 'error' => 'password_reset_mismatch'], site_url($custom_slug))); exit; } if (isset($_POST['pass1']) && !empty($_POST['pass1'])) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Passwords should not be sanitized as they can contain special characters. reset_password($user, wp_unslash($_POST['pass1'])); wp_safe_redirect(site_url($custom_slug . '?resetpass=complete')); exit; } } else { wp_safe_redirect(add_query_arg('error', $user->get_error_code(), site_url($custom_slug . '?action=lostpassword'))); exit; } } } if (is_user_logged_in() && !isset($_GET['action'])) { wp_safe_redirect(admin_url()); exit; } $action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : 'login'; $errors = new WP_Error(); ?> > >
'; if ($action === 'lostpassword' || $action === 'retrievepassword') { $this->adlhpti_render_lostpassword_form(); } elseif ($action === 'rp' || $action === 'resetpass') { $this->adlhpti_render_resetpassword_form(); } else { ?>

Login

' . esc_html__('Check your username / email address or password.', 'admin-login-hide-pti') . '

'; } } if (isset($_GET['checkemail']) && $_GET['checkemail'] === 'confirm') { echo '

' . esc_html__('Check your email for the confirmation link.', 'admin-login-hide-pti') . '

'; } if (isset($_GET['resetpass']) && $_GET['resetpass'] === 'complete') { echo '

' . esc_html__('Your password has been reset. You can now log in.', 'admin-login-hide-pti') . '

'; } wp_login_form([ 'redirect' => admin_url(), ]); echo ''; } echo '
'; wp_footer(); ?> 403] ); } } } public function adlhpti_custom_logout_redirect($redirect_to, $requested_redirect_to, $user) { $custom_slug = trim(get_option('adlhpti_custom_login_slug', 'secure-login'), '/'); return site_url($custom_slug . '?loggedout=true'); } public function adlhpti_custom_lostpassword_url($lostpassword_url, $redirect) { $is_enabled = get_option('adlhpti_enabled', false); if (!$is_enabled) { return $lostpassword_url; } $custom_slug = trim(get_option('adlhpti_custom_login_slug', 'secure-login'), '/'); $url = site_url($custom_slug . '?action=lostpassword'); if (!empty($redirect)) { $url = add_query_arg('redirect_to', urlencode($redirect), $url); } return $url; } public function adlhpti_custom_retrieve_password_message($message, $key, $user_login, $user_data) { $is_enabled = get_option('adlhpti_enabled', false); if (!$is_enabled) { return $message; } $custom_slug = trim(get_option('adlhpti_custom_login_slug', 'secure-login'), '/'); // Match any link that looks like a password reset link $pattern = '/(https?:\/\/[^\s]+wp-login\.php\?action=(?:rp|resetpass)[^\s]+)/'; $message = preg_replace_callback($pattern, function ($matches) use ($custom_slug) { $url = $matches[1]; return str_replace('wp-login.php', $custom_slug, $url); }, $message); return $message; } public function adlhpti_custom_login_url($login_url, $redirect, $force_reauth) { $is_enabled = get_option('adlhpti_enabled', false); if (!$is_enabled) { return $login_url; } $custom_slug = trim(get_option('adlhpti_custom_login_slug', 'secure-login'), '/'); $url = site_url($custom_slug); if (!empty($redirect)) { $url = add_query_arg('redirect_to', urlencode($redirect), $url); } return $url; } private function adlhpti_render_lostpassword_form() { ?>

Lost Password

' . esc_html($error_msg) . '

'; } $custom_slug = trim(get_option('adlhpti_custom_login_slug', 'secure-login'), '/'); ?>

Reset Password

' . esc_html__('The passwords do not match.', 'admin-login-hide-pti') . '

'; } } ?>