Update Page Templates Automatically

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(
		        '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';
				case 'full-width-page.php':
					$new_template = 'templates/full-width-page.php';
				case 'full-width-portfolio.php':
					$new_template = 'templates/full-width-portfolio.php';
			if ( $new_template ) {
				update_post_meta( get_the_ID(), '_wp_page_template', true );

About Devin

I'm a WordPress developer based in Austin, Texas. I run a little theme shop called DevPress and work for a startup called Nano. Find me on twitter @devinsays.

4 Responses

  1. 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.

  2. markS

    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) {
    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 to Aj Clarke Cancel reply