https://wplabo.idea-hack.com/blog/code-catalog/19589/
【Complete Guide】 19 instant codes for WordPress login URL change and page layout

【Complete Guide】 19 instant codes for WordPress login URL change and page layout

The WordPress login page is responsive compliant and very sophisticated, but customization is very difficult. So let ‘s introduce the instant code that can be implemented immediately, including URL changes.

The code introduced here is also used on the login page of this site. Of course I can not show you the login page for security reasons.

In the past I had done it using a plugin, but as the load on the site has increased, now I have written the code directly.

First of all


Write the code to be introduced in functions.php of your theme, or create and implement the original plugin.

Change the URL of the login page

The default login pages are as follows.

  • https://site.com/wp-login.php
  • https://site.com/wp-admin.php

If you want to change the URL of the login page, you need to do the following

This is very effective as one of security measures, so please do it.

  1. Create a dedicated PHP file
  2. Add URL change function

Well, let’s see.

Create a dedicated PHP file

Let’s create a file with the URL you want to use on the login page. In this case, create “custom-login.php” so that “https://site.com/custom-login.php” becomes the login page.

Let’s write the following code in the file.

php
<?php
define( 'LOGIN_CHANGE', sha1( 'security' ) );
require_once './wp-login.php';
?>

The ‘security’ part of “sha1 (‘security’)” on the second line is a password for encryption, so be sure to change it to your preferred value.

Add URL change function

Prepare functions.php or plugins for your theme.

Change WordPress administration screen login URL

php
define( 'LOGIN_CHANGE_PAGE', 'wp-test-login.php' );
add_action( 'login_init', 'login_change_init' );
add_filter( 'site_url', 'login_change_site_url', 10, 4 );
add_filter( 'wp_redirect', 'login_change_wp_redirect', 10, 2 );

Login URL other than specified is set to 403 error

php
if ( ! function_exists( 'login_change_init' ) ) {
  function login_change_init() {
    if ( !defined( 'LOGIN_CHANGE' ) || sha1( 'keyword' ) != LOGIN_CHANGE ) {
      status_header( 403 );
      exit;
    }
  }
}

Replace wp-login.php for login URLs that are already logged in or newly created

php
if ( ! function_exists( 'login_change_site_url' ) ) {
  function login_change_site_url( $url, $path, $orig_scheme, $blog_id ) {
    if ( $path == 'wp-login.php' &&
      ( is_user_logged_in() || strpos( $_SERVER['REQUEST_URI'], LOGIN_CHANGE_PAGE ) !== false ) )
      $url = str_replace( 'wp-login.php', LOGIN_CHANGE_PAGE, $url );
    return $url;
  }
}

Setting the redirect destination when logging out

php
if ( ! function_exists( 'login_change_wp_redirect' ) ) {
  function login_change_wp_redirect( $location, $status ) {
    if ( strpos( $_SERVER['REQUEST_URI'], LOGIN_CHANGE_PAGE ) !== false )
      $location = str_replace( 'wp-login.php', LOGIN_CHANGE_PAGE, $location );
    return $location;
  }
}

By doing this you can change the URL of the login page.

Customize the layout of the login page

If you liked this article
please click on the "Like!".

Follow on Twetter !

Related Posts