751 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • United States
CyberShot says

why would you want to do this though?

228 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • United Kingdom
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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • United States
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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 6-7 years
  • Referred between 1 and 9 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Sold between 5 000 and 10 000 dollars
  • Uruguay
_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 a member for 3-4 years
mlee says

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

Thanks for posting.

1383 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
  • Serbia
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
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Referred between 10 and 49 users
  • Serbia
wpCanyonThemes says

Sorry, one change.

if(isset($new_page_template)){

it should be

if(!empty($new_page_template)){
366 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Has been a member for 4-5 years
  • New Zealand
  • Referred between 10 and 49 users
  • Bought between 10 and 49 items
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
PixelStores says

Nice changes :)

Cheers

4 posts
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 5-6 years
  • Israel
sagive says

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

1 post
  • Bought between 1 and 9 items
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
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