#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: |
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:
- Get executed when post is updated
- 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)
Change History (17)
#2
@
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?
#3
@
12 years ago
- Component changed from Plugins to Post Types
- Type changed from feature request to enhancement
Related: #15230
#4
@
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.
#5
@
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
@
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.
#7
@
11 years ago
Bingo IMO, simplifies everything and give plugins already hooking into those two hooks the ability to distinguish easily.
#8
@
11 years ago
- Owner set to nacin
- Resolution set to fixed
- Status changed from new to closed
In 24823:
#9
@
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
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