Saturday, October 22, 2022
HomeWordPress Developmentwp insert put up - Create Web page with Template File from...

wp insert put up – Create Web page with Template File from Plugin Folder


I’ve a plugin that creates a template file saved within the plugin’s ‘templates’ folder. It additionally creates a brand new web page utilizing code much like:

    $template_file =  plugin_dir_path( __FILE__ ) . '/templates/mytemplate.php' ;
    $the_page         = array(
        'post_type' => 'web page',
        'post_title' => 'My New Web page',
        'post_content' => 'some textual content for the web page content material',
        '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 legitimate, and can be utilized manually. It’s registered as an obtainable template with the template_include filter. The code will create the web page.

The created web page would not use the page_template worth however makes use of the ‘default’ template from the theme.

The wp_insert_post docs appear to point that the page_template is relative to the present theme folder, though I am unable to discover that particularly. There’s this remark to the wp_insert_post operate within the codex: https://developer.wordpress.org/reference/features/wp_insert_post/#comment-3090 ) which states:

The post_template parameter needs to be described as properly. It needs to be set with a full file title, comparable to ‘templatefilename.php’. This would seem to reference the template filename within the theme folder.

How do you specify a template file exterior of the theme folder? Specifying absolutely the path of the template file (as proven within the first code block) would not set the right template when the web page is created. Utilizing simply ‘mytemplate.php’ because the file title additionally would not work.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments