Opened 10 months ago

Last modified 2 months ago

#21450 new enhancement

wp_insert_new_post hook

Reported by: rndbit Owned by:
Priority: normal Milestone: Awaiting Review
Component: Post Types Version:
Severity: minor Keywords: has-patch dev-feedback
Cc: lol@…

Description

Situation is this in wp_insert_post:

	if ( $update ) {
		do_action('edit_post', $post_ID, $post);
		$post_after = get_post($post_ID);
		do_action( 'post_updated', $post_ID, $post_after, $post_before);
	}

	do_action('save_post', $post_ID, $post);
	do_action('wp_insert_post', $post_ID, $post);

As you can see we have hooks that:

  1. Get executed when post is updated
  2. Get executed when post is updated or created

There is no hook that would be exectued only when post is created.
wp_insert_post was most logical candidate for the task, however i suspect changing it might break some plugins relying on it therefore i propose new action wp_insert_new_post which should be executed only when new post is created, but never when post is updated.

Like so:

	if ( $update ) {
		do_action('edit_post', $post_ID, $post);
		$post_after = get_post($post_ID);
		do_action( 'post_updated', $post_ID, $post_after, $post_before);
	}
+	else
+		do_action('wp_insert_new_post', $post_ID, $post);

	do_action('save_post', $post_ID, $post);
	do_action('wp_insert_post', $post_ID, $post);

P.S. I marked ticket as having patch even tho there is just updated code snippet above. Please excuse me :)

Attachments (1)

21450.patch (572 bytes) - added by sc0ttkclark 8 months ago.
Add new_post action when !$update

Download all attachments as: .zip

Change History (4)

There is no hook that would be exectued only when post is created

You could use transition_post_status action and check if $old_status is new:
http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/post.php#L2997
http://codex.wordpress.org/Post_Status_Transitions

Version 0, edited 10 months ago by SergeyBiryukov (next)
  • Cc lol@… added
  • Keywords dev-feedback added

I'd prefer to see something like new_post here for the action name. But beyond that, yeah, this is a sticky point for new devs trying to attach an action only for new posts. Anyone actually against adding this sort of action on that if/else statement?

Add new_post action when !$update

  • Component changed from Plugins to Post Types
  • Type changed from feature request to enhancement

Related: #15230

Note: See TracTickets for help on using tickets.