[wp-trac] [WordPress Trac] #21450: wp_insert_new_post hook

WordPress Trac noreply at wordpress.org
Sun Jul 28 20:40:04 UTC 2013


#21450: wp_insert_new_post hook
------------------------------------+------------------------------
 Reporter:  rndbit                  |       Owner:
     Type:  enhancement             |      Status:  new
 Priority:  normal                  |   Milestone:  Awaiting Review
Component:  Post Types              |     Version:
 Severity:  minor                   |  Resolution:
 Keywords:  has-patch dev-feedback  |
------------------------------------+------------------------------

Comment (by ericmann):

 > 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
 [http://codex.wordpress.org/Post_Status_Transitions 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.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/21450#comment:4>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list