751 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
CyberShot says

why would you want to do this though?

228 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
stevebluck says
why would you want to do this though?

Some themes require the use to create a specific page to act as the portfolio, blog or contact page for example. So instead of getting the user to manually add them – you can create them on theme activation.

751 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 1+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
CyberShot says

I recently bought purchased two themes that have done this. One theme automatically inserted the pages and content when i installed the theme. I did not like this because I was using the theme for an existing website. So when I switched themes it screwed up everything. I ended up having to use a backup of the database, delete everything restore it and start completly over.

A second theme that I had purchased, offered the ability to choose if I wanted the example content installed. This is the way to go. Because I could install the theme and take off from there or press a button and have the demo stuff installed and begin to change everything the way I wanted it to be.

I love the idea of demo content being installed. Because it does help to learn how the template is put together. Because I could find names and just change them instead of having to create everything from scratch.

So if you are going to do this. As a buyer, I would suggest that you offer up the choice to your purchaser to install the demo content. Don’t force it on them because if they are using the theme for an existing site, the demo content can do more harm than good.

688 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has sold $5,000+ on Envato Market
  • Has collected 10+ items on Envato Market
+3 more
_rg_ says
I recently bought purchased two themes that have done this. One theme automatically inserted the pages and content when i installed the theme. I did not like this because I was using the theme for an existing website. So when I switched themes it screwed up everything. I ended up having to use a backup of the database, delete everything restore it and start completly over.

A second theme that I had purchased, offered the ability to choose if I wanted the example content installed. This is the way to go. Because I could install the theme and take off from there or press a button and have the demo stuff installed and begin to change everything the way I wanted it to be.

I love the idea of demo content being installed. Because it does help to learn how the template is put together. Because I could find names and just change them instead of having to create everything from scratch.

So if you are going to do this. As a buyer, I would suggest that you offer up the choice to your purchaser to install the demo content. Don’t force it on them because if they are using the theme for an existing site, the demo content can do more harm than good.

good point

1 post
  • Has been part of the Envato Community for over 3 years
mlee says

Great information presented in such a way that it is easy to understand.

Thanks for posting.

1383 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wpCanyonThemes says

Slightly improved version of Matt’s snippet.

  • It was executing the get_page_by_title() every time a page loads, now only when the theme is activated
  • wp_insert_post() returns the id, so no need to execute get_page_by_title() after the page is added
  • and a few more things…
if ($_GET['activated']){

    $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(isset($new_page_template)){
            update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
        }
    }

}

Wrote an article about it too, so if anybody is interested check it out.

1383 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wpCanyonThemes says

Sorry, one change.

if(isset($new_page_template)){

it should be

if(!empty($new_page_template)){
370 posts
  • Has referred 50+ members
  • Has sold $125,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+5 more
PixelStores says

Nice changes :)

Cheers

4 posts
  • Has been part of the Envato Community for over 5 years
  • Has collected 100+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in Israel
sagive says

Very cool @wpCanyonThemes helped me a lot – thanks ;)

1 post
  • Has been part of the Envato Community for over 3 years
  • Has referred 1+ members
  • Has collected 1+ items on Envato Market
chaos67731 says

I know this is a little old, but I could use some help.

I have found this code to allow me to make more than one pages, and sup pages but I need help getting it to set a page template to each pages.

SO

Pages 1 (template-page-1.php) Pages 1.1 (template-page-1-1.php)

Pages 2 (template-page-2.php) Pages 2.1 (template-page-2-1.php)

function create_initial_pages() {
    $pages = array(
        array(
            'name'  => 'page1',
            'title' => 'Page 1',
            'child' => array(
                'page1-1' => 'Page 1.1',
                'page1-2' => 'Page 1.2',
                'page1-3' => 'Page 1.3',
                'page1-4' => 'Page 1.4'
            )
        ),

        array(
            'name'  => 'page2',
            'title' => 'Page 2',
            'child' => array(
                'page2-1' => 'Page 2.1',
                'page2-2' => 'Page 2.2',
                'page2-3' => 'Page 2.3'
            )
        )
    );

    $template = array(
        'post_type'   => 'page',
        'post_status' => 'publish',
        'post_author' => 1
    );

    foreach( $pages as $page ) {
        $exists = get_page_by_title( $page['title'] );
        $my_page = array(
            'post_name'  => $page['name'],
            'post_title' => $page['title']
        );
        $my_page = array_merge( $my_page, $template );

        $id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );

        if( isset( $page['child'] ) ) {
            foreach( $page['child'] as $key => $value ) {
                $child_id = get_page_by_title( $value );
                $child_page = array(
                    'post_name'   => $key,
                    'post_title'  => $value,
                    'post_parent' => $id
                );
                $child_page = array_merge( $child_page, $template );
                if( !isset( $child_id ) ) wp_insert_post( $child_page );
            }
        }
    }
}
by
by
by
by
by
by