Add Page Template from Plugin Code

Themes ship page templates in their root folder, but reusable layouts belong in a plugin. Here is how I add page template from plugin code so the Template dropdown shows a custom file that survives theme switches.

Add page template from plugin code in WordPress editor dropdown

Why Add Page Template from Plugin Code

If the template lives only in the theme, the next redesign drops it. Registering from a plugin keeps landing-page or full-width layouts portable. After it shows up in the editor, I often also add a page template column on the admin table so I can scan which pages use it.

Before and after add page template from plugin code

Register the template in the dropdown

Filter theme_page_templates and add a path ⇒ label pair. Put a real PHP template at templates/page-template.php inside your plugin (same structure as a theme page template).

/**
 * Add page templates.
 *
 * @param array $templates The list of page templates
 *
 * @return array  $templates  The modified list of page templates
 */
function sf_add_page_template_to_dropdown( $templates ) {
	$templates[ plugin_dir_path( __FILE__ ) . 'templates/page-template.php' ] = __( 'Page Template From Plugin', 'text-domain' );
	
	return $templates;
}

add_filter( 'theme_page_templates', 'pt_add_page_template_to_dropdown' );

Important: the function is named sf_add_page_template_to_dropdown, but add_filter points at pt_add_page_template_to_dropdown. Those strings must match or the filter never runs. Use the same name in both places (either sf_… or pt_…).

What each part does

  • theme_page_templates — Filter that builds the Page Attributes → Template list.
  • plugin_dir_path( __FILE__ ) — Absolute path to the current plugin file’s directory.
  • templates/page-template.php — Your template file relative to the plugin root; becomes the array key WordPress stores in post meta.
  • __( 'Page Template From Plugin', … ) — Label shown in the editor dropdown.

Load the file with template_include

Adding the dropdown entry alone is not enough. WordPress still looks for theme files unless you intercept template_include when the stored meta path is your plugin file:

/**
 * Load the plugin page template file when selected.
 */
function sf_load_plugin_page_template( $template ) {
	if ( is_page() ) {
		$meta = get_post_meta( get_the_ID(), '_wp_page_template', true );
		if ( $meta && file_exists( $meta ) ) {
			return $meta;
		}
	}

	return $template;
}
add_filter( 'template_include', 'sf_load_plugin_page_template' );

Together, the dropdown filter plus template_include is the full pattern to add page template from plugin code and have it render on the front end.

Prefer a Code Snippets Plugin?

For a one-off site you can paste both filters into a snippets plugin instead of shipping a full plugin—handy while testing.

Create a PHP snippet, paste the registration + template_include code, and run it Everywhere (or front end + admin so the dropdown appears in the editor). Point the path at a real PHP file on disk; a snippets-only approach still needs that template file somewhere loadable.

For anything you reuse across client sites, move the same code into a tiny plugin so the template path stays next to the PHP that registers it.

PHP to add page template from plugin code with theme_page_templates

Quick checks

  • Edit a Page → Template and confirm your label appears.
  • View the page on the front end; wrong content usually means template_include is missing or the path is wrong.
  • Keep function names identical in function and add_filter.
  • Docs: theme_page_templates.

FAQ

Does this work for posts or CPTs?

theme_page_templates is for pages. Other types need their own template filters or a block theme approach.

Template shows in the list but front end is wrong

You registered the label but did not load the file. Add the template_include filter (or equivalent) so WordPress uses the plugin path stored in _wp_page_template.

Can the template use get_header()?

Yes. Plugin page templates usually call the same template tags as theme page templates.

Block themes?

Classic PHP page templates still work for classic/hybrid setups. Pure block themes lean on the Site Editor; this snippet targets the classic Template dropdown.

Wrap-up

To add page template from plugin code: register the file on theme_page_templates, keep the callback name consistent, and load it with template_include. Do that once and your custom layout travels with the plugin, not the theme.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top