你在正确的道路上思考post-edit.php.
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/post-new.php#L45
查看如何
get_default_post_to_edit 被调用以返回新帖子。第二个参数告诉它在数据库中创建一篇文章。函数定义如下:
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/includes/post.php#L394
注释第422行:
$post_id = wp_insert_post( array( \'post_title\' => __( \'Auto Draft\' ), \'post_type\' => $post_type, \'post_status\' => \'auto-draft\' ) );如果没有钩子,WordPress会在哪里?
do_action(\'wp_insert_post\', $post_ID, $post); 可以在中看到wp_insert_post\'此处的定义:
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/post.php#L2656
因此,如果你陷入这样的境地:
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...
}
当然,这将是一件非常有趣的事情,而且是非常肯定的。或者,如果您查看media(媒体)按钮的获取方式:
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/includes/media.php#L371
它使用
get_upload_iframe_src, 定义得稍微低一点,这利用了全局
$post_ID, 这让我们回到
post-new.php 您可以在第46行看到它的定义:
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/post-new.php#L46
所以,一般来说,在第46行之后触发的所有钩子都是您自己的
$post_ID 全球,包括
wp_insert_post,
media_buttons, 还有其他几十个。请注意,
init 和
admin_init 在链条上来得太早。并确保钩子足够具体,只有在需要时才能开火。
wp_insert_post 是好的,但不会有全球性的
post_ID 设置,因为它将在函数返回后立即设置。
很迷人,不是吗?