Bảo mật trang quản trị WP bằng code

Add to wp-config.php

Mã bảo mật

define('MUATHEME_SECURITY_CODE', '181200');

Code add to function.php

<?php
// Khởi động session nếu chưa có
function muathemewpgiare_start_session() {
    if (session_status() === PHP_SESSION_NONE) {
        session_start();
    }
}
add_action('init', 'muathemewpgiare_start_session');

// Mã bảo mật cố định lưu trong wp-config.php
function muathemewpgiare_generate_security_code() {
    return defined('MUATHEME_SECURITY_CODE') ? MUATHEME_SECURITY_CODE : '';
}

// Chặn truy cập wp-admin và wp-login.php nếu chưa xác thực
function muathemewpgiare_secure_admin() {
    if ((is_admin() || $_SERVER['REQUEST_URI'] === wp_login_url()) && !is_user_logged_in()) {
        if (empty($_SESSION['admin_access']) || $_SESSION['admin_access'] !== muathemewpgiare_generate_security_code()) {
            muathemewpgiare_show_security_form();
            exit();
        }
    }
}
add_action('init', 'muathemewpgiare_secure_admin');

// Hiển thị form nhập mã bảo mật
function muathemewpgiare_show_security_form() {
    ?>
    <style>
        body { margin: 0; padding: 0; box-sizing: border-box; }
        #muathemewpgiare-popup {
            position: fixed; top: 0; left: 0; right: 0; bottom: 0;
            background: #f3f4f6; z-index: 999999; display: flex; justify-content: center; align-items: center;
        }
        #muathemewpgiare-popup form {
            background: #fff; padding: 40px; border-radius: 10px; box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
            text-align: center; max-width: 400px; width: 90%;
        }
        #muathemewpgiare-popup input {
            padding: 10px; font-size: 18px; margin: 5px; width: 40px; text-align: center;
            border: 1px solid #ccc; border-radius: 5px; outline: none;
        }
        #muathemewpgiare-popup button {
            margin-top: 20px; padding: 10px 20px; font-size: 16px; cursor: pointer;
            background: #4f46e5; color: #fff; border: none; border-radius: 5px;
        }
        #muathemewpgiare-popup button:hover {
            background: #4338ca;
        }
        #muathemewpgiare-popup small {
            display: block; margin-top: 20px; color: #888;
        }
    </style>

    <div id="muathemewpgiare-popup">
        <form method="POST">
            <h3>Nhập mã bảo mật để truy cập</h3>
            <div id="code-inputs">
                <?php for ($i = 0; $i < 6; $i++): ?>
                    <input type="text" maxlength="1" name="admin_code[]" oninput="moveNext(this)" required>
                <?php endfor; ?>
            </div>
            <button type="submit" name="submit_code">Xác nhận</button>
            <small>© Bản quyền: muathemewpgiare.com</small>
        </form>
    </div>

    <script>
        function moveNext(input) {
            if (input.value.length === 1) {
                let next = input.nextElementSibling;
                if (next && next.tagName === 'INPUT') next.focus();
            }
        }
    </script>
    <?php

    // Xử lý kiểm tra mã bảo mật
    if (isset($_POST['submit_code'])) {
        $input_code = implode('', array_map('sanitize_text_field', $_POST['admin_code']));
        if ($input_code === muathemewpgiare_generate_security_code()) {
            $_SESSION['admin_access'] = muathemewpgiare_generate_security_code();
            echo '<script>location.reload();</script>';
            exit();
        } else {
            echo '<script>alert("❌ Mã truy cập không chính xác!");</script>';
        }
    }
    exit();
}

}

Last updated