wordpress - 如何从前端禁用管理员登录?
问题描述
我正在尝试从我的 wordPress 网站的前端禁用管理员登录,但我的后端登录也被禁用了两个登录显示管理员无法在此处登录
<?php
add_filter( 'authenticate', 'wp_admin_prevent_authentication', 30, 3 );
function wp_admin_prevent_authentication( $user, $username, $password ) {
if ( $user instanceof WP_User && is_page( 'my-account' ) ) {
if ( array_intersect( (array) $user->roles, [ 'administrator' ] ) ) {
return new WP_Error( 'admin-error', 'Admins cannot login from here.' );
};
};
return $user;
};
解决方案
不确定您是否可以is_page()
在内部使用,authenticate
但您可以使用$_SERVER['REQUEST_URI']
. 检查下面的代码。
function wp_admin_prevent_authentication( $user, $username, $password ) {
$url = explode( '/', rtrim( $_SERVER['REQUEST_URI'], '/') );
// If in My account dashboard page
if( $user instanceof WP_User && end( $url ) == 'my-account' ){
if ( array_intersect( (array) $user->roles, [ 'administrator' ] ) ) {
return new WP_Error( 'admin-error', 'Admins cannot login from here.' );
}
}
return $user;
}
add_filter( 'authenticate', 'wp_admin_prevent_authentication', 30, 3 );
测试和工作。
推荐阅读
- c# - 该进程无法访问该文件,因为它已被另一个进程使用
- javascript - 循环用于元素 ID 的字符串数组
- html - 如何在移动设备中添加字体真棒选择选项?
- angular - 如果标签标题在第二个组件(Angular)中,则不显示标签标题
- javascript - 如何使用 \n
或类似方法在 React 中的变量内格式化文本 - javascript - 当我显示面板时,YUI (YAHOO) 阻止了我的 UI
- excel - 比较两个不同工作表中的两列
- android - 添加线性布局会弄乱预览中的布局(Android Studio)
- python - 使用熊猫图在 groupby 之后进行子图
- python-3.x - 我无法访问我的 Kivy 代码的“根”