Using WordPress ‘admin_post’ PHP action

The admin_post WordPress PHP action fires on an authenticated admin post request where no action is supplied.

Usage

add_action('admin_post', 'your_custom_function');
function your_custom_function() {
    // Your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: admin_post

Examples

Create a Custom Admin Page Submission

This example processes a form submission from a custom admin page.

add_action('admin_post_my_form_handler', 'my_form_handler');
function my_form_handler() {
    // Process your form data here
}

Custom Logout Redirect

This example redirects users to the homepage after logging out.

add_action('admin_post_logout', 'custom_logout_redirect');
function custom_logout_redirect() {
    wp_redirect(home_url());
    exit();
}

Save Custom Settings

This example saves custom settings from an admin settings page.

add_action('admin_post_save_my_settings', 'save_my_settings');
function save_my_settings() {
    // Save your custom settings here
}

Process Custom Admin Post Action

This example processes a custom admin post action.

add_action('admin_post_my_custom_action', 'my_custom_action_function');
function my_custom_action_function() {
    // Process your custom action here
}

Clear Custom Cache

This example clears a custom cache when an admin post action is triggered.

add_action('admin_post_clear_custom_cache', 'clear_custom_cache');
function clear_custom_cache() {
    // Clear your custom cache here
}