Make WordPress Core

Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#21450 closed enhancement (fixed)

Fire an action when posts are first created

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

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

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

Download all attachments as: .zip

Change History (17)

#1 @SergeyBiryukov
12 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':
http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/post.php#L2997
http://codex.wordpress.org/Post_Status_Transitions

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

#2 @sc0ttkclark
12 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?

@sc0ttkclark
12 years ago

Add new_post action when !$update

#3 @SergeyBiryukov
12 years ago

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

Related: #15230

#4 @ericmann
11 years 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.

That said, I'll refresh the patch anyway in case you really need to use the hook.

Last edited 11 years ago by ericmann (previous) (diff)

@ericmann
11 years ago

Refreshed patch

#5 @ericmann
11 years ago

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

#6 @sc0ttkclark
11 years 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.

@ericmann
11 years ago

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

#7 @sc0ttkclark
11 years ago

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

#8 @nacin
11 years 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.

#9 @sc0ttkclark
11 years ago

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

https://github.com/pods-framework/pods/commit/b55c8b3b936e11f5a6926ba8fbb0672c82c76346

#10 @nacin
11 years ago

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

#11 @nacin
11 years 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.

#12 @alex-ye
11 years ago

Related: #23759

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

#13 @ocean90
11 years ago

  • Milestone changed from Awaiting Review to 3.7

#14 @SergeyBiryukov
11 years ago

#23759 was marked as a duplicate.

Note: See TracTickets for help on using tickets.