WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Making a plugin to make my life easier at creating over 3000+ pages. Take a look at the code I have so far.

if (isset($_POST['action']))
$one = array(
 'post_title' => $_POST['title'],
 'post_date' => $_SESSION['cal_startdate'],
 'post_content' => 'This is a post.',
 'post_status' => 'publish',
 'post_type' => 'page',
 'post_parent' => $_POST['title']
$two = array(
 'post_title' => $_POST['title'] . ' Air Conditioning',
 'post_date' => $_SESSION['cal_startdate'],
 'post_content' => 'This is a post.',
 'post_status' => 'publish',
 'post_type' => 'page',
 'post_parent' => $_POST['title']

$post_id = wp_insert_post($one);
$post_id = wp_insert_post($two);

update_post_meta($post_id, "_wp_page_template", $_POST['title'] . '.php');
echo $_POST['title'] . '.php';
else {


For some reason, the _wp_page_template is NOT WORKING and it's quite frustrating. If I go to the pages this code generates.. it still says "default template" when it should be $_POST['title'].php

If anyone can offer help on this, it would be much appreciated.

Regards, Brandon

share|improve this question
Two questions: 1. What hook are you using? 2. Have you dumped $post_id to see if you're actually getting an iD? – Brian Fegter Jul 17 '12 at 5:45
There's no hook it just runs immediately after the form is submitted. I'm not sure how to dump the id? I'd guess just printing it. I'll try that. EDIT: Yes it assigns a $post_id. – Brandon Jul 17 '12 at 6:04
You should be using a callback function for a WP hook to make sure all of the WP functions are loaded when running your code. codex.wordpress.org/Plugin_API#Actions – Brian Fegter Jul 17 '12 at 6:06
Brian would it be possible for you to contact me via skype @ brandonjjon? I've referenced the codex but can't quite make sense of it. – Brandon Jul 17 '12 at 6:07
Just curious as to how you're checking if this is updated. Are you looking at the custom fields? If so, the underscored fields do not display there. If you're looking at the Page Template dropdown, you must have an actual page template with the name of that PHP file in theme directory. Are you looking at the database entries to see if this is updated? – Brian Fegter Jul 17 '12 at 6:10
up vote 0 down vote accepted

You are most likely not assigning the right page template. Make sure your template name is the same case as the file in your theme directory. Also make sure the file name you provide as the template actually exists in the theme directory otherwise it will be marked as the Default page template.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.