The admin_page_access_denied WordPress PHP action fires when access to an admin page is denied.
Usage
add_action('admin_page_access_denied', 'your_function_name'); function your_function_name() { // your custom code here }
Parameters
There are no parameters for this action.
More information
See WordPress Developer Resources: admin_page_access_denied
Examples
Log Access Denied Events
Log access denied events for later review.
add_action('admin_page_access_denied', 'log_access_denied'); function log_access_denied() { // Get the current user $user = wp_get_current_user(); // Log the event error_log("Access denied for user: {$user->user_login} at " . date('Y-m-d H:i:s')); }
Display Custom Message
Show a custom message when access is denied.
add_action('admin_page_access_denied', 'display_custom_message'); function display_custom_message() { wp_die('Sorry, you do not have the necessary permissions to access this page.'); }
Redirect to Dashboard
Redirect users to the dashboard when access is denied.
add_action('admin_page_access_denied', 'redirect_to_dashboard'); function redirect_to_dashboard() { wp_safe_redirect(admin_url()); exit; }
Notify Admin by Email
Send an email to the admin when access is denied.
add_action('admin_page_access_denied', 'notify_admin_by_email'); function notify_admin_by_email() { $user = wp_get_current_user(); $admin_email = get_option('admin_email'); $subject = 'Access Denied Notification'; $message = "User {$user->user_login} tried to access a restricted admin page at " . date('Y-m-d H:i:s'); wp_mail($admin_email, $subject, $message); }
Add an Activity Log Entry
Create an entry in an activity log when access is denied.
add_action('admin_page_access_denied', 'log_activity'); function log_activity() { // Assuming you have a custom activity logging function $user = wp_get_current_user(); $message = "User {$user->user_login} tried to access a restricted admin page"; your_custom_activity_log_function($message); }