is_custom_login_page()) { return; } $css_path = plugin_dir_path(__FILE__) . '../assets/css/frontend.css'; $css_url = plugin_dir_url(__FILE__) . '../assets/css/frontend.css'; wp_enqueue_style( 'adlhpti-frontend-css', $css_url, [], file_exists($css_path) ? filemtime($css_path) : null ); wp_enqueue_style( 'adlhpti-google-fonts', 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap', [], '1.0.5' ); $this->adlhpti_isolate_login_styles(); $this->adlhpti_isolate_login_scripts(); } public function adlhpti_isolate_login_styles() { if (!$this->is_custom_login_page()) { return; } global $wp_styles; if (!($wp_styles instanceof WP_Styles)) { return; } $allowed_styles = [ 'adlhpti-frontend-css', 'adlhpti-google-fonts', 'dashicons', ]; foreach ($wp_styles->queue as $handle) { if (!in_array($handle, $allowed_styles, true)) { wp_dequeue_style($handle); } } } public function adlhpti_isolate_login_scripts() { if (!$this->is_custom_login_page()) { return; } global $wp_scripts; if (!($wp_scripts instanceof WP_Scripts)) { return; } $allowed_scripts = [ 'jquery', 'jquery-core', 'jquery-migrate', ]; foreach ($wp_scripts->queue as $handle) { if (!in_array($handle, $allowed_scripts, true)) { wp_dequeue_script($handle); } } } public function add_settings_page() { add_options_page( 'Admin Login Hide PTI', 'Admin Login Hide PTI', 'manage_options', 'adlhpti-settings', [$this, 'adlhpti_render_settings_page'] ); } public function adlhpti_register_settings() { register_setting( 'adlhpti_settings_group', 'adlhpti_custom_login_slug', [ 'type' => '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 ); wp_localize_script('adlhpti-admin-js', 'adlhptiData', [ 'siteUrl' => trailingslashit(site_url()), 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('adlhpti_admin_nonce'), 'saving' => __('Saving...', 'admin-login-hide-pti'), 'saved' => __('Settings saved in real-time!', 'admin-login-hide-pti'), 'error' => __('Failed to save settings. Please try again.', 'admin-login-hide-pti'), 'copied' => __('Copied!', 'admin-login-hide-pti'), ]); } } public function adlhpti_ajax_save_settings() { if (!check_ajax_referer('adlhpti_admin_nonce', 'nonce', false)) { wp_send_json_error(['message' => __('Invalid security token. Please refresh the page.', 'admin-login-hide-pti')]); } if (!current_user_can('manage_options')) { wp_send_json_error(['message' => __('You do not have permission to manage options.', 'admin-login-hide-pti')]); } $raw_slug = isset($_POST['adlhpti_custom_login_slug']) ? sanitize_text_field(wp_unslash($_POST['adlhpti_custom_login_slug'])) : 'secure-login'; $slug = sanitize_title($raw_slug); if (empty($slug)) { $slug = 'secure-login'; } $enabled = isset($_POST['adlhpti_enabled']) && ($_POST['adlhpti_enabled'] === '1' || $_POST['adlhpti_enabled'] === 'true' || $_POST['adlhpti_enabled'] == 1); update_option('adlhpti_custom_login_slug', $slug); update_option('adlhpti_enabled', $enabled ? 1 : 0); $new_url = site_url('/' . $slug); wp_send_json_success([ 'message' => __('Settings saved in real-time!', 'admin-login-hide-pti'), 'slug' => $slug, 'url' => $new_url, 'enabled' => $enabled, ]); } 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', 'postpass']; // 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()) { $current_action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : ''; if ($current_action !== 'logout') { $target_redirect = !empty($_REQUEST['redirect_to']) ? wp_validate_redirect(esc_url_raw(wp_unslash($_REQUEST['redirect_to'])), admin_url()) : admin_url(); wp_safe_redirect($target_redirect); exit; } } $action = isset($_GET['action']) ? sanitize_text_field(wp_unslash($_GET['action'])) : 'login'; $errors = new WP_Error(); ?> > <?php if ($action === 'lostpassword' || $action === 'retrievepassword') { echo esc_html__('Lost Password', 'admin-login-hide-pti'); } elseif ($action === 'rp' || $action === 'resetpass') { echo esc_html__('Reset Password', 'admin-login-hide-pti'); } else { echo esc_html__('Log In', 'admin-login-hide-pti'); } echo ' ‹ ' . esc_html(get_bloginfo('name')); ?> >
'; if ($action === 'lostpassword' || $action === 'retrievepassword') { $this->adlhpti_render_lostpassword_form(); } elseif ($action === 'rp' || $action === 'resetpass') { $this->adlhpti_render_resetpassword_form(); } else { ?>

Login

' . esc_html__('Please enter your username and password.', 'admin-login-hide-pti') . '

'; } else { echo '

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

'; } } } if (isset($_GET['loggedout']) && sanitize_text_field(wp_unslash($_GET['loggedout'])) === 'true') { echo '

' . esc_html__('You are now logged out.', 'admin-login-hide-pti') . '

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

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

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

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

'; } $target_redirect = !empty($_REQUEST['redirect_to']) ? wp_validate_redirect(esc_url_raw(wp_unslash($_REQUEST['redirect_to'])), admin_url()) : admin_url(); $adlhpti_form = wp_login_form([ 'redirect' => $target_redirect, 'echo' => false, ]); // Theme/other plugins can inject their own "Lost your password?" link into the form - remove it, we print ours below. $adlhpti_form = preg_replace('#]*>\s*Lost your password\?\s*#i', '', $adlhpti_form); $adlhpti_form = preg_replace('#]*href=[^>]*lostpassword[^>]*>.*?#is', '', $adlhpti_form); $adlhpti_form = preg_replace('#<(p|div|span)\b[^>]*>\s*#i', '', $adlhpti_form); echo $adlhpti_form; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output of wp_login_form(). 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') . '

'; } } ?>