Okay
  Print

How to change which WordPress page template is set as default template?

I recently had a page template that needed to be set as the default page template INSTEAD OF the “Default Template”

I dig some digging and found a solution that works exactly as expected.

Here is how to change which WordPress page template is set as default template.

Make sure to update “post” to “page” if needed.

Also make sure to update “template-fullwidth.php” with your own template file-name.

- You can use templates available in wordpress themes: template-fullwidth.php, template-canvas.php, etc...

- You can create your own template file: https://developer.wordpress.org/themes/template-files-section/page-template-files/

Add this code below into your functions.php file (path: wp-content/themes/theme-name/functions.php)

function archi_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-templates/template-fullwidth.php";
    }
}
add_action('add_meta_boxes', 'archi_default_page_template', 1);