0

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

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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