I just realised that in wp-includes/post.php (source), at the end of wp_insert_post(), both the save_post and wp_insert_post actions are called one after the other, with the exact same parameters:

3520  /**
3521   * Fires once a post has been saved.
3522   *
3523   * @since 1.5.0
3524   *
3525   * @param int     $post_ID Post ID.
3526   * @param WP_Post $post    Post object.
3527   * @param bool    $update  Whether this is an existing post being updated or not.
3528   */
3529  do_action( 'save_post', $post_ID, $post, $update );
3530    
3531  /**
3532   * Fires once a post has been saved.
3533   *
3534   * @since 2.0.0
3535   *
3536   * @param int     $post_ID Post ID.
3537   * @param WP_Post $post    Post object.
3538   * @param bool    $update  Whether this is an existing post being updated or not.
3539   */
3540  do_action( 'wp_insert_post', $post_ID, $post, $update );

Nothing happens between them, so there appears to be no difference between using one or the other.

The same redundancy is repeated a little further down in wp_publish_post() (source), and the oldest tracked version of the file also has the same two actions (thanks toscho for pointing these out).

Am I missing something? Why are they both there, and if I am choosing an action to use, is there a reason to choose one over the other?

1
1

wp_insert_post was introduced in changeset 2887, and was to fix bug #1681. I couldn’t find the save_post hook’s original provenance, but it was most recently added to core in changeset 3291, related to ticket #2063. Evidently it had existed in 1.5.2 (although version control does not support this theory) and needed to be added back for back-compat.

So apparently, save_post was added in 1.5.0, then somehow removed in the 2.0 development lifecycle, then wp_insert_post was later added in the 2.0 development lifecycle, and finally, save_post was added back even later in the 2.0 lifecycle so as not to break back-compat.

And rather than being the deprecated hook you’d expect such a hook to be if that’s true, it became the de facto default hook that developers use.

Leave a Reply

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