Monday, February 23, 2015

Create Pages on Wordpress Theme Activation

If you are developing your own custom wordpress theme and want to deliver to client with custom page or pages when the theme is activated on client’s website. There is simple way add this lines of code in function .php in your theme. The pages will create automatically when you will define using this code in your theme.





if (isset($_GET['activated']) && is_admin())

$new_page_title = 'This is the page title';
$new_page_content = 'This is the page content';
$new_page_template = ''; //ex. template-custom.php. Leave blank if you don't want a custom page template.

//don't change the code bellow, unless you know what you're doing

$page_check = get_page_by_title($new_page_title);
$new_page = array(
'post_type' => 'page',
'post_title' => $new_page_title,
'post_content' => $new_page_content,
'post_status' => 'publish',
'post_author' => 1,
);
if(!isset($page_check->ID))
$new_page_id = wp_insert_post($new_page);
if(!empty($new_page_template))
update_post_meta($new_page_id, '_wp_page_template', $new_page_template);






Create Pages on Wordpress Theme Activation