I want my theme to have an 'about' page available by default when a user uses my theme.
Is there a way to auto create a page when a user selects your template?
Or some other way of achieving this?
I want my theme to have an 'about' page available by default when a user uses my theme.
Is there a way to auto create a page when a user selects your template?
Or some other way of achieving this?
Share Improve this question edited Jul 23, 2017 at 2:44 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Jul 22, 2017 at 17:46 Dr. ChocolateDr. Chocolate 1111 silver badge3 bronze badges4 Answers
Reset to default 5There is a hook specifically for this purpose, called after_switch_theme
. You can hook into it and create your personal page after theme activation. Have a look at this:
add_action( 'after_switch_theme', 'create_page_on_theme_activation' );
function create_page_on_theme_activation(){
// Set the title, template, etc
$new_page_title = __('About Us','text-domain'); // Page's title
$new_page_content = ''; // Content goes here
$new_page_template = 'page-custom-page.php'; // The template to use for the page
$page_check = get_page_by_title($new_page_title); // Check if the page already exists
// Store the above data in an array
$new_page = array(
'post_type' => 'page',
'post_title' => $new_page_title,
'post_content' => $new_page_content,
'post_status' => 'publish',
'post_author' => 1,
'post_name' => 'about-us'
);
// If the page doesn't already exist, create it
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);
}
}
}
I suggest you use a unique slug to make sure the code always functions properly, since pages with slugs like about-us
are very common, and might already exist but don't belong to your theme.
The wp_insert_post() function is where to start https://developer.wordpress/reference/functions/wp_insert_post/ . You would place your code to create the post in the theme activation code. The googles show many examples of using that function.
(Although I am not sure that theme users would want a post automatically created. Seems to me a bit of an imposition on your part. I sure wouldn't want an automatically created post on my site. Even with full disclosure prior to theme activation. Sort of like 'do no harm' to someone else's blog.)
Starter content is a user friendly way of doing this, so it doesn't impose anything unwanted on the user. Take a look at https://make.wordpress/core/2016/11/30/starter-content-for-themes-in-4-7/
add_action('wpmu_new_blog', 'create_pages');
function create_pages(){
$awesome_page_id = get_option("awesome_page_id");
if (!$awesome_page_id) {
//create a new page and automatically assign the page template
$post1 = array(
'post_title' => "Awesome Page!",
'post_content' => "",
'post_status' => "publish",
'post_type' => 'page',
);
$postID = wp_insert_post($post1, $error);
update_post_meta($postID, "_wp_page_template", "awesome-page.php");
update_option("awesome_page_id", $postID);
}
}