You are on the correct path on thinking about post-edit.php
.
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/post-new.php#L45
See how get_default_post_to_edit
is called to return a new post. The second argument tells it to create a post in the database. The function is defined here:
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/includes/post.php#L394
Note line 422: $post_id = wp_insert_post( array( 'post_title' => __( 'Auto Draft' ), 'post_type' => $post_type, 'post_status' => 'auto-draft' ) );
And where would WordPress be without hooks?
do_action('wp_insert_post', $post_ID, $post);
can be seen in wp_insert_post
‘s definition here:
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/post.php#L2656
Thus, if you hook into is like so:
add_action( 'wp_insert_post', 'wpse_45419_hold_global_post_number', null, 2 );
function wpse_45419_hold_global_post_number( $post_id, $post ) {
if ( $post->post_status != 'auto-draft' ) return; // not interested, thanks
// do stuff with $post_id here...
}
This would be quite an interesting thing to have, of course, and is quite surefire. Alternatively, if you look at how the media button gets it:
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/includes/media.php#L371
It uses get_upload_iframe_src
, defined a little lower, which makes use of the global $post_ID
, which brings us back to post-new.php
where you can see it being defined on line 46:
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/post-new.php#L46
So, generally speaking, any hooks that fire off after line 46 are all yours to hook into and tap the $post_ID
global, including wp_insert_post
, media_buttons
, and dozens of others. Note that, init
and admin_init
come too early up the chain. And make sure the hook is specific enough to fire off only when you need it to. wp_insert_post
is good, but will not have the global post_ID
set yet, since it’s going to be set just after the function returns.
Fascinating, isn’t it?