8

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.

4
  • I know this doesn't answer your question, but why not just change the template? In that way, the default template (page.php or whatever) is still accurate and uses the script you want.
    – jdm2112
    Jul 31, 2015 at 18:03
  • Because I have more than one template, and specific group of pages have specific templates. Changing the default template to the correct template makes things easier for the customer. This is not a big deal on a small site, but it makes a big difference on sites with hundreds of pages.
    – gdaniel
    Jul 31, 2015 at 18:08
  • The WP template hierarchy provides several options for consideration. You might find your answer there. developer.wordpress.org/themes/basics/template-hierarchy
    – jdm2112
    Jul 31, 2015 at 18:19
  • I know how the hierarchy works. I just want to know if there's an option, other than JS, to switch the default selected template in the page edit screen (when adding a new page). That's all. And since the point of the question is being ignored, my guess is that there isn't one.
    – gdaniel
    Jul 31, 2015 at 18:21

6 Answers 6

6

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);
4
  • But how is this actually implemented?
    – JGallardo
    Sep 15, 2017 at 22:20
  • 1
    The code above should work in your theme's functions.php file (or a custom plugin). You need to change the string page-mytemplate.php to a name matching your desired page template in your theme.
    – Adrian B
    Sep 18, 2017 at 12:47
  • You inspired me to do this in the new block editor (Gutenberg) using "rest_prepare_{$post_type}" filter hook. Feb 10, 2019 at 11:36
  • Although it changes the text value, you'll have to press save to have ACF (for example) trigger and show appropriate fields, etc Jan 30, 2022 at 18:30
1

I have not tested this, but it may work. The template is chosen based on a value in the global post object, so we need to change that value in the post object before the metabox is rendered. The code which chooses the default dropdown is here: https://core.trac.wordpress.org/browser/trunk/src/wp-admin/includes/meta-boxes.php#L751

add_action('add_meta_boxes', 'wpse196289_default_page_template', 1);

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 ) {
         if( $my_conditions )
             $post->page_template = "page-mytemplate.php";
    }
}
0
1

The simplest way that I found, without hacking code is to create a separate page.php (away from your page_template-name.php files) and get_template_part() to redirect to your template file. This then provides the default template across your site, as well as keeping the template as a discrete option.

<?php get_template_part('page_template-name'); ?>

This is all that needs to exist in your page.php file. It simply gets your default template. If you want to change your template site wide, then you could simply change the slug. Any page that then has the default option selected will change. Any page that is specifically set to use the template (i.e. set to template-1 instead of default), won't change.

1

I've managed to accomplish that in the new block editor (Gutenberg).

The task needs a hook into REST API response in this case.

I took the conditions from Adrian's answer.

foreach ( array( 'page', 'post' ) as $post_type ) :
    add_filter( "rest_prepare_{$post_type}", 'wpse196289_rest_set_default_page_template', 1, 2 );
endforeach;

function wpse196289_rest_set_default_page_template( $data, $post ) {
    if ( 0 != count( get_page_templates( $post ) ) && get_option( 'page_for_posts' ) != $post->ID && '' == $data->data['template'] ) :
        $data->data['template'] = 'page-mytemplate.php';
    endif;

    return $data;
}
2
  • When I implemented this code, the editor would set my template to the template I specified, however, I would get messages saying "Update failed" whenever I tried to update.
    – glass duo
    Mar 24, 2020 at 19:51
  • This code will broke the site and you'll need to re-install again. Dec 13, 2020 at 3:35
0

Use template_include in your functions file with a conditional tag.

add_filter( 'template_include', 'default_page_template', 99 );

function default_page_template( $template ) {

    if ( is_singular( 'page' )  ) {
        $default_template = locate_template( array( 'default-page-template.php' ) );
        if ( '' != $default_template ) {
            return $default_template ;
        }
    }

    return $template;
}
0

It's success for me:

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
    ) {
//print_r(get_page_templates( $post )); get list templates
//Array([Elementor Canvas] => elementor_canvas [Elementor Full Width] => elementor_header_footer [Theme] => elementor_theme)
        $post->page_template = "elementor_header_footer"; // set template you want
    }
}
add_action('add_meta_boxes', 'wpse196289_default_page_template', 1);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.