The admin_post_{$action} WordPress PHP action fires on an authenticated admin post request for the given action. The dynamic portion of the hook name, $action, refers to the given request action.
Usage
add_action('admin_post_your_action', 'your_function_name');
function your_function_name() {
// Your custom code here
}
Parameters
- $action (string): The action name that identifies the authenticated admin post request.
More information
See WordPress Developer Resources: admin_post_{$action}
Examples
Redirecting after a custom form submission
When you create a custom form in the WordPress admin area, you can use this action to handle the form submission and then redirect to a specific page.
add_action('admin_post_process_form', 'process_custom_form');
function process_custom_form() {
// Process the form data and save it to the database
// Redirect to the admin page after processing
wp_redirect(admin_url('admin.php?page=your_page_slug'));
exit;
}
Handling AJAX requests in the admin area
Use the action to handle AJAX requests submitted by admin users.
add_action('admin_post_my_ajax_action', 'handle_ajax_request');
function handle_ajax_request() {
// Process the AJAX request and return the result
wp_die(); // Required to terminate AJAX request
}
Customizing the admin bar
You can use this action to modify the admin bar based on the request action.
add_action('admin_post_customize_admin_bar', 'customize_admin_bar');
function customize_admin_bar() {
// Add or remove items from the admin bar
// Redirect back to the admin dashboard
wp_redirect(admin_url());
exit;
}
Deleting a custom post type item
Use the action to delete a custom post type item and then redirect the user to a specific page.
add_action('admin_post_delete_custom_post_type_item', 'delete_custom_post_type_item');
function delete_custom_post_type_item() {
// Delete the custom post type item from the database
// Redirect to the custom post type listing page
wp_redirect(admin_url('edit.php?post_type=your_custom_post_type'));
exit;
}
Performing a custom bulk action
Perform a custom bulk action on a list of items in the admin area.
add_action('admin_post_custom_bulk_action', 'perform_custom_bulk_action');
function perform_custom_bulk_action() {
// Process the bulk action on selected items
// Redirect back to the previous admin page
wp_redirect(wp_get_referer());
exit;
}