Skip to content

Instantly share code, notes, and snippets.

@em-piguet
Forked from tazeverywhere/wp_changeLoginUrl.php
Last active May 3, 2022 22:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save em-piguet/f0482886996b48dec8e0 to your computer and use it in GitHub Desktop.
Save em-piguet/f0482886996b48dec8e0 to your computer and use it in GitHub Desktop.
WP - change login/admin URL
<?php
//////////////////////
// SECURE WP_ADMIN //
//////////////////////
// ---- 1. edit wp-config.php
/* tells wordpress what the new directory is, and updates the cookie path.
define( 'WP_ADMIN_DIR', 'manager' );
define( 'ADMIN_COOKIE_PATH', '/' );
*/
// ---- 2. Url Rewrite
/* for APACHE
RewriteRule ^manager/(.*) wp-admin/$1?%{QUERY_STRING} [L]
RewriteRule ^connect$ wp-login.php
*/
/* for NGINX
location ~ ^/ifSubFolder/manager/(.*)$ {
try_files $uri $uri/ /ifSubFolder/wp-admin/$1;
}
location ~ ^/ifSubFolder/connect {
try_files $uri $uri/ /ifSubFolder/wp-login.php?$args;
}
*/
// NEXT in functions.php
// ---- 3. redirects the user if there are trying to reach either of the secure areas.
if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') !== false || strpos($_SERVER['REQUEST_URI'], 'wp-login') !== false || strpos($_SERVER['REQUEST_URI'], 'admin') !== false || strpos($_SERVER['REQUEST_URI'], 'login') !== false) {
header("HTTP/1.1 301 Moved Permanently");
header('Location: /ifSubFolder/'); die();
}
// ---- 4. replace wp-admin url’s with the proper name.
add_filter('site_url', 'wpadmin_filter', 10, 3);
function wpadmin_filter( $url, $path, $orig_scheme ) {
$old = array( "/(wp-admin)/");
$admin_dir = WP_ADMIN_DIR;
$new = array($admin_dir);
return preg_replace( $old, $new, $url, 1);
}
// ---- 5. make sure the login page redirects to the proper page.
add_action('login_form','redirect_wp_admin');
function redirect_wp_admin(){
$redirect_to = $_SERVER['REQUEST_URI'];
if(count($_REQUEST)> 0 && array_key_exists('redirect_to', $_REQUEST)) {
$redirect_to = $_REQUEST['redirect_to'];
}
}
// --- 6. function to handle the same issues the wpadmin_filter does with naming.
add_filter('site_url', 'wplogin_filter', 10, 3);
function wplogin_filter( $url, $path, $orig_scheme ) {
$old = array( "/(wp-login\.php)/");
$new = array( "connect");
return preg_replace( $old, $new, $url, 1);
}
// END SECURE ADMIN/LOGIN URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment