Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    It should be the name of the php file in your theme, no path. Like “template.php”. Or “default” for no template.

    Thread Starter jaredstein

    (@jaredstein)

    Thanks for the reply, but that doesn’t seem to work for me. More specifics:

    I’m using a custom theme page (page-proposal.php) in the theme directory properly named as “Page Proposal”. My custom form uses wp_insert_post as follows (in part):

    $prop_page = array();
    $prop_page[‘page_template’] = ‘page-proposal.php’;
    wp_insert_post($prop_page);

    The new page goes to “default” when I submit it, but in the actual Page editor GUI I can select “Page Proposal” from the template drop-down.

    Any other ideas would be welcome!

    Thread Starter jaredstein

    (@jaredstein)

    To add to that: I checked the db and found the entries in the wp_postmeta ending up as

    _wp_page_template default

    though this is updated properly if I change the template in the GUI. This makes me think that my wp_insert_post() isn’t sending the page_template value to the right place.

    I’ll post this question on the hacks forum too.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    I don’t know what to tell you. Here’s the code in WordPress that actually deals with the page template:

    if ( !empty($page_template) && 'page' == $post_type ) {
    		$post->page_template = $page_template;
    		$page_templates = get_page_templates();
    		if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) {
    			if ( $wp_error )
    				return new WP_Error('invalid_page_template', __('The page template is invalid.'));
    			else
    				return 0;
    		}
    		update_post_meta($post_ID, '_wp_page_template',  $page_template);
    	}

    As you can see, if you’re saving a page and have a page_template set (it’s extracted from that post array), then it updates the meta value to the value. So your code should work fine.

    Looks like the get_page_templates() is not available all the times. I managed to work around by not setting the page_template at the first time but just calling the update_post_meta() with the $post_ID as soon as it returned it on insert.

    I posted the code for this elsewhere on the forum on a similar stuff.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Syntax for page_template using wp_insert_post’ is closed to new replies.