Using WordPress ‘edit_post’ PHP action

The edit_post WordPress PHP action fires once an existing post has been updated.

Usage

add_action('edit_post', 'my_custom_edit_post', 10, 2);

function my_custom_edit_post($post_id, $post) {
    // your custom code here
}

Parameters

  • $post_id (int) – The ID of the post being updated.
  • $post (WP_Post) – The post object for the updated post.

More information

See WordPress Developer Resources: edit_post

Examples

Update post meta on post update

Update a custom post meta field when the post is updated.

add_action('edit_post', 'update_custom_meta_field', 10, 2);

function update_custom_meta_field($post_id, $post) {
    // Update the custom meta field 'my_custom_meta_field'
    update_post_meta($post_id, 'my_custom_meta_field', 'Updated value');
}

Send an email notification on post update

Send an email to the site administrator when a post is updated.

add_action('edit_post', 'send_email_on_post_update', 10, 2);

function send_email_on_post_update($post_id, $post) {
    $admin_email = get_option('admin_email');
    $subject = 'A post has been updated on your site';
    $message = 'The post with ID ' . $post_id . ' has been updated.';

    wp_mail($admin_email, $subject, $message);
}

Log post update

Log post updates to a custom log file.

add_action('edit_post', 'log_post_update', 10, 2);

function log_post_update($post_id, $post) {
    $log_message = 'Post ID ' . $post_id . ' has been updated at ' . current_time('mysql') . PHP_EOL;
    $log_file = fopen('post_update_log.txt', 'a');
    fwrite($log_file, $log_message);
    fclose($log_file);
}

Set post format on update

Automatically set the post format to ‘image’ when a post is updated and has a featured image.

add_action('edit_post', 'set_image_post_format', 10, 2);

function set_image_post_format($post_id, $post) {
    if (has_post_thumbnail($post_id)) {
        set_post_format($post_id, 'image');
    }
}

Add a category to updated posts

Add a specific category to the post when it’s updated.

add_action('edit_post', 'add_category_on_post_update', 10, 2);

function add_category_on_post_update($post_id, $post) {
    $category_id = 5; // Replace this with the desired category ID
    wp_set_post_categories($post_id, array($category_id), true);
}