• Resolved everyeurocounts

    (@everyeurocounts)


    i am adding templates from a plugin and want to create a page from the plugin as well, which works but i have issues with the correct template being applied. According to the codex, wp_insert_post template=> templatename.php which is probably looking in the theme for a template file.

    Is there anyway around this?

    function create_pages() {
    
    	$templates= array(
    	//name=template
    	'Advert-Payment' => 'ad-payment-screen'
    
    	);
    
    	foreach ($templates as $name=>$template) {
    		if(get_option($name)===False) {
    			$post= array(
    					'post_title'    => $name,
    					'post_name' =>$name,
    				 	'post_status'    => 'publish',
      					'post_type'          => 'page',
      					'post_content'  => '',
      					'post_author'   => 1,
      					'page_template' => $template
    
    			);
    
    			$postid=wp_insert_post( $post );
    			add_option( $name, $name );
    		}
    
    	}
    }
    
    add_action('wp_loaded', 'create_pages');
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Add a postmeta entry for the post with a key of “_wp_page_template”. Set the value to the path to your template file. I forget what it’s relative to, probably either the installation root or theme folder. An absolute path should work too.

    Thread Starter everyeurocounts

    (@everyeurocounts)

    Thanks a million for this, it never occurred to me to check the database for a solution! I got it working using a relative url + a function to check in the plugin if not found in the theme.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘add page template by name’ is closed to new replies.