Wp添加网址保护(包括添加分类url网址保护)
Published
2023-01-06
浏览次数 : 147
add_action('template_redirect', 'ha_adds_password_on_url',999);
function ha_adds_password_on_url() {
global $user_ID;
global $wpdb;
$username = ha_get_option('valide_user');
$password = ha_get_option('valide_pass');
//获取所有页面来选择
$current_url = parse_url( home_url( add_query_arg( array() ) ) );
//specific url
$allowed_path = ha_get_option('allowered_path');
foreach($allowed_path as $path) {
//每个路径都执行一遍
if($current_url['path'] == '/'.$path.'/' && $user_ID != 1) {
//这个username和password要通过wp的用户验证
//$_SERVER['PHP_AUTH_USER']&&$_SERVER['PHP_AUTH_PW']
if (
!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
($_SERVER['PHP_AUTH_USER'] != $username) || ($_SERVER['PHP_AUTH_PW'] != $password)
) {
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="My Website"');
exit('Sorry, you must enter a valid user name and password to access this page.');
}
}
}
// Page content goes here
}
- 标签1
- 标签1
- 标签1