钩子文档

admin_page_access_denied

💡 云策文档标注

概述

admin_page_access_denied 是一个 WordPress 动作钩子,当用户无权访问管理页面时触发。它通常在 user_can_access_admin_page 函数返回 false 后、wp_die 执行前被调用。

关键要点

  • admin_page_access_denied 钩子用于处理用户权限不足时的访问拒绝情况
  • 触发时机:在 wp_die 执行之前,基于 user_can_access_admin_page 返回 false
  • 常见用途:重定向用户或自定义错误消息

代码示例

function mwb_permission_error() {
    wp_redirect( site_url() );
    exit;
}
add_action( 'admin_page_access_denied', 'mwb_permission_error');

注意事项

  • 该钩子自 WordPress 2.5.0 版本引入
  • 源调用为 do_action( 'admin_page_access_denied' )

📄 原文内容

Fires when access to an admin page is denied.

More Information

admin_page_access_denied is an action hook triggered when an user does not have permission to access a page in the control panel. This is most likely due to not having the required capability to access the page.

The action hook is fired before wp_die is executed, and is a result of the function user_can_access_admin_page returning false.

Basic Examples

function mwb_permission_error() {

wp_redirect( site_url() );
exit;

}

add_action( 'admin_page_access_denied', 'mwb_permission_error');

Source

do_action( 'admin_page_access_denied' );

Changelog

Version Description
2.5.0 Introduced.