# Register Page Templates From a Folder Outside the Theme

> Adds every PHP file in wp-content/page-templates/ to the page Template menu of classic themes, so custom layouts survive theme switches and updates.

- Type: PHP
- Runs: Run everywhere
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: templates, pages, themes
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/template-page-template-from-folder/

## Code

```php
// Folder that holds the templates.
$wpide_template_dir = WP_CONTENT_DIR . '/page-templates';

$wpide_find_templates = function () use ( $wpide_template_dir ) {
    static $found = null;
    if ( null !== $found ) {
        return $found;
    }
    $found = array();
    foreach ( (array) glob( trailingslashit( $wpide_template_dir ) . '*.php' ) as $file ) {
        $data = get_file_data( $file, array( 'name' => 'Template Name' ) );
        if ( $data['name'] ) {
            $found[ 'wpide/' . basename( $file ) ] = $data['name'];
        }
    }
    return $found;
};

add_filter( 'theme_page_templates', function ( $templates ) use ( $wpide_find_templates ) {
    return array_merge( $templates, $wpide_find_templates() );
} );

add_filter( 'template_include', function ( $template ) use ( $wpide_template_dir ) {
    if ( ! is_page() ) {
        return $template;
    }

    $chosen = get_page_template_slug( get_queried_object_id() );
    if ( 0 !== strpos( (string) $chosen, 'wpide/' ) ) {
        return $template;
    }

    $file = trailingslashit( $wpide_template_dir ) . basename( $chosen );

    return is_readable( $file ) ? $file : $template;
}, 99 );
```

## Notes

Each file needs a "Template Name: ..." header comment like a theme template; block themes use the Site Editor for templates instead.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Register Page Templates From a Folder Outside the Theme" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
