Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've got this code which sets up 4 default pages on every new site install on Wordpress MU. I was wondering how I could set up different 'post_content' for each of the four pages?


function default_pages( $blog_id )
{
$default_pages = array(
    'Contact',
    'Information',
    'About',
    'Home',
);

switch_to_blog( $blog_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 your '$page_title' page. Enter in your content here.",
        'post_status'  => 'publish',
        'post_type'    => 'page',
    );

    wp_insert_post( add_magic_quotes( $data ) );
}

restore_current_blog();
}

add_action( 'wpmu_new_blog', 'default_pages' );
share|improve this question

1 Answer 1

up vote 0 down vote accepted

Make your $default_pages array being associated array, where a key is page title and a value is page content. Then modify your foreach loop to fetch $key => $value pairs and use it for the wp_insert_post function. Like this:

function default_pages( $blog_id )
{
$default_pages = array(
    'Contact'     => "This is your 'Contact' page. Enter in your content here.",
    'Information' => "This is your 'Information' page. Enter in your content here.",
    'About'       => "This is your 'About' page. Enter in your content here.",
    'Home'        => "This is your 'Home' page. Enter in your content here.",
);

switch_to_blog( $blog_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 => $page_content ) {        
    $data = array(
        'post_title'   => $page_title,
        'post_content' => $page_content,
        'post_status'  => 'publish',
        'post_type'    => 'page',
    );

    wp_insert_post( add_magic_quotes( $data ) );
}

restore_current_blog();
}

add_action( 'wpmu_new_blog', 'default_pages' );
share|improve this answer
    
Beautiful! Thanks @Eugene-Manuilov –  Laura Oct 6 '13 at 1:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.