5

If i have pages that point to somefilename.php and want to change the filename to betterfilename.php, can I leave the template comments unchanged without manually changing all the pages?

/*
Template Name: Some File Name
*/

2 Answers 2

7

Chris gave me some good insight, and I appreciate the filter func. But I wound up changing the db through phpMyAdmin:

UPDATE wp_postmeta SET meta_value = 'new-filename.php' WHERE meta_value = 'old-filename.php';
4

When you select a page template, WordPress stores it as postmeta with the key _wp_page_template. The value that gets stored is the actual page template path relative to the template directory (or stylesheet directory).

So when WP looks for the template to include, it will for somefilename.php and when it doesn't find it, it will fall back to the page.php.

You can see the files WP searches for in the functions get_page_template and get_page_template_slug

<?php
// in wp-includes/template.php
/**
 * Retrieve path of page template in current or parent template.
 *
 * Will first look for the specifically assigned page template
 * The will search for 'page-{slug}.php' followed by 'page-id.php'
 * and finally 'page.php'
 *
 * @since 1.5.0
 *
 * @return string
 */
function get_page_template() {
    $id = get_queried_object_id();
    $template = get_page_template_slug();
    $pagename = get_query_var('pagename');

    if ( ! $pagename && $id ) {
        // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
        $post = get_queried_object();
        $pagename = $post->post_name;
    }

    $templates = array();
    if ( $template && 0 === validate_file( $template ) )
        $templates[] = $template;
    if ( $pagename )
        $templates[] = "page-$pagename.php";
    if ( $id )
        $templates[] = "page-$id.php";
    $templates[] = 'page.php';

    return get_query_template( 'page', $templates );
}

// in wp-includes/post-template.php
/**
 * Get the specific template name for a page.
 *
 * @since 3.4.0
 *
 * @param int $id The page ID to check. Defaults to the current post, when used in the loop.
 * @return string|bool Page template filename. Returns an empty string when the default page template
 *  is in use. Returns false if the post is not a page.
 */
function get_page_template_slug( $post_id = null ) {
    $post = get_post( $post_id );
    if ( 'page' != $post->post_type )
        return false;
    $template = get_post_meta( $post->ID, '_wp_page_template', true );
    if ( ! $template || 'default' == $template )
        return '';
    return $template;
}

Best solution: go update your pages. Other, less manual solution: hook into page_template and look for the old filename, replace it with the new:

<?php
add_filter('page_template', 'wpse57568_page_template');
function wpse57568_page_template($t)
{
    $old_slug = 'pages/t.php'; // replace this
    $new_slug = 'pages/new.php'; // replace this

    $page_id = get_queried_object_id();
    $template = get_post_meta($page_id, '_wp_page_template', true);
    if($template && 'default'!= $template && $old_slug == $template)
    {
        if(file_exists(trailingslashit(STYLESHEETPATH) . $new_slug))
        {
            $t = trailingslashit(STYLESHEETPATH) . $new_slug;
        }
        elseif(file_exists(trailingslashit(TEMPLATEPATH) . $new_slug))
        {
            $t = trailingslashit(TEMPLATEPATH) . $new_slug;
        }
    }
    return $t;
}

As a plugin.

2
  • 2
    My answer was wrong. I deleted it. When the filename changes the page reverts to the default template.
    – Chris_O
    Commented Jul 6, 2012 at 1:47
  • Thanks, i found some sql that helped me out. See below. and I'd give a vote up, but SO rep doesnt transfer to WP.SE :/ Commented Jul 6, 2012 at 3:36

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.