我试图阻止除管理员之外的用户访问注册页面,如果他们已经登录,则重置密码页面。我有以下几点:
function redirect_loggedin_users() {
// Maybe use is_user_logged_in() instead?
if (!current_user_can(\'manage_options\') && is_page(array(2090, 2092))) {
wp_redirect(home_url());
exit();
}
}
add_action(\'init\', \'redirect_loggedin_users\');
我测试了它,但它不起作用。有人能帮我吗?谢谢
最合适的回答,由SO网友:Chip Bennett 整理而成
一个问题是,你太早就上钩了。参考Hooks API Action Reference. 模板条件,例如is_page()
仅在设置和分析查询后可用。通常可以安全依赖查询条件的最早操作是pre_get_posts
. 你在勾搭init
, 点火时间要早得多:
muplugins_loaded
加载必须使用的插件后registered_taxonomy
对于类别、post\\u标记等registered_post_type
用于帖子、页面等plugins_loaded
加载活动插件和可插入功能后sanitize_comment_cookies
setup_theme
load_textdomain
对于默认域after_setup_theme
通常用于初始化主题设置/选项auth_cookie_malformed
auth_cookie_valid
set_current_user
init
Typically used by plugins to initialize. The current user is already authenticated by this time.widgets_init
用于注册侧栏。这是在“init”触发的,优先级为1register_sidebar
对于每个侧边栏和页脚区域wp_register_sidebar_widget
对于每个小部件wp_default_scripts
(参考阵列)wp_default_styles
(参考阵列)admin_bar_init
add_admin_bar_menus
wp_loaded
WordPress完全加载后parse_request
Allows manipulation of HTTP request handling (ref array)send_headers
允许自定义HTTP标头(ref数组)parse_query
(参考阵列)pre_get_posts
Exposes the query-variables object before a query is executed. (ref array)posts_selection
wp
设置WP对象(参考数组)后template_redirect
get_header
wp_enqueue_scripts
考虑到你想做的事情的性质,我建议你template_redirect
.