Is there a way to change the default page template selection?

Using template_include (as suggested by Brad Dalton) only changes the page template on the front end, not in the admin when editing a page.

On the other hand, changing the value in the post object before the metabox is rendered, as suggested by czerspalace, works! I added a check to only apply this when $post->page_template is not set, like this:

function wpse196289_default_page_template() {
    global $post;
    if ( 'page' == $post->post_type 
        && 0 != count( get_page_templates( $post ) ) 
        && get_option( 'page_for_posts' ) != $post->ID // Not the page for listing posts
        && '' == $post->page_template // Only when page_template is not set
    ) {
        $post->page_template = "page-mytemplate.php";
    }
}
add_action('add_meta_boxes', 'wpse196289_default_page_template', 1);

Leave a Comment