Opened 11 months ago
Last modified 3 months ago
#21450 new enhancement
wp_insert_new_post hook
| Reported by: |
|
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:
- 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 (1)
Change History (4)
comment:1
SergeyBiryukov
— 11 months ago
comment:2
sc0ttkclark
— 9 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?
comment:3
SergeyBiryukov
— 3 months ago
- Component changed from Plugins to Post Types
- Type changed from feature request to enhancement
Related: #15230
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