Update Page Templates Automatically

Posted

In the latest version of Portfolio Press I decided to move all the custom page templates into their own “templates” directory. This helps to organize the files better and simplifies the directory. However, I also needed to build an update routine for existing users who had the page templates set at the previous paths

To do that, I needed to query for all pages that had a page template set, check if matched one of my existing templates, and then update it to the new path. Here’s the code snippet in case it is useful to anyone else.

/**
 * Part of the Portfolio Press upgrade routine.
 * The page template paths have changed, so let's update the template meta for the user.
 */
function portfoliopress_update_page_templates() {
	$args = array(
		'post_type' => 'page',
		'post_status' => 'publish',
		'meta_query' => array(
		    array(
		        'key' => '_wp_page_template',
		        'value' => 'default',
		        'compare' => '!='
		    )
		)
	);
	$query = new WP_Query( $args );
	if ( $query->have_posts() ) :
		while ( $query->have_posts() ) : $query->the_post();
			$current_template = get_post_meta( get_the_ID(), '_wp_page_template', true );
			$new_template = false;
			switch ( $current_template ) {
				case 'archive-portfolio.php':
					$new_template = 'templates/portfolio.php';
					break;
				case 'full-width-page.php':
					$new_template = 'templates/full-width-page.php';
					break;
				case 'full-width-portfolio.php':
					$new_template = 'templates/full-width-portfolio.php';
					break;
			}
			if ( $new_template ) {
				update_post_meta( get_the_ID(), '_wp_page_template', true );
			}
		endwhile;
	endif;
}

About Devin

I'm a WordPress developer based in Austin, Texas. Follow my projects on GitHub, or more general WordPress ramblings as @devinsays on twitter.

4 thoughts on “Update Page Templates Automatically

  1. Aj Clarke wrote:

    Very useful, I didn’t realize it would be so simple! Now I can go back to some of my really old themes and clean them up ;)

  2. Great solution, Devin. Another thing to keep in mind is the body class name for these templates will change, too:

    page-template-full-width-portfolio-php => page-template-templatesfull-width-portfolio.php

    If users were using the old CSS selectors, things may not appear as desired. Maybe adding the old selectors back to the body uses the body_class filter would be a good solution to the plugin; you could also unset the new class names to keep the body classes cleaner.

    • Devin wrote:

      Good thought. Having the additional “templates” folder name in the body class is not particularly useful.

  3. markS wrote:

    Looking forward to the new changes and what they might add to the capability.
    I have a question about custom mods to the x3 image tile layout. I have modified the theme(2.0) to make them 6 across instead of 3. I had to make 8 changes to 6 files – and I wanted to do them as a child theme -But in the end I coudln’t.
    One of the problems was this line in the CSS
    #portfolio .portfolio-item:nth-of-type(3n) {
    margin-right:4%;
    }
    which I changed to : (6n)

    However in a child theme the 3n is still active. I had other problems too. So in the end the mods are all to the primary theme. At least I’ve documented them. :)

    Can you tell us anything about how the new version will deal with child themes and this kind of customisation?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>