Change the WordPress Login URL

Secure your WordPress site by customizing the default wp-login.php path.

Mon May 16 2022
— words · — minutes

Overview

Secure your WordPress site by customizing the default wp-login.php path.

Originally published on Jang Keyte's Blog.

This article covers Change the WordPress Login URL — practical notes from real-world web development experience.

Code Examples


function redirect_page() {
    if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' ||
            $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
            $protocol = 'https://';
        }
        else {
            $protocol = 'http://';
        }
    $currenturl = $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $currenturl_relative = wp_make_link_relative($currenturl);

    switch ($currenturl_relative) {
        case '/topcomsg':
            setcookie("jk-shield", "jangkeyte");
            $urlto = home_url('/wp-login.php');
            break;

        default:
            return;
    }

    if ($currenturl != $urlto)
        exit( wp_redirect( $urlto ) );
}
add_action( 'template_redirect', 'redirect_page' );

/**
 * Check to see if the current page is the login/register page.
 *
 * @return bool
 */
if ( ! function_exists( 'is_login_page' ) ) {
  function is_login_page() {
    return in_array(
      $GLOBALS['pagenow'],
      array( 'wp-login.php', 'wp-register.php' ),
      true
    );
  }
}
if ( is_login_page() ) {
    if ( !isset( $_COOKIE["jk-shield"]) ) {
        header( 'Location: ' . $_SERVER['HTTP_HOST'] );
    }
}

Read More

For the full Vietnamese version, switch language using the VI | EN toggle above, or visit the original post.


Thanks for reading!

Change the WordPress Login URL

Mon May 16 2022
— words · — minutes