1

I'm trying to figure out how to properly register a page template (and all of the associated assets, like CSS and Images) from a plugin. Basically, I've created a landing page that I want to live outside of the Theme, so I can use it on multiple websites.

My code was as follows:

add_filter( 'page_template', 'custom_page_template' );
function custom_page_template( $page_template )
{
    $page_template = dirname( __FILE__ ) . '/custom-page-template.php';
    return $page_template;
}

But I'm not seeing the page template within WordPress.

1 Answer 1

1

You misunderstand what page_template does. It does not create a new template that you will "show up" somewhere and that you can use. It replaces the page.php template provided by the theme.

I think that what you want is 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' );

Or 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' );
1
  • It seems that hooking into template_redirect can break any code that may be hooked into template_redirect after your plugin. I would expect a workaround is to set a high integer priority in add_filter so that your plugin hooks last, but if two plugins do this, it would cause a conflict where whoever hooks last loses. Any thoughts on this? markjaquith.wordpress.com/2014/02/19/…
    – iyrin
    Apr 2, 2017 at 22:26

Your Answer

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

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