0

I'm currently using this code to create some default pages in my Wordpress Multiste...

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

I was just wondering whether it was possible to create subpages/child pages as well? For example to have 'Information' as a child page of 'About'.

Thanks guys!

1 Answer 1

0

wp_insert_post() returns a post ID for the fresh post. You can use that ID to set a parent post.

Example

$about_id = wp_insert_post(
    array (
        'post_title'   => 'About',
        'post_status'  => 'publish',
        'post_type'    => 'page',
    )
);
wp_insert_post(
    array (
        'post_title'   => 'Information',
        'post_status'  => 'publish',
        'post_type'    => 'page',
        'post_parent'  => $about_id,
    )
);

In your case, I would restructure the page data and build a nested array:

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

Then I would move the page insertion to a separate function to allow recursion:

function insert_page( $title, Array $properties, $post_parent = 0 )
{
    $data = array(
        'post_title'   => $title,
        'post_content' => $properties[ "content" ],
        'post_status'  => 'publish',
        'post_type'    => 'page',
        'post_parent'  => $post_parent,
    );

    $id = wp_insert_post( add_magic_quotes( $data ) );

    if ( ! empty ( $properties[ "children" ] ) )
    {
        foreach ( $properties[ "children" ] as $child_title => $child_properties )
            insert_page( $child_title, $child_properties, $id );
    }
}

So your complete code should look like this:

function default_pages( $blog_id )
{
    $default_pages = array(
        'Contact'     => array(
            "content" => "This is your 'Contact' page. Enter in your content here."
        ),
        'About'       => array(
            "content" => "This is your 'About' page. Enter in your content here.",
            "children" => array(
                'Information' => array(
                    "content" => "This is your 'Information' page. Enter in your content here."
                )
            ),
        ),
        'Home'        => array(
            "content" => "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 => $properties )
        insert_page( $page_title, $properties );

    restore_current_blog();
}

function insert_page( $title, Array $properties, $post_parent = 0 )
{
    $data = array(
        'post_title'   => $title,
        'post_content' => $properties[ "content" ],
        'post_status'  => 'publish',
        'post_type'    => 'page',
        'post_parent'  => $post_parent,
    );

    $id = wp_insert_post( add_magic_quotes( $data ) );

    if ( ! empty ( $properties[ "children" ] ) )
    {
        foreach ( $properties[ "children" ] as $child_title => $child_properties )
            insert_page( $child_title, $child_properties, $id );
    }
}
2
  • Sorry - where would I add this ? When i added into my existing code it just duplicated the set of About>Information pages multiple times.
    – Laura
    Oct 23, 2013 at 3:18
  • @Laura See my update. It is not tested and might need some tweaking. :)
    – fuxia
    Oct 23, 2013 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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