Click on “Pages” -> Click on “Add New”

In the add new page screen. The default page template selected is “Default Template”. Is there a way to change the default option to, let’s say “My Other Template”. So that when I click on “Add New”, “My Other Template” will always be selected by default? This needs to be done before the page is saved. I can accomplish this with JS. Is there a WP option?

UPDATE:

Here’s an example:

Template Options:

  • Default Template (automatically selected)
  • My Template 1
  • My Template 2

Is there a WordPress way to change to:

  • Default Template
  • My Template 1 (automatically selected)
  • My Template 2

I thought maybe this could be accomplished with a function:

if (some condition is met)
 default_template = My Template 1
endif

I know how the template hierarchy works. I know how to make my template always be used for certain pages. But that’s not the question. I am just trying to make things easier for the end-user so that upon certain conditions I can preselect the template, that should be used on a group of pages, but still, give the user the flexibility to switch back to another template if needed.

5 s
5

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 Reply

Your email address will not be published. Required fields are marked *