I am attempting to compare a username to a page template name. I found the following code:

get_post_meta( $post->ID, '_wp_page_template', true );

But this isn't quite what I am looking for. This returns the filename of the page template, IE:


What I am looking for is the actual template name defined in the page template here:

Template Name: products

Is this possible?


An alternative to @Ben Cole that's less intensive (especially if you have several page templates), but not as awesome because it doesn't use the WP_Theme object ;)

function wpse_184317_get_template_name( $page_id = null ) {
    if ( ! $template = get_page_template_slug( $page_id ) )
    if ( ! $file = locate_template( $template ) )

    $data = get_file_data(
            'Name' => 'Template Name',

    return $data['Name'];

And in use:

echo wpse_184317_get_template_name(); // Template name for current page
echo wpse_184317_get_template_name( 14 ); // Template name for page ID 14
| improve this answer | |

One way to retrieve the name of the template as defined in the file could be like this:

function get_template_name () {
    // List all available template names for current theme
    $available_templates = wp_get_theme()->get_page_templates();

    // Get filename of page template we are on
    $template_filename = basename(get_page_template())

    // Return the template name for the currently active file
    return $available_templates[$template_filename];

The function above will return whatever text is used for the name of the template. In the example below, it would return "NAME OF TEMPLATE":


More info about the WP_Theme object.

| improve this answer | |
  • You don't want basename, it'll break for page templates in a sub-directory of the theme. Use get_page_template_slug and then an isset to check it's a valid index. – TheDeadMedic Apr 15 '15 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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