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
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');
December 28th, 2010 at 6:22 am
How To Manipulate Child Pages to Use Parent Page Templates Automatically « graphicbeacon…
Learn how to automate a process whereby child pages will be manipulated to adapt to parent page templates automatically…….
January 20th, 2011 at 10:47 am
I can´t get it work. Getting following message:
Parse error: syntax error, unexpected T_VARIABLE in /…/functions.php on line 517
Copied and pastet the code in functions.php
January 25th, 2011 at 8:45 pm
You may need to check and ensure you pasted the code properly. I don’t seem to find anything wrong with the syntax of the code. Maybe you pasted this incorrectly?
Is there anyway you can show me your functions.php file?
February 10th, 2011 at 5:07 pm
This would be perfect, but it doesn’t work for me. I don’t get any type of error messages.
March 5th, 2011 at 11:16 am
@ck @Kier Sims
Try changing this line:
$parents = get_post_ancestors($post->ID);
to this one:
$parents = (is_page() && ($post->post_parent==$post->ID)) ? return true : return false;
March 18th, 2011 at 5:59 pm
WP Ver 3.1 M getting an error on this line
$parents = ($post->post_parent==$post->ID) ? return true : return false;
March 26th, 2011 at 11:00 am
Hi Puneet
You may have to revert to replacing the $parents variable to this then:
$parents = get_post_ancestors($post->ID);Will make time to test all of this but please let me know how this works for you.
April 6th, 2011 at 12:54 am
Very interesting function Beac! I found this post looking for ways to mandate a given page template for “top” pages and retain the ability to select templates for all other pages. This is the opposite, but I think it won’t be too difficult to reverse.
By the way, you might not need to declare $post as a global anymore. I could be misremembering, but in my research, I believe I’ve seen where a plugin author took out global $post; to resolve issues under 3.x.
May 25th, 2011 at 8:33 pm
I was also getting the error of “Parse error: syntax error….”
till I used this code instead that someone posted over at forrst:
/**
* 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 = get_post_ancestors($post->ID);
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');
May 30th, 2011 at 12:14 pm
@gray ayer
The code the person posted over at forrst was taken from here – check the earlier code. Would have been nice if Anthony Clark gave credit to whom credit was due.