PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Setting page template in functions.php

  1. dannybull
    Member

    We're successfully using Gravity forms to create a post that is converted into a page within functions.php. We're also changing the parent page based on a selection made in the form. Here's the code we're using:

    add_filter("gform_post_data", "change_post_type", 10, 2);
    function change_post_type($post_data, $form){
        //only change post type on form id X
        if($form["id"] != 1)
           return $post_data;
    
    	   //add other page data here
    	   $post_data["post_type"] = "page";
    	   $post_data["post_parent"] = $_POST["input_8"]; // Parent page is pulled from drop down on form
        return $post_data;
    }

    What we'd like to do next is change the page template in the above function. Does anyone know if this is possible?

    Many thanks.

    Posted 13 years ago on Sunday October 31, 2010 | Permalink
  2. Hi Danny,

    http://pastie.org/1264896

    I update your script with this snippet:

    $post_data["post_custom_fields"][] = array(
            "meta_name" => "_wp_page_template",
            "meta_value" => "page_custom.php"
            );

    In WordPress the page template is stored as a custom field per page. To update this when the post is created, we simply append a new custom field to the post data using WP's expected page template custom field for the meta name and the filename of your page template for the meta value.

    Hope this helps! :)

    Posted 13 years ago on Monday November 1, 2010 | Permalink
  3. dannybull
    Member

    Cheers David. I had to edit your code slightly to the following to get it working:

    $post_data["post_custom_fields"] = array(
           	"_wp_page_template" => "recipe.php"
    );

    Thanks for your help.

    Posted 13 years ago on Monday November 1, 2010 | Permalink