Make WordPress Core

Opened 3 years ago

Closed 21 months ago

Last modified 20 months ago

#21450 closed enhancement (fixed)

Fire an action when posts are first created

Reported by: rndbit Owned by: nacin
Milestone: 3.7 Priority: normal
Severity: minor Version:
Component: Posts, Post Types Keywords: has-patch commit
Focuses: Cc:


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 (3)

21450.patch (572 bytes) - added by sc0ttkclark 3 years ago.
Add new_post action when !$update
21450.1.diff (459 bytes) - added by ericmann 21 months ago.
Refreshed patch
21450.2.diff (507 bytes) - added by ericmann 21 months ago.
Alternatively, pass the value of $update back through existing hooks.

Download all attachments as: .zip

Change History (17)

comment:1 @SergeyBiryukov3 years 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':

Last edited 3 years ago by SergeyBiryukov (previous) (diff)

comment:2 @sc0ttkclark3 years 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?

@sc0ttkclark3 years ago

Add new_post action when !$update

comment:3 @SergeyBiryukov2 years ago

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

Related: #15230

comment:4 @ericmann21 months ago

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

This is mostly by design.

In WordPress, posts are only ever created when you click "Add New," and they're created immediately. These blank posts are marked with a post status of 'Auto Draft.' In a normal workflow, this could happen 5, 10, or 20 times for every real post you actually create.

Trying to hook in to wp_insert_post to detect when a post is "created" isn't the right approach. Instead, you'd want to hook in to wp_transition_post_status and its family of related hooks and check when the status transitions from 'auto_draft' to pretty much anything else. For example:

function do_something_awesome( $post ) {
    // Do your magic here when the post is "created"
add_action( 'new_to_publish', 'do_something_awesome' );
add_action( 'new_to_draft', 'do_something_awesome' );
add_action( 'new_to_future', 'do_something_awesome' );
add_action( 'auto-draft_to_publish', 'do_something_awesome' );
add_action( 'auto-draft_to_draft', 'do_something_awesome' );
add_action( 'auto-draft_to_future', 'do_something_awesome' );

The code above will fire whenever a "new" or "Auto Draft" post is saved, published, or scheduled. Considering a post has to be created in the database to give you an ID to work with, this is the absolute closest you can get to actually hooking in at creation time.

Version 0, edited 21 months ago by ericmann (next)

@ericmann21 months ago

Refreshed patch

comment:5 @ericmann21 months ago

  • Keywords commit added; dev-feedback removed
  • Summary changed from wp_insert_new_post hook to Fire an action when posts are first created

comment:6 @sc0ttkclark21 months ago

@ericmann That does make sense, and works in most cases, but for plugins to hook into the origination of the Post, it won't actually exist until it transitions beyond new/auto-draft because we wouldn't be able to accurately tell if the post was created just then.

I'll settle with whatever outcome of this ticket, but having access to this hook would be helpful.

@ericmann21 months ago

Alternatively, pass the value of $update back through existing hooks.

comment:7 @sc0ttkclark21 months ago

Bingo IMO, simplifies everything and give plugins already hooking into those two hooks the ability to distinguish easily.

comment:8 @nacin21 months ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In 24823:

Pass $update to the save_post and wp_insert_post hooks in wp_insert_post(). props ericmann, fixes #21450.

comment:9 @sc0ttkclark21 months ago

Thanks for your efforts, all! I've already implemented the new $update var available in the action integration within Pods :)


comment:10 @nacin21 months ago

Follow-up: wp_publish_post() also must be updated. #24957

comment:11 @nacin21 months ago

In 24977:

Pass $update = true to the save_post and wp_insert_post hooks in wp_publish_post().

see #21450, [24823].

props wpsmith.
fixes #24957.

comment:12 @alex-ye21 months ago

Related: #23759

Great! that this resolved now, I was need it before 5 months :(

comment:13 @ocean9021 months ago

  • Milestone changed from Awaiting Review to 3.7

comment:14 @SergeyBiryukov20 months ago

#23759 was marked as a duplicate.

Note: See TracTickets for help on using tickets.