Php Wordpress-从自定义插件目录设置页面模板

Php Wordpress-从自定义插件目录设置页面模板,php,wordpress,plugins,Php,Wordpress,Plugins,我正在创建一个插件来创建一个页面。我想更改该页面的模板 代码: 上面的代码根据数据库中的页面ID设置了路径,但当我在浏览器中打开该页面时,它不会加载我指定的模板 但是,如果我将模板文件放入主题/my theme/template.php中,它将加载模板 function my_plugn_create_front_page(){ $page_title = 'My Lovely Page'; $post_details = array( 'post_title' =&

我正在创建一个插件来创建一个页面。我想更改该页面的模板

代码:

上面的代码根据数据库中的页面ID设置了路径,但当我在浏览器中打开该页面时,它不会加载我指定的模板

但是,如果我将模板文件放入
主题/my theme/template.php
中,它将加载模板

function my_plugn_create_front_page(){
    $page_title = 'My Lovely Page';
   $post_details = array(
    'post_title'    => $page_title,
    'post_content'  => '',
    'post_status'   => 'publish',
    'post_author'   => get_current_user_id(),
    'post_type' => 'page'
   );
    $page_id = wp_insert_post( $post_details );
    update_post_meta( $page_id, '_wp_page_template', plugin_dir_path(__FILE__).'templates/template.php' );
}
register_activation_hook(__FILE__, 'my_plugin_create_front_page');