368 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

Hey,

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

Help appreciated :)

Matt

95 posts
  • Has referred 10+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+4 more
bringthepixel says
368 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

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
  • Has referred 10+ members
  • Has sold $500,000+ on Envato Market
  • Has collected 100+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+4 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

368 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

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

368 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

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
  • Has been part of the Envato Community for over 5 years
  • Has referred 200+ members
  • Has sold $250,000+ on Envato Market
  • Interviewed on an Envato blog
+6 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
  • 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

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'); ?>
368 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

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 :)

4336 posts ThemeForest Reviewer
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Member of the Envato Team
+12 more
Ivor Envato team says

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

by
by
by
by
by
by