Change Wordpress Admin URL

后端 未结 6 1950
遇见更好的自我
遇见更好的自我 2020-11-28 11:37

I changed my Wordpress directory structure quite a bit. Here\'s what I have:

define(\'WP_SITEURL\', \'http://\' . $_SERVER[\'SERVER_NAME\'] . \'/wordpress\'         


        
6条回答
  •  天涯浪人
    2020-11-28 12:02

    Finally found a way to do it without a plugin AND WITHOUT MODIFYING WP CORE (all tutorials suggests to do so for some weird reason).

    1- Copy wp-login.php and rename it to new-secret-url.php (on your root directory)

    2- Open new-secret-url.php file and perform a search/replace of wp-login.php to new-secret-url.php

    3- Add the following code to your functions.php:

    /** Hide default login */
    add_action( 'init', 'marounmelhem_hide_login' );
    function marounmelhem_hide_login() {
    
        //Only proceed for guests
        if ( ! is_user_logged_in() ) {
    
            //Getting current page
            $current_url   = str_replace( '/', '', $_SERVER['REQUEST_URI'] );
            $hiddenWpAdmin = 'new-secret-url'; //Change this to your new secret wp-admin url
            $redirectNaTo  = '/';
    
            //Checking if accessing correct login url
            if ( $current_url == $hiddenWpAdmin ) {
                wp_redirect( '/'.$hiddenWpAdmin.'.php' );
                exit;
            }
    
            //Only allow requests to wp-login.php coming from correct login url
            $adminToCheck = [
                'wp-admin',
                'wp-login.php'
            ];
            if (
                in_array( $current_url, $adminToCheck )
                &&
                $_GET['action'] !== "logout"
            ) {
                wp_redirect( $redirectNaTo );
                exit();
            }
        }
    }
    

    4- This only works if you're not using any other frontend login forms, if you do, you can change:

    is_user_logged_in() to possibly !current_user_can( 'subscriber' ) (or the role given in the frontend login logic)

    5- Not sure if ajax calls works with the above, please let me know if you've tried it

提交回复
热议问题