Skip to content

Instantly share code, notes, and snippets.

@aaronhuisinga
Created September 1, 2015 21:12
Show Gist options
  • Save aaronhuisinga/5cb046ea9ddbf37f0da8 to your computer and use it in GitHub Desktop.
Save aaronhuisinga/5cb046ea9ddbf37f0da8 to your computer and use it in GitHub Desktop.
WordPress function to change admin URL from /wp-admin/* to /admin/*
<?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;
}
}
@EllisBenus
Copy link

Thank you! This worked wonderfully! This was the only solution I found online which worked correctly! thanks again!

@Akots
Copy link

Akots commented Sep 17, 2020

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