Created
September 1, 2015 21:12
-
-
Save aaronhuisinga/5cb046ea9ddbf37f0da8 to your computer and use it in GitHub Desktop.
WordPress function to change admin URL from /wp-admin/* to /admin/*
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Change Admin URL | |
* | |
* Copyright (C) 2010 hakre <http://hakre.wordpress.com/> | |
* | |
* USAGE: | |
* | |
* Copy the file into wp-content/mu-plugins directory and add the | |
* following RewriteRule to your apache configuration or .htaccess: | |
* | |
* Apache: RewriteRule ^admin/(.*)$ wp-admin/$1 [QSA,L] | |
* Nginx: rewrite ^/admin/(.*)? /wp-admin/$1 last; | |
*/ | |
return ChangeAdminUrlPlugin::bootstrap(); | |
class ChangeAdminUrlPlugin { | |
private $renameFrom = 'wp-admin'; | |
private $renameTo = 'admin'; | |
static $instance; | |
static public function bootstrap() | |
{ | |
null === self::$instance | |
&& self::$instance = new self(); | |
return self::$instance; | |
} | |
private function setCookiePath() | |
{ | |
defined( 'SITECOOKIEPATH' ) || define( 'SITECOOKIEPATH', preg_replace( '|https?://[^/]+|i', '', get_option( 'siteurl' ) . '/' ) ); | |
defined( 'ADMIN_COOKIE_PATH' ) || define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH . $this->renameTo ); | |
} | |
public function __construct() | |
{ | |
$this->setCookiePath(); | |
add_action( 'init', [ $this, 'init' ] ); | |
} | |
public function init() | |
{ | |
add_filter( 'admin_url', [ $this, 'admin_url' ], 10, 3 ); | |
add_filter( 'network_admin_url', [ | |
$this, | |
'network_admin_url' | |
], 10, 3 ); | |
} | |
public function admin_url( $url, $path, $blog_id ) | |
{ | |
$renameFrom = $this->renameFrom; | |
$renameTo = $this->renameTo; | |
$scheme = 'admin'; | |
$find = get_site_url( $blog_id, $renameFrom . '/', $scheme ); | |
$replace = get_site_url( $blog_id, $renameTo . '/', $scheme ); | |
( 0 === strpos( $url, $find ) ) | |
&& $url = $replace . substr( $url, strlen( $find ) ); | |
return $url; | |
} | |
public function network_admin_url( $url, $path ) | |
{ | |
$renameFrom = $this->renameFrom; | |
$renameTo = $this->renameTo; | |
$scheme = 'admin'; | |
$find = network_site_url( $renameFrom . '/', $scheme ); | |
$replace = network_site_url( $renameTo . '/', $scheme ); | |
( 0 === strpos( $url, $find ) ) | |
&& $url = $replace . substr( $url, strlen( $find ) ); | |
return $url; | |
} | |
} |
Seems it is not working properly
on open /admin/ it redirects on wp-login.php page
and /wp-admin/ is still available if type directy.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! This worked wonderfully! This was the only solution I found online which worked correctly! thanks again!