admin管理员组

文章数量:1327750

I’d like to have a page added by default when a new site is created with WP Multisite.

So I have my function that creates two pages:

function my_default_pages() {
    $default_pages = array('Impress', 'Contact');
    $existing_pages = get_pages();

    foreach($existing_pages as $page) {
        $temp[] = $page->post_title;
    }

    $pages_to_create = array_diff($default_pages,$temp);

    foreach($pages_to_create as $new_page_title) {
        // Create post object
        $my_post = array();
        $my_post['post_title'] = $new_page_title;
        $my_post['post_content'] = 'This is my '.$new_page_title.' page.';
        $my_post['post_status'] = 'publish';
        $my_post['post_type'] = 'page';

        // Insert the post into the database
        $result = wp_insert_post( $my_post );
    }
}

And I found out that I have to hook into the wpmu_new_blog[1] action that fires when a new site is created.

add_action('wpmu_new_blog', 'my_default_pages');

But I don’t know how make both working together …

I’d like to have a page added by default when a new site is created with WP Multisite.

So I have my function that creates two pages:

function my_default_pages() {
    $default_pages = array('Impress', 'Contact');
    $existing_pages = get_pages();

    foreach($existing_pages as $page) {
        $temp[] = $page->post_title;
    }

    $pages_to_create = array_diff($default_pages,$temp);

    foreach($pages_to_create as $new_page_title) {
        // Create post object
        $my_post = array();
        $my_post['post_title'] = $new_page_title;
        $my_post['post_content'] = 'This is my '.$new_page_title.' page.';
        $my_post['post_status'] = 'publish';
        $my_post['post_type'] = 'page';

        // Insert the post into the database
        $result = wp_insert_post( $my_post );
    }
}

And I found out that I have to hook into the wpmu_new_blog[1] action that fires when a new site is created.

add_action('wpmu_new_blog', 'my_default_pages');

But I don’t know how make both working together …

Share Improve this question edited Nov 7, 2012 at 19:15 user1706680 asked Nov 7, 2012 at 17:57 user1706680user1706680 6943 gold badges13 silver badges29 bronze badges 3
  • "But I don’t know how make both working together" - what do you mean? When/where are you calling the add_action? – TheDeadMedic Commented Nov 7, 2012 at 18:54
  • I call the add_action right after the function in my functions.php. But it doesn’t work. – user1706680 Commented Nov 7, 2012 at 18:59
  • Where are you placing this code? – 1.21 gigawatts Commented Feb 19, 2014 at 14:57
Add a comment  | 

1 Answer 1

Reset to default 11

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

本文标签: WP Multisite Adding pages on blog creation by default