钩子不是问题所在-您的代码在当前站点的上下文中运行,而不是在刚刚创建的上下文中运行!以下代码未经测试,但至少应突出显示问题:
function wpse_71863_default_pages( $new_site ) {
$default_pages = array(
\'Impress\',
\'Contact\',
);
switch_to_blog( $new_site->id );
if ( $current_pages = get_pages() ) {
$default_pages = array_diff( $default_pages, wp_list_pluck( $current_pages, \'post_title\' ) );
}
foreach ( $default_pages as $page_title ) {
$data = array(
\'post_title\' => $page_title,
\'post_content\' => "This is my $page_title page.",
\'post_status\' => \'publish\',
\'post_type\' => \'page\',
);
wp_insert_post( add_magic_quotes( $data ) );
}
restore_current_blog();
}
add_action( \'wp_insert_site\', \'wpse_71863_default_pages\' );