I am working on developing a theme which automatically creates a page on theme ativation. But the problem if when the theme is switched and activated again, it recreates the page.
is it possible to put a check if page is already available/publish, don't create or perhaps delete the page on theme switching/deativating?
Here is the code i am using for creating new page on theme activation :

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

global $wpdb;

    $the_page_title = 'Homepage Template';
    $the_page_name = 'homepage-1';
//$page_check = get_page_by_title('Page Title');
$page_check_id = $page_check->ID;
   $the_page = get_page_by_title( $the_page_title );

        // Create post object
    $new_page = array(
'post_type' => 'page',
'post_title' => 'Homepage Template',
'post_status' => 'publish',
'post_author' => 1,

$new_page_data = get_page_by_title($the_page_title);
$new_page_id = $new_page_data->ID;
update_post_meta($new_page_id, '_wp_page_template','homepage-1.php');

My Main goal is offering users multiple homepage templates/layouts they can switch from theme admin panel or settings -> reading . So if there is a better option please do suggest!

1 Answer 1


You are calling get_page_by_title( $page_title ) already. Use it!

if ( ! get_page_by_title( 
    _x( 'Homepage Template', 'home page title', 'your_theme_textdomain' ) 
    // create the page

Do not delete user content on theme deactivation. The user might have put much energy into that page, and maybe he is not aware you delete his work.

But I would not even create that page. Use a home.php for the newest blog posts and a front-page.php for the front page. Customize that and let the user choose any page.

Now, when your users rename the page and try another theme before switching back you don’t have to recreate the page with just a different title.

Update in response to your question update

Create the templates for the home page in a separate directory, for example /front-templates. Then offer those templates as a choice in the customizer and save the template name in the theme options.

In your front-page.php check for the option and include the matching template.

Sample code

$template = get_theme_mod( 'front-page-template', 'default' );

locate_template( "front-templates/$template.php", TRUE, TRUE );

Now you don’t have to create a new page, because front-page.php will be used automatically on the front page.

  • Hello, yes i understand that, but what i am trying to achieve is provide 3-4 homepage layouts. Do you have a better suggestion for that? cheers
    – Ayaz Malik
    Commented May 5, 2013 at 20:01
  • @AyazMalik If you update your question with more details about your goal I can come up with a better solution. :)
    – fuxia
    Commented May 5, 2013 at 20:05
  • hello just updated
    – Ayaz Malik
    Commented May 5, 2013 at 20:51
  • bravo my friend! that's awesome suggestion and perfectly makes sense! thank you :) This is my first theme deveopment, would you like to take a preview once i am done ?
    – Ayaz Malik
    Commented May 5, 2013 at 21:08
  • Sure, you’ll find my email on wpkrauts.com. :)
    – fuxia
    Commented May 5, 2013 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.