manipulate-page-template

How To Manipulate Child Pages to Use Parent Page Templates Automatically

Posted in Wordpress | 10 Comments

Over at WPQuestions was a question by an ‘asker’ who was enquiring about a problem that required an automated process whereby child pages will be manipulated to adapt to parent page templates automatically. Now to go through each child page and manipulate this would demand a lot of manual labour especially when you have loads of nested pages.

The first thing I tried doing here was finding the ‘page_templates’ column in the database and use some form of SQL statement, thinking it was a column on its own.  A Google search led to the discovery that the ‘Page Template’ setting in the WordPress page edit screen is nothing more than a custom field dropdown box, able to be reached using the get_post_meta() function.

So you would detect the WordPress template using the function like this:

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

Source: Benedict Estraugh

The custom field key here is _wp_page_template and you can print this out easily using the usual echo statement.

Now to use this and manipulate child pages to use parent page templates, you have to check the current page template(above) and its parent page template using this code:

// Check current page and parent page template
$current_page_template = get_post_meta($post->ID,'_wp_page_template',true);
$parent_page_template = get_post_meta($post->post_parent,'_wp_page_template',true);

Then we can use get_post_ancestors() to check whether the current page is a child page or has a parent page and then finish this with the update_post_meta() function which will allow us to change the previous value of our custom field key:

// Check if parent page exists and update the _wp_page_template key value

$parents = get_post_ancestors($post->ID);
if($parents){update_post_meta($post->ID,'_wp_page_template',$parent_page_template,$current_page_template);}

Now lets combine this into a function called switch_page_template():

// switch_page_template() function

function switch_page_template(){
	global $post;
	$current_page_template = get_post_meta($post->ID,'_wp_page_template',true);
        $parent_page_template = get_post_meta($post->post_parent,'_wp_page_template',true);
        $parents = get_post_ancestors($post->ID);
	if($parents){update_post_meta($post->ID,'_wp_page_template',$parent_page_template,$current_page_template);}
}

Now we have our function in place and all we have to do here now is attach this function to the ‘save_post’ plugin hook using add_action():

add_action('save_post','switch_page_template');

Now simply paste this into your functions.php and that pretty much it fellas! Use the full code below:

// Manipulate Child Pages to Use Parent Page Templates Automatically
function switch_page_template(){
	global $post;
	$current_page_template = get_post_meta($post->ID,'_wp_page_template',true);
        $parent_page_template = get_post_meta($post->post_parent,'_wp_page_template',true);
        $parents = get_post_ancestors($post->ID);
	if($parents){update_post_meta($post->ID,'_wp_page_template',$parent_page_template,$current_page_template);}
}
add_action('save_post','switch_page_template');

So what do you think? Leave a comment below, thanks :-)

UPDATE – 05/03/11

I’ve updated the code here to check if the post type of the document is a ‘page‘ as a ‘post‘ post type does not deal with page templates.  I’ve also made slight adjustments  to the $parents variable, which does a more precise check.

// Manipulate Child Pages to Use Parent Page Templates Automatically
function switch_page_template(){
	global $post;
        if(is_page()){// Checks if current post type is a page, rather than a post

	$current_page_template = get_post_meta($post->ID,'_wp_page_template',true);
        $parent_page_template = get_post_meta($post->post_parent,'_wp_page_template',true);
        $parents = (is_page() && $post->post_parent==$post->ID) ? return true : return false;
	if($parents){update_post_meta($post->ID,'_wp_page_template',$parent_page_template,$current_page_template);

        }// End check for page
}
add_action('save_post','switch_page_template');


10 Comments to “How To Manipulate Child Pages to Use Parent Page Templates Automatically”











Recommended

Expert Help with WordPress
Learn how you can market your business on Twitter
Get the Book now! Use 25% discount code 'november25'
Hostgator - Cheap and Reliable Web Hosting - WordPress friendly

Incoming Tweets