/** * ===================================================== * CUSTOM LOGIN PAGE - DIGITAL TRANSFORMATION STYLE * ===================================================== */ /** * Enqueue custom login styles and scripts */ function danguy_custom_login_assets() { // Google Fonts Inter for login page (instead of @import in CSS) wp_enqueue_style('google-fonts-inter', 'https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:wght@300;400;500;600;700;800;900&display=swap', array(), null); // Custom Login CSS wp_enqueue_style( 'danguy-login-style', DANGUY_URI . '/assets/css/login.css', array(), DANGUY_VERSION ); // Font Awesome for icons wp_enqueue_style( 'font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css', array(), '6.5.1' ); // Custom Login Robot Animation JS wp_enqueue_script( 'danguy-login-robot', DANGUY_URI . '/assets/js/login-robot.js', array(), DANGUY_VERSION, true ); } add_action('login_enqueue_scripts', 'danguy_custom_login_assets'); /** * Output theme color CSS variables on login page * This ensures login page uses the same colors as the main site */ function danguy_login_color_css() { // Get theme color from option or cookie $default_color = danguy_get_option('general_default_color', 'red'); // Check for cookie override (user's preference) if (isset($_COOKIE['danguy_theme']) && in_array($_COOKIE['danguy_theme'], array('red', 'yellow', 'blue', 'green'))) { $default_color = sanitize_text_field($_COOKIE['danguy_theme']); } // Color definitions $themes = array( 'red' => array( 'primary' => '#8B0000', 'primary-light' => '#c62828', 'primary-dark' => '#6d0000', 'gradient' => 'linear-gradient(135deg, #8B0000 0%, #c62828 100%)', ), 'yellow' => array( 'primary' => '#f57f17', 'primary-light' => '#f9a825', 'primary-dark' => '#e65100', 'gradient' => 'linear-gradient(135deg, #f57f17 0%, #f9a825 100%)', ), 'blue' => array( 'primary' => '#0d47a1', 'primary-light' => '#1565c0', 'primary-dark' => '#002171', 'gradient' => 'linear-gradient(135deg, #0d47a1 0%, #1565c0 100%)', ), 'green' => array( 'primary' => '#1b5e20', 'primary-light' => '#2e7d32', 'primary-dark' => '#0a3d0a', 'gradient' => 'linear-gradient(135deg, #1b5e20 0%, #2e7d32 100%)', ), ); $t = isset($themes[$default_color]) ? $themes[$default_color] : $themes['red']; $css = ':root {'; $css .= '--dg-primary:' . $t['primary'] . ';'; $css .= '--dg-primary-light:' . $t['primary-light'] . ';'; $css .= '--dg-primary-dark:' . $t['primary-dark'] . ';'; $css .= '--dg-gradient:' . $t['gradient'] . ';'; $css .= '--login-primary:' . $t['primary'] . ';'; $css .= '--login-primary-light:' . $t['primary-light'] . ';'; $css .= '--login-primary-hover:' . $t['primary-dark'] . ';'; $css .= '--login-gradient:' . $t['gradient'] . ';'; $css .= '}'; echo ''; } add_action('login_head', 'danguy_login_color_css', 1); /** * Change login logo URL to home page */ function danguy_login_logo_url() { return home_url('/'); } add_filter('login_headerurl', 'danguy_login_logo_url'); /** * Change login logo title */ function danguy_login_logo_title() { return get_bloginfo('name') . ' - Theme News CQNN'; } add_filter('login_headertext', 'danguy_login_logo_title'); /** * Custom login logo using theme logo or custom logo */ function danguy_custom_login_logo() { $custom_logo_id = get_theme_mod('custom_logo'); $logo_url = ''; if ($custom_logo_id) { $logo_url = wp_get_attachment_image_url($custom_logo_id, 'full'); } else { // Fallback to theme logo $logo_url = DANGUY_URI . '/assets/images/logo.svg'; } if ($logo_url) { echo ''; } } add_action('login_head', 'danguy_custom_login_logo'); /** * Add body class for login page type and theme color */ function danguy_login_body_class($classes) { $action = isset($_GET['action']) ? $_GET['action'] : 'login'; $classes[] = 'danguy-' . sanitize_html_class($action); // Add theme color class $default_color = danguy_get_option('general_default_color', 'red'); if (isset($_COOKIE['danguy_theme']) && in_array($_COOKIE['danguy_theme'], array('red', 'yellow', 'blue', 'green'))) { $default_color = sanitize_text_field($_COOKIE['danguy_theme']); } $classes[] = 'theme-' . sanitize_html_class($default_color); return $classes; } add_filter('login_body_class', 'danguy_login_body_class'); /** * Custom login error messages (more user-friendly) */ function danguy_login_error_messages($error) { global $errors; if (is_wp_error($errors)) { $error_codes = $errors->get_error_codes(); // Override generic error messages with friendly ones if (in_array('invalid_username', $error_codes)) { return 'Lá»—i: Tên đăng nhập không tồn tại trong hệ thống.'; } if (in_array('incorrect_password', $error_codes)) { return 'Lá»—i: Máºt khẩu không chÃnh xác. Vui lòng thá» lại.'; } if (in_array('empty_username', $error_codes)) { return 'Lá»—i: Vui lòng nháºp tên đăng nhập.'; } if (in_array('empty_password', $error_codes)) { return 'Lá»—i: Vui lòng nháºp máºt khẩu.'; } } return $error; } add_filter('login_errors', 'danguy_login_error_messages'); /** * Redirect after login (optional - customize as needed) */ function danguy_login_redirect($redirect_to, $request, $user) { // Check if user is admin if (isset($user->roles) && is_array($user->roles)) { if (in_array('administrator', $user->roles)) { return admin_url(); } // For other users, redirect to home page or custom page return home_url('/'); } return $redirect_to; } add_filter('login_redirect', 'danguy_login_redirect', 10, 3); /** * Add custom message above login form */ function danguy_login_message($message) { $action = isset($_GET['action']) ? $_GET['action'] : 'login'; if (empty($message) && $action === 'login') { // You can add a welcome message here // return '
'; } return $message; } add_filter('login_message', 'danguy_login_message'); /** * ===================================================== * CUSTOM LOGIN URL - Change /wp-login.php to /login * ===================================================== */ /** * Add rewrite rule for custom login URL */ function danguy_login_rewrite_rules() { add_rewrite_rule('^login/?$', 'wp-login.php', 'top'); add_rewrite_rule('^login/(.+)$', 'wp-login.php?action=$matches[1]', 'top'); add_rewrite_rule('^dang-ky/?$', 'wp-login.php?action=register', 'top'); add_rewrite_rule('^quen-mat-khau/?$', 'wp-login.php?action=lostpassword', 'top'); } add_action('init', 'danguy_login_rewrite_rules'); /** * Filter login URL to use custom path */ function danguy_custom_login_url($login_url, $redirect, $force_reauth) { $login_url = home_url('/login/'); if (!empty($redirect)) { $login_url = add_query_arg('redirect_to', urlencode($redirect), $login_url); } if ($force_reauth) { $login_url = add_query_arg('reauth', '1', $login_url); } return $login_url; } add_filter('login_url', 'danguy_custom_login_url', 10, 3); /** * Filter register URL */ function danguy_custom_register_url($register_url) { return home_url('/dang-ky/'); } add_filter('register_url', 'danguy_custom_register_url'); /** * Filter lost password URL */ function danguy_custom_lostpassword_url($lostpassword_url, $redirect) { $url = home_url('/quen-mat-khau/'); if (!empty($redirect)) { $url = add_query_arg('redirect_to', urlencode($redirect), $url); } return $url; } add_filter('lostpassword_url', 'danguy_custom_lostpassword_url', 10, 2); /** * Redirect old wp-login.php to new login URL (optional - for security) * Uncomment the add_action line below to enable this feature */ function danguy_redirect_old_login() { $page_viewed = basename($_SERVER['REQUEST_URI']); // Only redirect if accessing wp-login.php directly (not ajax or other) if (strpos($page_viewed, 'wp-login.php') !== false && !isset($_GET['action'])) { // Don't redirect POST requests (login form submission) if ($_SERVER['REQUEST_METHOD'] !== 'POST') { wp_redirect(home_url('/login/')); exit; } } } // Uncomment line below to redirect wp-login.php to /login // add_action('init', 'danguy_redirect_old_login'); /** * Flush rewrite rules on theme activation */ function danguy_flush_rewrite_rules_on_activation() { danguy_login_rewrite_rules(); flush_rewrite_rules(); } add_action('after_switch_theme', 'danguy_flush_rewrite_rules_on_activation'); /** * Create login pages automatically on theme activation */ function danguy_create_login_pages() { // Define pages to create $pages = array( 'login' => array( 'post_title' => 'Äăng nháºp', 'post_name' => 'login', 'post_content' => '', ), 'dang-ky' => array( 'post_title' => 'Äăng ký', 'post_name' => 'dang-ky', 'post_content' => '', ), 'quen-mat-khau' => array( 'post_title' => 'Quên máºt khẩu', 'post_name' => 'quen-mat-khau', 'post_content' => '', ), ); foreach ($pages as $key => $page) { // Check if page already exists by slug $existing_page = get_page_by_path($page['post_name']); if (!$existing_page) { // Create the page $page_id = wp_insert_post(array( 'post_title' => $page['title'], 'post_name' => $page['slug'], 'post_content' => $page['content'], 'post_status' => 'publish', 'post_type' => 'page', 'post_author' => 1, 'comment_status' => 'closed', 'ping_status' => 'closed', )); if ($page_id && !is_wp_error($page_id)) { // Store created page ID in options for reference update_option('danguy_page_' . $key, $page_id); } } } } add_action('after_switch_theme', 'danguy_create_login_pages'); /** * Delete login pages when theme is deactivated (optional) * Uncomment the add_action line below to enable this feature */ function danguy_delete_login_pages() { $page_slugs = array('login', 'dang-ky', 'quen-mat-khau'); foreach ($page_slugs as $slug) { $page = get_page_by_path($slug); if ($page) { wp_delete_post($page->ID, true); // true = force delete (bypass trash) } delete_option('danguy_page_' . $slug); } } // Uncomment line below to delete pages when switching away from this theme // add_action('switch_theme', 'danguy_delete_login_pages'); /** * Redirect login pages to wp-login.php with appropriate action * This allows the custom pages to display the actual login/register/lostpassword forms */ function danguy_login_page_redirect() { if (!is_page()) { return; } global $post; // Map page slugs to wp-login.php actions $page_actions = array( 'login' => '', 'dang-ky' => 'register', 'quen-mat-khau' => 'lostpassword', ); $current_slug = $post->post_name; if (array_key_exists($current_slug, $page_actions)) { // Set the action for wp-login.php $action = $page_actions[$current_slug]; if (!empty($action)) { $_REQUEST['action'] = $action; $_GET['action'] = $action; } // Include wp-login.php and exit require_once(ABSPATH . 'wp-login.php'); exit; } } add_action('template_redirect', 'danguy_login_page_redirect');