首先,如果你想阻止访问wp-admin
, 为什么要挂接在每次加载页面时都会触发的东西上?钩入admin_init
相反
而且,正如@MattSmath所提到的,编辑不是一种能力。edit_posts
是而且admin_init
仅在管理页面上激发,因此您可以删除is_admin()
从你的支票中。
修改后的功能:
<?php
add_action(\'admin_init\', \'wpse51831_init\');
function wpse51831_init()
{
if(!current_user_can(\'edit_posts\'))
{
wp_redirect(home_url());
exit();
}
}
奖励:你可能不想展示你的
subscriber
将用户级别设置为WordPress管理栏(将其链接回
wp-admin
). 这将隐藏它:
<?php
add_filter(\'show_admin_bar\', \'wpse51831_hide_admin_bar\');
/*
* hide the admin bar for `subscribers`
*
* @uses current_user_can
* @return boolean
*/
function wpse51831_hide_admin_bar($bool)
{
if(!current_user_can(\'edit_posts\'))
{
$bool = false;
}
return $bool;
}