Using WordPress ‘admin_action_{$_REQUEST[‘action’]}’ PHP action

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

Usage

add_action('admin_action_{$_REQUEST['action']}', 'my_custom_function');

function my_custom_function() {
    // your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: admin_action_{$_REQUEST[‘action’]}

Examples

Custom bulk action for posts

Add a custom bulk action in the post list to change the post format.

// Add custom bulk action to the dropdown
add_filter('bulk_actions-edit-post', 'my_custom_bulk_action');

function my_custom_bulk_action($bulk_actions) {
    $bulk_actions['change_post_format'] = __('Change Post Format', 'text-domain');
    return $bulk_actions;
}

// Handle custom bulk action
add_action('admin_action_change_post_format', 'handle_change_post_format');

function handle_change_post_format() {
    // Check for nonce and permissions
    // your custom code here
}

Custom admin action to clear cache

Create a button to clear cache in the admin area.

// Add custom page
add_action('admin_menu', 'my_cache_clear_page');

function my_cache_clear_page() {
    add_submenu_page('tools.php', 'Clear Cache', 'Clear Cache', 'manage_options', 'clear-cache', 'clear_cache_page_callback');
}

// Output the custom page
function clear_cache_page_callback() {
    echo '<form action="" method="post">';
    echo '<input type="hidden" name="action" value="clear_cache">';
    echo '<input type="submit" value="Clear Cache" class="button button-primary">';
    echo '</form>';
}

// Handle custom action
add_action('admin_action_clear_cache', 'handle_clear_cache');

function handle_clear_cache() {
    // Clear cache
    // your custom code here
}

Custom admin action to export data

Export custom data in a CSV format.

// Add custom page
add_action('admin_menu', 'my_data_export_page');

function my_data_export_page() {
    add_submenu_page('tools.php', 'Export Data', 'Export Data', 'manage_options', 'export-data', 'export_data_page_callback');
}

// Output the custom page
function export_data_page_callback() {
    echo '<form action="" method="post">';
    echo '<input type="hidden" name="action" value="export_data">';
    echo '<input type="submit" value="Export Data" class="button button-primary">';
    echo '</form>';
}

// Handle custom action
add_action('admin_action_export_data', 'handle_export_data');

function handle_export_data() {
    // Export data
    // your custom code here
}

Custom admin action to import data

Import custom data from an uploaded file.

// Add custom page
add_action('admin_menu', 'my_data_import_page');

function my_data_import_page() {
    add_submenu_page('tools.php', 'Import Data', 'Import Data', 'manage_options', 'import-data', 'import_data_page_callback');
}

// Output the custom page
function import_data_page_callback() {
    echo '<form action="" method="post" enctype="multipart/form-data">';
    echo '<input type="hidden" name="action" value="import_data">';
    echo '<input type="file" name="import_file">';
echo '<input type="submit" value="Import Data" class="button button-primary">';
echo '</form>';
}

// Handle custom action
add_action('admin_action_import_data', 'handle_import_data');

function handle_import_data() {
// Import data
// your custom code here
}

Custom admin action to reset settings

Reset custom plugin settings to default values.

// Add custom page
add_action('admin_menu', 'my_settings_reset_page');

function my_settings_reset_page() {
    add_submenu_page('options-general.php', 'Reset Settings', 'Reset Settings', 'manage_options', 'reset-settings', 'reset_settings_page_callback');
}

// Output the custom page
function reset_settings_page_callback() {
    echo '<form action="" method="post">';
    echo '<input type="hidden" name="action" value="reset_settings">';
    echo '<input type="submit" value="Reset Settings" class="button button-primary">';
    echo '</form>';
}

// Handle custom action
add_action('admin_action_reset_settings', 'handle_reset_settings');

function handle_reset_settings() {
    // Reset settings
    // your custom code here
}