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

Hey,

Is it possible to create two pages both with selected page templates upon theme activation in wordpress?

Help appreciated :)

Matt

95 posts
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Europe
  • Referred between 10 and 49 users
bringthepixel says
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

Hey,

Thanks for your reply :)

I had a look at the codex, the function switches the current theme to a new template and stylesheet name, i think.

http://codex.wordpress.org/Function_Reference/switch_theme

This isn’t something i want to do, basically when you install wordpress for example, it has a default, such as the About page is created. I want my theme to automatically create two new pages both with a selected page template once activated. This may use the switch_theme function.

I have looked everywhere but not much info on the subject.

Cheers

95 posts
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Bought between 100 and 499 items
  • Exclusive Author
  • Has been a member for 4-5 years
  • Europe
  • Referred between 10 and 49 users
bringthepixel says
function btp_switch_theme_($theme)
{
    echo 'Voila!';    
}    

add_action('switch_theme', 'btp_switch_theme');

Then switch your theme in WP admin :). Then replace echo with your custom code that creates new pages

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

Cheers buddy, going to give it ago i’ll let you know the outcome. :)

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

Ok, i almost there, the code below automatically creates a new page named “My Page” once the theme is activated, however i need some help with the code to select a page template which is listed in the “Page Attributes”

if (is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" ) {

global $wpdb;

    $the_page_title = 'My Page';
    $the_page_name = 'my-page';

    $the_page = get_page_by_title( $the_page_title );

        // Create post object
        $_p = array();
        $_p['post_title'] = $the_page_title;
        $_p['post_content'] = "This is the text for page.";
        $_p['post_status'] = 'publish';
        $_p['post_type'] = 'page';

        // Insert the post into the database
        $the_page_id = wp_insert_post( $_p );
}

Ideas?

Cheers :)

204 posts Bold WordPress Theme Development
  • Bought between 1 and 9 items
  • Elite Author
  • Europe
  • Exclusive Author
  • Has been a member for 5-6 years
  • Interviewed on the Envato Notes blog
  • Referred between 200 and 499 users
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+1 more
srhnbr says

I don’t know if this may help but if you have a look here: http://codex.wordpress.org/Function_Reference/wp_insert_post

you’ll see the following said about pages:

NOTE (added 10/1/2009): The page_template key was removed from the $post table and is now located in the wp_postmeta table. To set the page template of a page, use update_post_meta instead.

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

The page template is stored as post meta with the key “_wp_page_template” so you could update this setting by using the update_post_meta() or add_post_meta() function like so:

<?php add_post_meta($post_id, '_wp_page_template', 'my_template.php'); ?>
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

Perfect! It works!

$page_check = get_page_by_title('Page Title');
$page_check_id = $page_check->ID;

$new_page = array(
'post_type' => 'page',
'post_title' => 'Page Title',
'post_status' => 'publish',
'post_author' => 1,

);

if(!isset($page_check_id)){
wp_insert_post($new_page);
$new_page_data = get_page_by_title('Page Title');
$new_page_id = $new_page_data->ID;
update_post_meta($new_page_id, '_wp_page_template','page-template.php');
}

Thanks for the help :)

4313 posts ThemeForest Reviewer
  • Envato Staff
  • Reviewer
  • Community Moderator
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Venezuela
  • Has been a member for 5-6 years
  • Envato Studio (Microlancer) Beta Tester
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
+9 more
Ivor Reviewer says

Hey Matt, that’s a nice piece of code, thanks for share.

by
by
by
by
by
by