只允许管理员访问WordPress后台

为了提高WordPress后台安全性,可以设置为只允许管理员访问WordPress后台,要实现这个功能,只需将下面的代码添加到当前主题的 functions.php:

/**
 * 重置非管理员用户到首页
 * https://www.wpdaxue.com/only-allow-administrators-to-access-wordpress-admin-area.html
 */
function redirect_non_admin_users() {
	if ( ! current_user_can( 'manage_options' ) && '/wp-admin/admin-ajax.php' != $_SERVER['PHP_SELF'] ) {
		wp_redirect( home_url() );
		exit;
	}
}
add_action( 'admin_init', 'redirect_non_admin_users' );

通过 admin_init 钩子挂载链接重置函数,将非管理员用户重置到网站首页,如果你需要重置到其他页面,可以修改 wp_redirect() 的参数;如果你要修改用户角色,可以修改  current_user_can( ‘manage_options’ ),至于如何判断用户角色,请参考 WordPress Roles and Capabilities

参考资料:http://bavotasan.com/2013/only-allow-administrators-to-access-the-wordpress-admin-area/

来源:

https://www.wpdaxue.com/only-allow-administrators-to-access-wordpress-admin-area.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_33187.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?