382 posts
  • Weekly Top Seller
  • Elite Author
  • Author Level 8
  • New Zealand
+6 more
PixelStores
says

Hey,

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

Help appreciated :)

Matt

99 posts
  • Author Level 11
  • Elite Author
  • Featured Author
  • Exclusive Author
+6 more
bringthepixel
says
382 posts
  • Weekly Top Seller
  • Elite Author
  • Author Level 8
  • New Zealand
+6 more
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

99 posts
  • Author Level 11
  • Elite Author
  • Featured Author
  • Exclusive Author
+6 more
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

382 posts
  • Weekly Top Seller
  • Elite Author
  • Author Level 8
  • New Zealand
+6 more
PixelStores
says

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

382 posts
  • Weekly Top Seller
  • Elite Author
  • Author Level 8
  • New Zealand
+6 more
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
  • 6 Years of Membership
  • Affiliate Level 5
  • Author Level 9
  • Blog Interview
+7 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
  • 6 Years of Membership
  • Affiliate Level 1
  • Collector Level 4
  • Exclusive Author
+2 more
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'); ?>
382 posts
  • Weekly Top Seller
  • Elite Author
  • Author Level 8
  • New Zealand
+6 more
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 :)

4483 posts ThemeForest Senior Reviewer
  • Affiliate Level 1
  • Author Level 6
  • Collector Level 3
  • Envato Team
+14 more
Ivor
Envato team
says

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

by
by
by
by
by
by