A way to automatically install pages on theme install?

I got similiar situation where I needed to add page on theme activation and set it as homepage automatically.

Here’s how I did it:

add_action('after_setup_theme', 'mytheme_setup');

function mytheme_setup(){

 if(get_option('page_on_front')=='0' && get_option('show_on_front')=='posts'){
        // Create homepage
        $homepage = array(
            'post_type'    => 'page',
            'post_title'    => 'Home',
            'post_content'  => '',
            'post_status'   => 'publish',
            'post_author'   => 1
        ); 
        // Insert the post into the database
        $homepage_id =  wp_insert_post( $homepage );
        // set this page as homepage
        update_option('show_on_front', 'page');
        update_option('page_on_front', $homepage_id);
    }

}

Hope this helps someone.

Update:

add_action('after_setup_theme', 'mytheme_setup');

function mytheme_setup(){

 if(get_option('page_on_front')=='0' && get_option('show_on_front')=='posts'){
        // Create homepage
        $homepage = array(
            'post_type'    => 'page',
            'post_title'    => 'Home',
            'post_content'  => '',
            'post_status'   => 'publish',
            'post_author'   => 1
        ); 
        // Insert the post into the database
        $homepage_id =  wp_insert_post( $homepage );
        //set the page template 
        //assuming you have defined template on your-template-filename.php
        update_post_meta($homepage_id, '_wp_page_template', 'your-template-filename.php');
    }

}

Thanks Maruti Mohanty.

Leave a Comment