How to Change WordPress Admin URL in WordPress Without Plugin

Posted By : Renu Yadav | 31-Dec-2018

In the present world, so many websites are working online. Website security is very important for today website. Many websites hacked by hacker every day. So many data lost. Wordpress website easy to hacked because hacker knows that which file will access to corrupt the wordpress website. Wp-admin panels common in every WordPress website and the wp_ prefix is added on the table name. This so common thing because this thing is provided by the WordPress by default. So for security, change the prefix of your table when you install a fresh WordPress setup. You more security you can also buy a plugin for the scan your website time to time and remove malware file from the website. you can also rename the wp-admin file or hide the wp-admin file from the website to prevent your website from hacking. These are very simple and comman method to prevent your website from hacking. 

Below are the few steps you can do :

1) Change your table prefix.
2) Add some plugin which can scan your website on daily bases and remove malware from the website.
3) Rename your admin panel.

 

Rename your Admin Panel :

For renaming your admin panel some First thing you can do add this code in wp-config file.

define('WP_ADMIN_DIR', 'adminnew');

define('SITECOOKIEPATH', preg_replace('|http?://[^/]+|i', '', get_option('siteurl').'/'));
define( 'ADMIN_COOKIE_PATH',SITECOOKIEPATH.WP_ADMIN_DIR);

After adding the code in wp-config add the below code in the function.php to redirect the admin to new URL of the admin panel.

 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);
}
add_filter('siteurl',  'wpadmin_filter', 10, 3);

For restriction of the admin panel, you can add this code. If anybody accesses the wp-admin it will show the 404 error on the 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'];
    $check_wp_admin = stristr($redirect_to, 'wp-admin');
    
    if($check_wp_admin){
        wp_safe_redirect( '404.php' );
    }
 }
}

After adding the above code in function.php. You can see can't be able to log in your admin panel because your admin panel redirects value is changed. So you can't be able to log in your admin login page Inspect the value check the value of redirect_to input box. You will see that value is changed to new admin URL. By default value is yourwebisteurl/wp-admin. So you have to change this new URL valueto default value. For changing this value don't change the core login file .Use another method for change the value.  I used the script for changes the after redirect to the new admin panel and modify the aboe code and if you accesss the wp-login page it will show 404 error.

add_action('login_form','redirect_wp_admin');

function redirect_wp_admin(){     

     $home_address=home_url();

      $current_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") .'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
   
        $admin_url=$home_address.'/wp-login.php';
      $redirect_to = home_url();   
      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'); 
                    }else{                

                          ?>
                     
                           $(window).on('load', function()
                            {
                                function abc(){                          
                                  var input_redirect_value = $('input[name=redirect_to]').val();
                                 
                                   var match_redirect_value ='<?php echo home_url();?>/internal-control';
                                   var actual_redirect_value='<?php echo home_url();?>/wp-admin';              
                                    if(input_redirect_value==match_redirect_value){                                    
                                        $("input[name=redirect_to]").val(actual_redirect_value);
                                     }
                                   }
                                    abc();
                             });    
                          </script>
                    <?php                   
                  }
                     
        }else{
           if($current_link==$admin_url){  wp_safe_redirect('404.php');   }
         }
  }

 

When you logout you can see the 404 error. You can add the below code after logut to from admin panel . It will redirect to new admin url. For this, you can add this code in function.php

// After logout redirect to new url of admin panel

add_action('wp_logout','auto_redirect_after_logout');
function auto_redirect_after_logout(){
	   $current_link = home_url().'/adminnew';	 
	   wp_redirect( $current_link);
      exit();
}

 

For redierect  to the new url you must add this code in htaccess file . If your website is on nginx server so you can add the bewlo code for your redirect. 

.htaccsee file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^adminnew(.*) wp-admin/$1?%{QUERY_STRING} [L]

</IfModule>

# END WordPress

Nginx configuration

# nginx configurationlocation /internal {  rewrite ^/adminnew(.*) /wp-admin/$1?$query_string break;}

You can see above step. These step are simple . Easy to implement to change admin url . 

 

Thanks 

 

 

 

 

About Author

Author Image
Renu Yadav

Renu is an Associate Consultant -WordPress in Oodles. She likes watching movies and reading books.

Request for Proposal

Name is required

Comment is required

Sending message..