Found this cool snippet over at snipplr.com today, for automatically creating a page on theme activation, but i couldn’t not notice a few ways to improve it. Originally the snippet came from graphicriver’s forum.
You can skip right to the improved version if you want.
Original Snippet
Bellow you can see the original code with some comments added by me to help you (i guess) understand the code easier.
//Get data for page with the title of the new post.
//We'll later use this to check if it already exists, if it does we won't create it.
$page_check = get_page_by_title('Sermon Media');
$page_check_id = $page_check->ID;
//Declaring the data for our new page
$new_page = array(
'post_type' => 'page',
'post_title' => 'Sermon Media',
'post_status' => 'publish',
'post_author' => 1,
);
//If the page doesn't already exist create it
if(!isset($page_check_id)){
wp_insert_post($new_page);
//getting the data of our new page and assigning a page template for it.
//If you're not going to use a custom template remove the next 3 lines
$new_page_data = get_page_by_title('Sermon Media');
$new_page_id = $new_page_data->ID;
update_post_meta($new_page_id, '_wp_page_template','page-template.php');
}
Improving It
I found few ways to improve it, some are minor and some are very important. Let’s start…
Unnecessary database calls
Ok, this is very important. As it is now, every single time a page loads the snippet will be executing get_page_by_title() function. There is absolutely no need for that, we need the whole snippet to be executed only after the theme has been activated. Luckily, that’s very easy, here is how:
if (isset($_GET['activated']) && is_admin()){
//the theme has just been activated, coding goes here
}
Title is needed 3 times
Important. As you can see the page title is used 3 times(lines 3, 9 and 19). There is a chance of mistyping it on one place and create a problem (even the person that added it to snipplr forgot to change it on the last line, it was still “Post Title”).
Page template – issue 1
After the snippet is added there are 3 more lines, they add a page template to the newly created page. What if you don’t want to assign a specific template to the page? In my opinion it’s better to have a variable at the top which will be empty by default, and if you wish you can add the template filename there.
Page template – issue 2
wp_insert_post() will return the id of the page, there is no need to use a special function to get something we already have. So simply instead of “wp_insert_post()” it’ll be “$new_page_id = wp_insert_post()” and we can remove the next 2 lines.
Final Snippet
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);
}
}
}
Final Words
I’m not saying that “MattStrange”, the original author, wrote a bad code. We all write something that can be improved, hell i don’t even want to take a look at the wpCanyon’s current theme source code, it was made long time ago and i’ll simply start working on a new theme these days, instead of rewriting 90% of the current one.
Our WordPress themes
We have a few WordPress themes that we would like you to take a look at if you want.
check out the portfolio











thank you very much for this,
i was looking for something similar,
i try to find a way to add some categories on activation,
thanks a lot again
all the best!!!
Glad to help
http://codex.wordpress.org/Function_Reference/wp_insert_category
This is brilliant thanks!
Is there a preferred way to create multiple new pages on activation? Or even remove pages
Realy great. Was looking for days for this function before ending up here.
Thanks a lot
if ($_GET['activated']) only? No way.
if (isset($_GET['activated']) && is_admin() && current_user_can(‘edit_posts’)) instead
@Ozh – Thx Ozh. I used that when i needed to redirect the admin to theme options after the theme is activated, but i simplified it a bit for this snippet since it’s just a harmless page insert with predefined content.
Since is_admin() doesn’t make any db calls i guess it’s ok to include it (as soon as i finish the comment), but there is really no need to do the current_user_can check. It’s just going to make unnecessary calls to the db.
That’s just what i think
But you certainly know more about WordPress then i do, so let me know if you think i’m wrong.
would you just repeat the code to create additional pages? I need to create 5 pages on Theme activation.
thanks, I’ve been looking for this for a while.
thank you very much for the link Boba,
all the best my friend!
This is just awesome!
This is a great tutorial! Thank you for the help.