Hooks inside wp_insert_post()

I felt that as WordPress developer we need to know about various hooks which is fired when post is created and updated. wp_insert_post and wp_update_post functions are called when post is create/update.

So, here I listed various hooks which is inside wp_insert_post function and this function defined in wp/includes/post.php.

I have explored wp/includes/post.php file for hooks and write down here.

Notes: Same functions, hooks and process for post, page, attachment and custom post type.

 

Excecution flow of post update

1.  wp_insert_post_empty_content filter whether the post should be considered “empty”.

/**
* The post is considered "empty" if both:
* 1. The post type supports the title, editor, and excerpt fields
* 2. The title, editor, and excerpt fields are all empty
*
* Returning a truthy value to the filter will effectively short-circuit
* the new post being inserted, returning 0. If $wp_error is true, a WP_Error
* will be returned instead.
*
* @since 3.3.0
*
* @param bool  $maybe_empty Whether the post should be considered "empty".
* @param array $postarr     Array of post data.
*/
apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr );

2.  wp_insert_post_parent filter the post parent — used to check for and prevent hierarchy loops.

/*
* @since 3.1.0
*
* @param int   $post_parent Post parent ID.
* @param int   $post_ID     Post ID.
* @param array $new_postarr Array of parsed post data.
* @param array $postarr     Array of sanitized, but otherwise unmodified post data.
*/
$post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );

3.  wp_insert_attachment_data filter attachment post data before it is updated in or added to the database. wp_insert_post_data filter slashed post data just before it is inserted into the database.

if ( 'attachment' === $post_type ) {
/**
* @since 3.9.0
*
* @param array $data    An array of sanitized attachment post data.
* @param array $postarr An array of unsanitized attachment post data.
*/
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
} else {
/**
* @since 2.7.0
*
* @param array $data    An array of slashed post data.
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
*/
$data = apply_filters( 'wp_insert_post_data', $data, $postarr );
}

4.  pre_post_update action fires immediately before an existing post is updated in the database.

/**
* @since 2.5.0
*
* @param int   $post_ID Post ID.
* @param array $data    Array of unslashed post data.
*/
do_action( 'pre_post_update', $post_ID, $data );

5.  transition_post_status action fires when a post is transitioned from one status to another.

/**
* @since 2.3.0
*
* @param string  $new_status New post status.
* @param string  $old_status Old post status.
* @param WP_Post $post       Post object.
*/
do_action( 'transition_post_status', $new_status, $old_status, $post );

6.  {$old_status}_to_{$new_status} action fires when a post is transitioned from one status to another.

/**
* The dynamic portions of the hook name, `$new_status` and `$old status`,
* refer to the old and new post statuses, respectively.
*
* @since 2.3.0
*
* @param WP_Post $post Post object.
*/
do_action( "{$old_status}_to_{$new_status}", $post );

7.  {$new_status}_{$post->post_type} action fires when a post is transitioned from one status to another.

/**
* The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
* refer to the new post status and post type, respectively.
*
* Please note: When this action is hooked using a particular post status (like
* 'publish', as `publish_{$post->post_type}`), it will fire both when a post is
* first transitioned to that status from something else, as well as upon
* subsequent post updates (old and new status are both the same).
*
* Therefore, if you are looking to only fire a callback when a post is first
* transitioned to a status, use the {@see 'transition_post_status'} hook instead.
*
* @since 2.3.0
*
* @param int     $post_id Post ID.
* @param WP_Post $post    Post object.
*/
do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );

8.  edit_attachment action fires once an existing attachment has been updated. This action fires if post type is attachment.

/**
* @since 2.0.0
*
* @param int $post_ID Attachment ID.
*/
do_action( 'edit_attachment', $post_ID );

9.  add_attachment is action fires once an attachment has been added. This action fires if post type is attachment.

/**
* @since 2.0.0
*
* @param int $post_ID Attachment ID.
*/
do_action( 'add_attachment', $post_ID );

10.  edit_post action fires once an existing post has been updated.

/**
* @since 1.2.0
*
* @param int     $post_ID Post ID.
* @param WP_Post $post    Post object.
*/
do_action( 'edit_post', $post_ID, $post );

11.  post_updated action fires once an existing post has been updated.

/**
* @since 3.0.0
*
* @param int     $post_ID      Post ID.
* @param WP_Post $post_after   Post object following the update.
* @param WP_Post $post_before  Post object before the update.
*/
do_action( 'post_updated', $post_ID, $post_after, $post_before);

12.  save_post_{$post->post_type} action fires once a post has been saved.

/**
* The dynamic portion of the hook name, `$post->post_type`, refers to
* the post type slug.
*
* @since 3.7.0
*
* @param int     $post_ID Post ID.
* @param WP_Post $post    Post object.
* @param bool    $update  Whether this is an existing post being updated or not.
*/
do_action( "save_post_{$post->post_type}", $post_ID, $post, $update );

13.  save_post action fires once a post has been saved.

/**
* @since 1.5.0
*
* @param int     $post_ID Post ID.
* @param WP_Post $post    Post object.
* @param bool    $update  Whether this is an existing post being updated or not.
*/
do_action( 'save_post', $post_ID, $post, $update );

14.  wp_insert_post action fires once a post has been saved.

/**
* @since 2.0.0
*
* @param int     $post_ID Post ID.
* @param WP_Post $post    Post object.
* @param bool    $update  Whether this is an existing post being updated or not.
*/
do_action( 'wp_insert_post', $post_ID, $post, $update );

Leave a Reply

Your email address will not be published. Required fields are marked *