从插件注册页面模板

时间:2015-06-30 作者:dcolumbus

我试图找出如何从插件中正确注册页面模板(以及所有相关资产,如CSS和图像)。基本上,我已经创建了一个登录页,我想生活在主题之外,所以我可以在多个网站上使用它。

我的代码如下:

add_filter( \'page_template\', \'custom_page_template\' );
function custom_page_template( $page_template )
{
    $page_template = dirname( __FILE__ ) . \'/custom-page-template.php\';
    return $page_template;
}
但我没有在WordPress中看到页面模板。

1 个回复
最合适的回答,由SO网友:s_ha_dum 整理而成

You misunderstand what page_template does. 它不会创建一个新模板,您将在某个地方“显示”并可以使用它。它取代了page.php 主题提供的模板。

我想你想要的是template_redirect:

function custom_page_template( $page_template ) {
  if (is_home()) {
    get_header();
    echo \'do stuff\';
    get_footer();
  }
}
add_filter( \'template_redirect\', \'custom_page_template\' );
template_include:

function custom_page_template( $page_template ) {
  if (is_home()) {
    $page_template = plugin_dir_path( __FILE__ ) . \'custom-page-template.php\';
    return $page_template;
  }
}
add_filter( \'template_include\', \'custom_page_template\' );

结束