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 am wanting to make about 6 pages or so on theme activation, and I have found two ways to do it on this site.

One will make as many pages needed and that is all, the second one will make only one page but allows me to pick a page template and content.

I am looking for a kind of mash up of the both, but have not been able to get it to work. I would like to make as many pages as needed and for each page set a template and content if needed.

Here is the code I have that will make more than one page.

function create_initial_pages() {
    $pages = array(
        'page1' => 'Page 1',
        'page2' => 'Page 2',
        'page3' => 'Page 3',
        'page4' => 'Page 4'
    );
    foreach($pages as $page_url => $page_title) {
        $id = get_page_by_title($page_title);
        $page = array(
            'post_type'   => 'page',
            'post_name'   => $page_url,
            'post_title'  => $page_title,
            'post_status' => 'publish',
            'post_author' => 1,
            'post_parent' => ''
        );
        if (!isset($id)) wp_insert_post($page);
    };
}

And here is code that will make one page but set content and a page template

if (isset($_GET['activated']) && is_admin()){
    $new_page_title = 'Sitemap';
    $new_page_content = ' ';
    $new_page_template = 'sitemap.php'; //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);
            }
    }
}

Does anyone know a way to get this done?

Edit: If able, I could also see having use for creating a child page as well but not that needed.

share|improve this question
    
What if your users don't want the theme creating pages for them? Your theme could alternatively provide a button that fires an ajax event to create them(basically just doing what you have in your answer, but on cue), thus the pages are created at the user's discretion and not forced upon them(it's not for a theme to go about unknowingly creating pages in a users WordPress installation). –  t31os Aug 9 '13 at 0:51
    
That would be useful to have some kind of "Make Pages" button, but I am using this code for when I install a basic theme I have been working on. Well sometimes there will be pages I wont need and end up removing. I have added it in to save me the time to make the pages and have a starting point. –  Chaos67731 Aug 21 '13 at 19:44
add comment

1 Answer

There might be a better way to do this, but after testing and testing I have found out how to get the code to work :)

So this code will make as many pages as needed, set different content for each page and set a different template if needed.

I am not the best at coding php, so there might be a better way to do this but it works (after hours of testing)

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

    $pages = array( 
         // Page Title and URL (a blank space will end up becomeing a dash "-")
        'Services' => array(
            // Page Content     // Template to use (if left blank the default template will be used)
            'Services Content'=>'page-bottom-sidebar.php'),

        'Prices' => array(
            'Prices Content'=>'page-wide.php'),

        'F A Q' => array(
            'FAQ Content'=>' '),
    );

    foreach($pages as $page_url_title => $page_meta) {

        $id = get_page_by_title($page_url_title);

        foreach ($page_meta as $page_content=>$page_template){

            $page = array(
                'post_type'   => 'page',
                'post_title'  => $page_url_title,
                'post_name'   => $page_url_title,
                'post_status' => 'publish',
                'post_content' => $page_content,
                'post_author' => 1,
                'post_parent' => ''
            );

            if(!isset($id->ID)){
                $new_page_id = wp_insert_post($page);
                if(!empty($page_template)){
                    update_post_meta($new_page_id, '_wp_page_template', $page_template);
                }
            }
        }
    }
}

Well I hope it helps someone out there!

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.