I have a plugin that creates a template file stored in the plugin's 'templates' folder. It also creates a new page using code similar to:

    $template_file =  plugin_dir_path( __FILE__ ) . '/templates/mytemplate.php' ;
    $the_page         = array(
        'post_type' => 'page',
        'post_title' => 'My New Page',
        'post_content' => 'some text for the page content',
        'post_status' => 'publish',
        'post_author' => 1,
        'post_slug' => 'my-new-page',
        'page_template' => $template_file,
    $the_page_id = wp_insert_post($the_page);

The template file is valid, and can be used manually. It is registered as an available template with the template_include filter. The code will create the page.

The created page doesn't use the page_template value but uses the 'default' template from the theme.

The wp_insert_post docs seem to indicate that the page_template is relative to the current theme folder, although I can't find that specifically. There is this comment to the wp_insert_post function in the codex: https://developer.wordpress.org/reference/functions/wp_insert_post/#comment-3090 ) which states:

The post_template parameter should be described as well. It should be set with a full file name, such as ‘templatefilename.php’. This would appear to reference the template filename in the theme folder.

How do you specify a template file outside of the theme folder? Specifying the absolute path of the template file (as shown in the first code block) doesn't set the proper template when the page is created. Using just 'mytemplate.php' as the file name also doesn't work.


I tried the answer from @abhick, and that didn't work. Additional info:

The template is created in the plugin folder, and the page_template value of wp_insert_post is set to the absolute location of that template (in the plugin folder). You can see the value in the post_meta for that post, but the template name in post/quickview is still set as default. The plugin template name is seen in the drop-down list.

But I can't get the plugin template's name to show in the template field in the post edit screen. And the plugin template, even though the full file is in the post_meta, is not used.

I can't use a 'dotted' filename as the template file (to get a path relative to the theme folder), as the validate_file function checks for directory traversal.

I could add the template to the theme, but that's not good practice, I'd think. And the user can change the template manually, but I want to avoid that.

1 Answer 1


It is probably wp_insert_post can't recognize your template. You need to include it in the list of the templates.

function wpse410645_add_plugin_template( $page_templates ) {

    $page_templates[plugin_dir_path( __FILE__ ) . '/templates/mytemplate.php] = 'My Template Name';
    return $page_templates;

add_filter( 'theme_page_templates', 'wpse410645_add_plugin_template' );
  • That didn't appear to work, see added info in the question. Oct 22, 2022 at 18:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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