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.
Tạo hàm check đường dẫn đăng nhập và set cookie lưu trữ
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' );
Tạo hàm kiểm tra xem có phải trang wp-login.php hoặc trang wp-register.php
/**
* 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
);
}
}
Khi truy cập vào wp-login.php tiến hành kiểm tra xem cookie đã được set hay chưa, nếu chưa thì trả về trang chủ
if ( is_login_page() ) {
if ( !isset( $_COOKIE["jk-shield"]) ) {
header( 'Location: ' . $_SERVER['HTTP_HOST'] );
}
}
Thanks for reading!