Using WordPress ‘admin_action_{$action}’ PHP action

The admin_action_{$action} WordPress PHP action fires when an ‘action’ request variable is sent. The dynamic portion of the hook name, $action, refers to the action derived from the GET or POST request.

Usage

add_action('admin_action_{$action}', 'your_custom_function');
function your_custom_function() {
    // your custom code here
}

Parameters

  • $action (string): The action derived from the GET or POST request.

More information

See WordPress Developer Resources: admin_action_{$action}

Examples

Redirect users after a custom action

This example redirects users to a custom page after performing a custom action.

add_action('admin_action_your_custom_action', 'redirect_after_custom_action');
function redirect_after_custom_action() {
    // Redirect to custom page
    wp_redirect(admin_url('edit.php?page=your_custom_page'));
    exit;
}

Save custom data on action

This example saves custom data when a custom action is performed.

add_action('admin_action_save_custom_data', 'save_custom_data_function');
function save_custom_data_function() {
    // Save custom data to the database
    update_option('your_custom_option', 'your_custom_value');
}

Delete a custom post type

This example deletes a custom post type when a custom action is performed.

add_action('admin_action_delete_custom_post_type', 'delete_custom_post_type_function');
function delete_custom_post_type_function() {
    // Get the custom post ID
    $post_id = intval($_GET['post_id']);

    // Delete the custom post
    wp_delete_post($post_id, true);
}

Send email on custom action

This example sends an email when a custom action is performed.

add_action('admin_action_send_custom_email', 'send_custom_email_function');
function send_custom_email_function() {
    // Send an email to the specified email address
    wp_mail('[email protected]', 'Custom Email Subject', 'Custom Email Message');
}

Update user meta on custom action

This example updates user meta when a custom action is performed.

add_action('admin_action_update_user_meta', 'update_user_meta_function');
function update_user_meta_function() {
    // Get the user ID
    $user_id = intval($_GET['user_id']);

    // Update user meta
    update_user_meta($user_id, 'your_custom_meta_key', 'your_custom_meta_value');
}