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,
);
if(!isset($page_check_id)){
wp_insert_post($new_page);
$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!