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 want create 5 pages when user active my theme. I found a code from wpcanyon which can create one page only. From this code how do I create 5 pages without repeat it 5 times.

if (isset($_GET['activated']) && is_admin()){

    $new_page_title = 'This is the page title';
    $new_page_content = 'This is the page content';
    $new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.

    //don't change the code bellow, unless you know what you're doing

    $page_check = get_page_by_title($new_page_title);
    $new_page = array(
        'post_type' => 'page',
        'post_title' => $new_page_title,
        'post_content' => $new_page_content,
        'post_status' => 'publish',
        'post_author' => 1,
    );
    if(!isset($page_check->ID)){
        $new_page_id = wp_insert_post($new_page);
        if(!empty($new_page_template)){
            update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
        }
    }

}

Let me know.

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Ehm, just repeat the page creation code several times, providing different content and such. Is there any issue with that?..

share|improve this answer
    
No issue with it. But possible to make it more simple :) –  haha Jan 21 '11 at 12:29
    
@haha well, you can create separate function for creating page and call it five times... In the end creating page is just creating page. :) There is no API way to create bunch of pages at once or anything. –  Rarst Jan 21 '11 at 12:31
    
Yeah! make a function –  haha Jan 21 '11 at 13:12
    
Just create an array and loop through it. –  Tareq Oct 16 '11 at 16:25
add comment

Try this:

if (isset($_GET['activated']) && is_admin()){
    add_action('init', 'create_initial_pages');
}

function create_initial_pages() {
    $pages = array(
        'page1' => 'Page 1',
        'page2' => 'Page 2',
        'page3' => 'Page 3',
        'page4' => 'Page 4',
        'page5' => 'Page 5'
    );
    foreach($pages as $key => $value) {
        $id = get_page_by_title($value);
        $page = array(
            'post_type'   => 'page',
            'post_title'  => $value,
            'post_name'   => $key,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_parent' => ''
        );
        if (!isset($id)) wp_insert_post($page);
    };
}
share|improve this answer
add comment

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.