gpt4 book ai didi

php - 只有管​​理员允许访问 wp-admin 和登录?

转载 作者:行者123 更新时间:2023-12-05 04:13:32 32 4
gpt4 key购买 nike

我想这样做只有管理员(角色)允许使用 wp-admin 登录,而不是任何其他角色,其他用途如(编辑,作者)是从前端登录并且它工作正常但我希望只有管理员可以通过 wp-admin 登录。

前端登录我用的是终极成员(member)插件。 Ultimate Plugin link

我还使用了下面的代码来访问 wp-admin 仅为管理员(角色),但它不起作用。

<?php
function restrict_admin(){
//if not administrator, kill WordPress execution and provide a message
if( !current_user_can('administrator') ) {
wp_die( __('You are not allowed to access this part of the site') );
}
}
add_action( 'admin_init', 'restrict_admin', 1 );
?>

最佳答案

谢谢大家的帮助,我正在回答我自己的问题,希望这对其他人也有帮助。

我已引用此链接 https://developer.wordpress.org/reference/functions/wp_authenticate/

我已经用 hook wp_authenticate 解决了这个问题

 add_action( 'wp_authenticate' , 'check_custom_authentication' );
function check_custom_authentication ( $username ) {

$username;
$user = new WP_User($username);
$user_role_member=$user->roles[0];



if($user_role_member == 'author' || $user_role_member == 'editor'){
session_destroy();
wp_redirect( home_url() );
exit;
}

}

关于php - 只有管​​理员允许访问 wp-admin 和登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37269294/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com