page_template_dropdown()WP 1.5.0

Gets the template files for the specified post type and outputs them on the screen as <option> elements for the form select field.

The function is designed to work in the admin panel. If it is needed on the front end, the following files need to be included:

require_once ABSPATH . '/wp-admin/includes/theme.php';
require_once ABSPATH . '/wp-admin/includes/template.php';

No Hooks.

Returns

null. Nothing. Outputs HTML code for use in <select>.

Usage

page_template_dropdown( $default, $post_type );
$default(string)
The name of the template file that should be selected in the select.
Default: ''
$post_type(string)
The post type for which templates need to be retrieved. Since version 4.7, templates can be specified for any post types.
Default: 'page'

Examples

0

#1 Demo

<?php
//for the front
require_once ABSPATH . '/wp-admin/includes/theme.php';
require_once ABSPATH . '/wp-admin/includes/template.php';
?>

<select name="" id="">
	<?php page_template_dropdown(); ?>
</select>
<?php

The result is this HTML:

<select name="" id="">    
	<option value='page_tpl_custom.php'>My page template</option>
	<option value='page_tpl_custom2.php'>Another page template</option>
</select>

Changelog

Since 1.5.0 Introduced.
Since 4.7.0 Added the $post_type parameter.

page_template_dropdown() code WP 6.9

function page_template_dropdown( $default_template = '', $post_type = 'page' ) {
	$templates = get_page_templates( null, $post_type );

	ksort( $templates );

	foreach ( array_keys( $templates ) as $template ) {
		$selected = selected( $default_template, $templates[ $template ], false );
		echo "\n\t<option value='" . esc_attr( $templates[ $template ] ) . "' $selected>" . esc_html( $template ) . '</option>';
	}
}