The admin_post WordPress PHP action is triggered when an authenticated admin makes a POST request without supplying an action.
Usage
add_action('admin_post', 'your_custom_function'); function your_custom_function() { // your custom code here }
Parameters
No parameters for this action.
More information
See WordPress Developer Resources: admin_post
Examples
Save Custom Settings
In this example, we create a custom function to save settings from a custom admin form.
add_action('admin_post_save_custom_settings', 'save_custom_settings'); function save_custom_settings() { // Check nonce for security check_admin_referer('custom_settings_nonce'); // Save the custom setting update_option('custom_setting', sanitize_text_field($_POST['custom_setting'])); // Redirect back to the settings page wp_redirect(admin_url('options-general.php?page=custom_settings')); exit; }
Add Custom Post Meta
In this example, we create a function to add custom post meta from an admin form.
add_action('admin_post_add_custom_meta', 'add_custom_meta'); function add_custom_meta() { // Check nonce for security check_admin_referer('add_custom_meta_nonce'); // Add custom post meta add_post_meta($_POST['post_id'], 'custom_meta_key', sanitize_text_field($_POST['custom_meta_value'])); // Redirect back to the edit post page wp_redirect(admin_url('post.php?post=' . intval($_POST['post_id']) . '&action=edit')); exit; }
Delete Custom Post Type
In this example, we create a function to delete a custom post type from the admin area.
add_action('admin_post_delete_custom_post', 'delete_custom_post'); function delete_custom_post() { // Check nonce for security check_admin_referer('delete_custom_post_nonce'); // Delete custom post type wp_delete_post($_POST['post_id'], true); // Redirect back to the custom post type listing page wp_redirect(admin_url('edit.php?post_type=custom_post_type')); exit; }
Add Custom Taxonomy Term
In this example, we create a function to add a custom taxonomy term from the admin area.
add_action('admin_post_add_custom_taxonomy_term', 'add_custom_taxonomy_term'); function add_custom_taxonomy_term() { // Check nonce for security check_admin_referer('add_custom_taxonomy_term_nonce'); // Add custom taxonomy term wp_insert_term(sanitize_text_field($_POST['term_name']), 'custom_taxonomy'); // Redirect back to the taxonomy management page wp_redirect(admin_url('edit-tags.php?taxonomy=custom_taxonomy')); exit; }