The post_action_{$action} WordPress PHP action fires for a given custom post action request. The dynamic portion of the hook name, $action
, refers to the custom post action.
Usage
add_action( 'post_action_my_custom_action', 'my_custom_action_function', 10, 1 ); function my_custom_action_function( $post_id ) { // your custom code here return; }
Parameters
$post_id
(int) – Post ID sent with the request.
More information
See WordPress Developer Resources: post_action_{$action}
Examples
Change post title
This example changes the post title when the custom action is triggered.
add_action( 'post_action_change_title', 'change_post_title', 10, 1 ); function change_post_title( $post_id ) { $new_title = 'New Title'; $post_data = array( 'ID' => $post_id, 'post_title' => $new_title ); wp_update_post( $post_data ); }
Add a custom post meta
This example adds a custom post meta when the custom action is triggered.
add_action( 'post_action_add_meta', 'add_custom_post_meta', 10, 1 ); function add_custom_post_meta( $post_id ) { $meta_key = 'custom_meta_key'; $meta_value = 'custom_meta_value'; update_post_meta( $post_id, $meta_key, $meta_value ); }
Change post status
This example changes the post status to ‘draft’ when the custom action is triggered.
add_action( 'post_action_set_to_draft', 'set_post_to_draft', 10, 1 ); function set_post_to_draft( $post_id ) { $post_data = array( 'ID' => $post_id, 'post_status' => 'draft' ); wp_update_post( $post_data ); }
Increment post view count
This example increments the view count of a post when the custom action is triggered.
add_action( 'post_action_increment_view_count', 'increment_post_view_count', 10, 1 ); function increment_post_view_count( $post_id ) { $meta_key = 'view_count'; $view_count = get_post_meta( $post_id, $meta_key, true ); $view_count++; update_post_meta( $post_id, $meta_key, $view_count ); }
Send an email on post update
This example sends an email to the admin when the custom action is triggered.
add_action( 'post_action_send_email', 'send_email_on_post_update', 10, 1 ); function send_email_on_post_update( $post_id ) { $post = get_post( $post_id ); $to = get_option( 'admin_email' ); $subject = 'Post Updated: ' . $post->post_title; $message = 'The post with ID ' . $post_id . ' has been updated.'; wp_mail( $to, $subject, $message ); }