WordPress

This is a page for that and all its other awesomeness that it packs.
Below you can find Tips, Tricks, and Even some Help with your WordPress Site or Install


Renaming WP-Admin

To rename the wordpress admin you need to take two steps.

In the following code I'm using dashboard as the name of my new wp-admin. Change dashboard in the code below to whatever you want to name your new admin.

First you need to tell wordpress you want to change the admin url.

On line 2558 wp-includes/link-template.php is the code that dertermines the admin url.

Using the admin_url filter you can successfully change the url of the admin with the following function:

function my_custom_admin_url($path) { 
    return str_replace('wp-admin', 'dashboard', $path); 
}
add_filter('admin_url', 'my_custom_admin_url');

You can test to see what your new url is by doing this:

function whats_my_admin_url() {
    $url = admin_url();
    echo '<pre><code>'; print_r( $url ); echo '</code></pre>';
    }
add_action( 'admin_notices', 'whats_my_admin_url' );

However, if you'll notice when clicking through the admin that not everything works and some of the links may give you 404 not found or something similar.

Second, change the .htaccess in your wordpress root directory and add the following in the begining before anything else.

#CUSTOM ADMIN URL REWRITE
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^dashboard[^/]*$ dashboard/ [R=301,L]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule ^dashboard(.*)$ wp-admin$1? [QSA,L,NE]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule ^wp-admin/?$ / [NE,R=404,L]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule ^wp-admin/(.*)$ dashboard/$1 [QSA,R=301,L,NE]
</IfModule>
#CUSTOM ADMIN URL REWRITE

Now, I'm not an expert when it comes to editing .htaccess so some of this might not be necessary. However, I've never found it not to work.

Here's the whole thing. Create a file and drop in your plugins folder or mu-plugins folder. (remember to change every instance of dashboard to your preferred admin url)

<?php
/**
 * Plugin Name: Change My Admin URL
 * Plugin URI: http://wordpress.stackexchange.com/questions/106/can-i-rename-the-wp-admin-folder
 * Description: Changes the admin url where wp-admin becomes dashboard (or whatever you change it to)
 * Version: 1.0
 * Author: Bryan Willis
 * Author URI: http://profiles.wordpress.org/codecandid
 * License: GPL2
 */

/* 

#CUSTOM ADMIN URL REWRITE FOR HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^dashboard[^/]*$ dashboard/ [R=301,L]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule ^dashboard(.*)$ wp-admin$1? [QSA,L,NE]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule ^wp-admin/?$ / [NE,R=404,L]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule ^wp-admin/(.*)$ dashboard/$1 [QSA,R=301,L,NE]
</IfModule>
#CUSTOM ADMIN URL REWRITE

*/

function my_custom_admin_url($path) { 
    return str_replace('wp-admin', 'dashboard', $path); 
}
add_filter('admin_url', 'my_custom_admin_url');

Issues?

I haven't had any in over a year using this method. You might notice that wp-admin will still work which kind of sucks, but it's more of a precaution than anything. I had some poorly written plugins that hardcoded wp-admin in some places that wouldn't load when trying to block or redirect wp-admin. I'm sure there is a way to do this with the htaccess, but I haven't successfully figured it out. Also, this hasn't been tested on multisite or anything like that ever.

Update: Alternative Approach

This is pretty similar, but for some reason my above answer didn't work on every host I tried.

Add to .htaccess

RewriteRule ^admin/(.*) wp-admin/$1?%{QUERY_STRING} [L]

Create a file in mu-plugins folder called new-admin.php and add this there:

<?php
define('WP_ADMIN_DIR', 'admin');
defined('SITECOOKIEPATH') || define('SITECOOKIEPATH', preg_replace('|https?://[^/]+|i', '', get_option('siteurl') . '/' ) );
define( 'ADMIN_COOKIE_PATH', SITECOOKIEPATH . WP_ADMIN_DIR);

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);
}

Note: This approach seemed to work better on some hosts, but still had the issue of not redirecting wp-admin links to the new admin url. Here's an approach I tried below. While this below doesn't work I think it's on the right track. I'm not totally sure what hook to use. htaccess might be a better alternative but I kept getting redirect loops when I tried that way.

add_action('init', 'redirect_wp_admin_url_to_404');
function redirect_wp_admin_url_to_404(){
  $redirect_to = $_SERVER['REQUEST_URI'];
  if(count($_REQUEST)> 0 && array_key_exists('redirect_to', $_REQUEST)){
    $redirect_to = $_REQUEST['redirect_to'];
    $check_wp_admin = stristr($redirect_to, 'wp-admin');
    if($check_wp_admin){
      wp_safe_redirect( '404.php' );
    }
  }
}

Fix Your WordPress

Fixing it since back then..