Is it possible to make custom page templates available from a plugin?

4 Answers 4


get_page_template() can be overridden via the page_template filter. If your plugin is a directory with the templates as files in them, it's just a matter of passing the names of these files. If you want to create them "on the fly" (edit them in the admin area and save them in the database?), you might want to write them to a cache directory and refer to them, or hook into template_redirect and do some crazy eval() stuff.

A simple example for a plugin that "redirects" to a file in the same plugin directory if a certain criterium is true:

add_filter( 'page_template', 'wpa3396_page_template' );
function wpa3396_page_template( $page_template )
    if ( is_page( 'my-custom-page-slug' ) ) {
        $page_template = dirname( __FILE__ ) . '/custom-page-template.php';
    return $page_template;
  • Hey Jan, do you have some example code on how to pass a plugin file in as a custom page template?
    – jnthnclrk
    Commented Nov 16, 2010 at 17:58
  • @trnsfrmr: It's really easy, I added a simple example to my answer.
    – Jan Fabry
    Commented Nov 16, 2010 at 19:46
  • 15
    Note that this has been more or less replaced by the "template_include" filter in later versions (3.1+).
    – Daniel R
    Commented Jun 10, 2013 at 15:54
  • Perfect !!!, you have save my time @JanFabry Commented Dec 21, 2018 at 14:02
  • As stated by @fireydude, this is not a generic solution. It's a workaround that hard-codes the page slug. Commented Jul 7, 2019 at 21:18

Overriding get_page_template() is just a quick hack. It does not allow the template to be selected from the Admin screen and the page slug is hard-coded into the plugin so the user has no way to know where the template is coming from.

The preferred solution would be to follow this tutorial which allows you to register a page template in the back-end from the plug-in. Then it works like any other template.

 * Initializes the plugin by setting filters and administration functions.
private function __construct() {
        $this->templates = array();

        // Add a filter to the attributes metabox to inject template into the cache.
            array( $this, 'register_project_templates' ) 

        // Add a filter to the save post to inject out template into the page cache
            array( $this, 'register_project_templates' ) 

        // Add a filter to the template include to determine if the page has our 
        // template assigned and return it's path
            array( $this, 'view_project_template') 

        // Add your templates to this array.
        $this->templates = array(
                'goodtobebad-template.php'     => 'It\'s Good to Be Bad',
  • Would be nice (and prefered) if you can post the relevant code from the link in your answer, otherwise this is nothing more than a bloated comment :-) Commented Oct 12, 2015 at 11:32
  • 1
    The tutorial actually credits Tom McFarlin's example as the originator of this approach.
    – fireydude
    Commented Oct 12, 2015 at 11:53

None of the previous answers was working for me. Here one where you can choose your template in Wordpress admin. Just put it in your main php plugin file and change template-configurator.php by your template name

//Load template from specific page
add_filter( 'page_template', 'wpa3396_page_template' );
function wpa3396_page_template( $page_template ){

    if ( get_page_template_slug() == 'template-configurator.php' ) {
        $page_template = dirname( __FILE__ ) . '/template-configurator.php';
    return $page_template;

 * Add "Custom" template to page attirbute template section.
add_filter( 'theme_page_templates', 'wpse_288589_add_template_to_select', 10, 4 );
function wpse_288589_add_template_to_select( $post_templates, $wp_theme, $post, $post_type ) {

    // Add custom template named template-custom.php to select dropdown 
    $post_templates['template-configurator.php'] = __('Configurator');

    return $post_templates;
  • This is the right way to do it. Commented Aug 26, 2023 at 8:52

Yes, it is possible. I found this example plugin very helpful.

Another approach that is come into my head is using WP Filesystem API to create the template file to theme. I am not sure that it is the best approach to take, but I am sure it work!

  • The linked example plugin is even pretty well documented. I like that. :)
    – Arvid
    Commented Aug 14, 2017 at 14:46

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.