Set page template for all pages?

EDIT As noted in the comments on your question, the best approach would just be to edit your page.php file. If you want ALL of your pages to have the same page template, and not have to do anything extra to set it that way, it’s quite obvious why this is a good idea. 🙂

Then, you can make a custom page template for the pages that you don’t use your default page template for. That is pretty much what that system was made for!


If you insist though:

The page template of each page is actually stored as a custom field, you could loop over each post in your code and set the page template that way, instead of going and doing it in the database manually. See below:

add_action( 'admin_init', 'set_page_templates' );

function set_page_templates(){

    foreach( get_posts('post_type=page&posts_per_page=-1') as $page ) {
        $current_template = get_post_meta( $page->ID, '_wp_page_template', true );
        $new_template="new_template.php";

        if( $current_template != $new_template )
            update_post_meta( $page->ID, '_wp_page_template', $new_template );
    }

}

There may be a better action to put this on, as this will run on every admin page load. Maybe you could run this once and then remove the code, it should work fine that way. Alternatively, you could hook it onto the save_post action (which actually will return the page ID you are saving for you as one of your function arguments) to automatically change the page template if it isn’t what you want it to be on save of a post or page (of course skipping the foreach in that case).

Leave a Comment