WordPress.org

Make WordPress Core

Opened 11 months ago

Last modified 3 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 9 months ago.
Add new_post action when !$update

Download all attachments as: .zip

Change History (4)

comment:1 SergeyBiryukov11 months ago

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

Last edited 11 months ago by SergeyBiryukov (previous) (diff)

comment:2 sc0ttkclark9 months ago

  • 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?

sc0ttkclark9 months ago

Add new_post action when !$update

comment:3 SergeyBiryukov3 months ago

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

Related: #15230

Note: See TracTickets for help on using tickets.