There are various ways to set (and get) wordpress admin directory. I do not understand why wordpress has not included that!

Method 1:

if (!defined('WP_ADMIN_DIR')) define('WP_ADMIN_DIR', './wp-admin/');

In this method the WP_ADMIN_DIR constant will be very vague and will create lots of trouble. This quick method is useful on local development environment.

Method 2:

$WP_DIR = explode('/', WP_CONTENT_DIR);
if (!defined('WP_ADMIN_DIR')) define('WP_ADMIN_DIR', $WP_DIR[0] . '/wp-admin/');

This method works well if the platform is Windows. On Windows, it will print C:wampwwwwordpress/wp-admin/. But on Linux platform (ie. Fedora, CentOS) it will printĀ  /var/wp-admin/ which is absolutely wrong path. It should be /var/www/html/wordpress/wp-admin/

Method 3:

$WP_DIR = explode('/wp-content/', WP_CONTENT_DIR);
if (!defined('WP_ADMIN_DIR')) define('WP_ADMIN_DIR', $WP_DIR[0] . '/wp-admin/');

This method is similar to method 2 and will print /var/www/html/wordpress/wp-admin/ path onto Linux platform as it will split exact string which we expect. But again Windows and Linux path differences persist in this method too.

Method 4:

$WP_DIR = substr(WP_CONTENT_DIR, 0, -10);
if (!defined('WP_ADMIN_DIR')) define('WP_ADMIN_DIR', $WP_DIR . 'wp-admin/');

This method works cross-platforms. Here substr() will exclude last 10 characters from WP_CONTENT_DIR and WP_ADMIN_DIR constant value will be C:wampwwwwordpress/wp-admin/ (on Windows) and /var/www/html/wordpress/wp-admin/ (on Linux). But if WP_CONTENT_DIR has last trailing slash then we will need to exclude last 11 characters. Else it will print wrong path like C:wampwwwwordpresswp-admin/ or /var/www/html/wordpresswp-admin/

Method 5:

$WP_DIR = substr_replace(WP_CONTENT_DIR, '', -10);
if (!defined('WP_ADMIN_DIR')) define('WP_ADMIN_DIR', $WP_DIR . 'wp-admin/');

This method is similar to method 4. Only difference is substr_replace() replaces the characters whereas substr() excludes the characters.

Method 6 (more secure):

if ( strrpos(WP_CONTENT_DIR, '/wp-content/', 1) !== false) {
    $WP_ADMIN_DIR = substr(WP_CONTENT_DIR, 0, -10) . 'wp-admin/';
} else {
    $WP_ADMIN_DIR = substr(WP_CONTENT_DIR, 0, -11) . '/wp-admin/';
}
if (!defined('WP_ADMIN_DIR')) define('WP_ADMIN_DIR', $WP_ADMIN_DIR);

This method is more secure as strrpos() will first check whether WP_CONTENT_DIR has /wp-content/ string at the last occurrence. If it has then substr() will exclude last 10 else 11 characters. Thus WP_ADMIN_DIR constant value will be C:wampwwwwordpress/wp-admin/ (on Windows) and /var/www/html/wordpress/wp-admin/ (on Linux).

WordPress admin url:

If you need wordpress admin url then wordpress providesĀ admin_url() function to get the admin url. It will print the url as http://localhost/wordpress/wp-admin/

You might also like: