WP Multisite: Adding pages on blog creation by default

The hook is not the problem – your code runs in the context of the current site, not the one just created! The following code isn’t tested, but it should at least highlight the problem:

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' );

Leave a Comment