WordPress Theme Developers, Save Yourself Tons of Setup Time

Do you design and develop themes for multiple clients over and over again? And every time you setup a new WordPress installation, what do you do? Log in, activate your theme, create a “Home” page, a “Blog” page, change the front page to static, maybe build out a “Privacy Policy” page…etc.

Sure, that only takes maybe 5 minutes to do. But 5 minutes times 100 websites equals, like, forever. Here’s how to shave some time off of that process and automate it completely.

Add the following to your functions.php file:

// programmatically create some basic pages, and then set Home and Blog
// setup a function to check if these pages exist
function the_slug_exists($post_name) {
global $wpdb;
if($wpdb->get_row("SELECT post_name FROM wp_posts WHERE post_name = '" . $post_name . "'", 'ARRAY_A')) {
return true;
} else {
return false;
}
}
// create the blog page
if (isset($_GET['activated']) && is_admin()){
$blog_page_title = 'Blog';
$blog_page_content = 'This is blog page placeholder. Anything you enter here will not appear in the front end, except for search results pages.';
$blog_page_check = get_page_by_title($blog_page_title);
$blog_page = array(
'post_type' => 'page',
'post_title' => $blog_page_title,
'post_content' => $blog_page_content,
'post_status' => 'publish',
'post_author' => 1,
'post_slug' => 'blog'
);
if(!isset($blog_page_check->ID) && !the_slug_exists('blog')){
$blog_page_id = wp_insert_post($blog_page);
}
}
// create the site map page
if (isset($_GET['activated']) && is_admin()){
$sitemap_page_title = 'Site Map';
$sitemap_page_check = get_page_by_title($sitemap_page_title);
$sitemap_page = array(
'post_type' => 'page',
'post_title' => $sitemap_page_title,
'post_status' => 'publish',
'post_author' => 1,
'post_slug' => 'site-map'
);
if(!isset($sitemap_page_check->ID) && !the_slug_exists('site-map')){
$sitemap_page_id = wp_insert_post($sitemap_page);
}
}
// change the Sample page to the home page
if (isset($_GET['activated']) && is_admin()){
$home_page_title = 'Home';
$home_page_content = '';
$home_page_check = get_page_by_title($home_page_title);
$home_page = array(
'post_type' => 'page',
'post_title' => $home_page_title,
'post_content' => $home_page_content,
'post_status' => 'publish',
'post_author' => 1,
'ID' => 2,
'post_slug' => 'home'
);
if(!isset($home_page_check->ID) && !the_slug_exists('home')){
$home_page_id = wp_insert_post($home_page);
}
}
if (isset($_GET['activated']) && is_admin()){
// Set the blog page
$blog = get_page_by_title( 'Blog' );
update_option( 'page_for_posts', $blog->ID );

// Use a static front page
$front_page = 2; // this is the default page created by WordPress
update_option( 'page_on_front', $front_page );
update_option( 'show_on_front', 'page' );
}

What we do here:

  1. Create our various pages, “Blog”, “Site Map” and “Privacy Policy”, including setting their slugs, titles and content. This happens upon activation of the theme, and is setup so that it won’t overwrite or create duplicate pages.
  2. Edit the default “Sample Page” that comes with WordPress to create our home page. This function looks much like the ones that create the three pages above, but by setting the ID to 2 (which is the ID of that “Sample Page”), we overwrite that with our home page information.
  3. Then near the end there, we set the “Blog” page as our posts page, and the “Home” page as our front page.

With all of your newfound time, you’ll have plenty of free time to +1 me on Google. ūüėõ

Special thanks to Nicolas Kuttler and this Pastebin from a WordPress user.

Up Next: WordPress Conditional Function for Static Home Page vs. Recent Blog Posts