Methods to enhance WordPress background URL security, no plug -in!
This article introduces a 100%effective WordPress background URL security enhancement method without installing any plug -in. Just copy the following code into your
file, or use the code fragment plug -in to paste. functions.php
/your-url/
<code class="language-php">function redirect_default_login() { // 如果有人尝试直接访问wp-login.php(未登录或登出),则重定向他们。 if (strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false && !isset($_POST['log']) && (!isset($_GET['action']) || $_GET['action'] !== 'logout')) { wp_safe_redirect(home_url()); // 将他们重定向到主页(或您指定的其他页面)。 exit; // 停止进一步执行。 } } add_action('init', 'redirect_default_login');</code>
<code class="language-php">function handle_custom_login_url() { // 定义您的自定义登录slug(例如,“your-url”)。 $custom_login_slug = 'your-url'; // 这是您要用于登录的自定义URL。 // 检查是否有人正在访问自定义登录URL。 if (strpos($_SERVER['REQUEST_URI'], $custom_login_slug) !== false) { // 显示WordPress登录页面。 require_once ABSPATH . 'wp-login.php'; exit; // 停止进一步执行。 } } add_action('init', 'handle_custom_login_url');</code>
<code class="language-php">function prevent_default_login_action($action) { // 如果有人尝试使用默认登录操作并且他们不在自定义登录页面上,则重定向他们。 if ($action === 'login' && strpos($_SERVER['REQUEST_URI'], 'your-url') === false) { wp_safe_redirect(home_url()); exit; // 停止进一步执行。 } } add_action('login_init', 'prevent_default_login_action');</code>
<code class="language-php">function custom_logout_redirect() { // 注销后,将用户重定向到自定义登录页面。 wp_safe_redirect(home_url('/your-url')); // 将他们发送到自定义登录页面。 exit; // 停止进一步执行。 } add_action('wp_logout', 'custom_logout_redirect');</code>
Create a custom login URL (for example, yoursite.com/your-urll), which is used to display the WordPress login page.
Prevent the default login operation, unless the user is on the custom login page.
After the user is canceled, it redirects it to the custom login page.
In short, this setting makes the attacker more difficult to find and use your login page to improve security, and at the same time provide seamless experience for legal users.
Thank you for reading and look forward to sharing more content with you in the future.
Follow my other platforms:
linkedin| Medium | Bluesky
The above is the detailed content of Did you know you can secure WordPress admin URL without a plugin?. For more information, please follow other related articles on the PHP Chinese website!