Snippet

Secure Your Site: Change WordPress Login Page URL Easily

How to change wordpress login urlChange wordpress admin login urlCustom wordpress login page urlWordpress change wp-admin urlChange wordpress login page url pluginWordpress login url customizationSecure wordpress login urlModify wordpress login urlWordpress login url change tutorialBest plugin to change wordpress login url

Explanation

Changing the login URL in WordPress can help enhance your site's security by making it less predictable for potential intruders. Here's a simple way to do it using a bit of code.

Redirecting the Default Login URL:

  • The code hooks into WordPress's initialization process to check if someone is trying to access the default login page (wp-login.php).
  • If they are, it redirects them to a new URL of your choice, like /custom-login-url.

Handling the Custom Login URL:

  • When someone visits your new login URL, the code ensures that the WordPress login page is loaded properly.

Blocking Direct Access to wp-login.php:

  • To prevent anyone from accessing the old login page, the code redirects any requests for wp-login.php back to your site's homepage.

Updating the Login URL:

  • The code also updates the login URL used throughout WordPress, ensuring that any links or redirects point to your new custom login URL.

Remember, after implementing this change, you'll need to use the new URL to log in to your WordPress site. This approach doesn't require a plugin, but if you're not comfortable with code, there are plugins available that can achieve the same result with a few clicks.

Code

1<?php 2// Hook into 'init' to change the login URL 3add_action('init', 'wp_dudecom_change_login_url'); 4 5function wp_dudecom_change_login_url() { 6 // Check if the current request is for the login page 7 if (strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) { 8 // Redirect to the new login URL 9 wp_redirect(home_url('/custom-login-url')); 10 exit; 11 } 12} 13 14// Hook into 'login_init' to handle the custom login URL 15add_action('login_init', 'wp_dudecom_handle_custom_login_url'); 16 17function wp_dudecom_handle_custom_login_url() { 18 // Check if the current request is for the custom login URL 19 if (strpos($_SERVER['REQUEST_URI'], 'custom-login-url') !== false) { 20 // Load the WordPress login page 21 require_once ABSPATH . 'wp-login.php'; 22 exit; 23 } 24} 25 26// Hook into 'template_redirect' to prevent access to wp-login.php 27add_action('template_redirect', 'wp_dudecom_prevent_wp_login_access'); 28 29function wp_dudecom_prevent_wp_login_access() { 30 // Check if the current request is for wp-login.php 31 if (strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) { 32 // Redirect to the home page 33 wp_redirect(home_url()); 34 exit; 35 } 36} 37 38// Hook into 'login_url' filter to change the login URL 39add_filter('login_url', 'wp_dudecom_custom_login_url', 10, 3); 40 41function wp_dudecom_custom_login_url($login_url, $redirect, $force_reauth) { 42 // Return the custom login URL 43 return home_url('/custom-login-url'); 44} 45?>

Instructions

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure you have access to your WordPress site's files via FTP or a file manager.
  • Backup your site before making changes to the code.

Implementation Steps:

  1. Open your WordPress theme's functions.php file or create a new custom plugin file.
  2. Copy and paste the provided code into the file.
  3. Replace /custom-login-url with your desired custom login URL.
  4. Save the changes to the file.
  5. Test the new login URL by navigating to http://yourwebsite.com/custom-login-url.
  6. Ensure that the old login URL wp-login.php redirects to your homepage.

Note: After implementing this change, remember to use the new custom login URL to access your WordPress admin area.

If you need assistance with this implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert help.