5

I am working on a client's site and they need to select a Template whenever they are adding a new page; the "Default Template" is not a valid choice.

I would like to rename "Default Template" to something like "-- Select Template --".

Searching the WordPress codebase I found references to "Default Template" are hard-coded, in wp-admin/includes/meta-boxes.php (line 588) and wp-admin/includes/class-wp-posts-list-table.php (line 882). Does this mean there is currently no way to change this, without altering the core code (something I want to avoid)?

4
  • Can you explain why "Default Template" is not a valid choice? Apr 12, 2012 at 13:09
  • Because I'm using Advanced Custom Fields and depending on the theme you select a completely different set of fields appears, there is no one we could set as "Default" and be happy with.
    – Shaun
    Apr 12, 2012 at 16:29
  • But something has to be the default. In WordPress, static Pages must have a Template, and will always fall back to page.php and then to index.php. Apr 12, 2012 at 18:15
  • It could be done with jquery/js i guess, if you really want to enforce it, naturally though a user could bypass the restriction by disabling js or blocking the script file that enforces a template choice.
    – t31os
    Apr 13, 2013 at 19:18

4 Answers 4

9

There is a filter for this since version 4.1; cf. https://github.com/WordPress/WordPress/commit/7cdbac53e8497b346d1009375d36586fb6e5197c

You can now use:

add_filter('default_page_template_title', function() {
    return __('My default template name', 'your_text_domain');
});
0
4

I agree with the answer that is given by @TravisPflanz but if you only want to change the name that is provided by the WordPress core you can use the following filter.

function yourprefix_filter_gettext( $translation, $text, $domain ) {
    if ( $text == 'Default Template' ) {
        return __( 'Detail Page', 'your-theme-or-plugin-textdomain' );
    }
    return $translation;
}
add_filter( 'gettext', 'yourprefix_filter_gettext', 10, 3 );    

This replaces the default core translation with one of your own.

Please rename yourprefix and your-theme-or-plugin-textdomain to your configuration

1
  • 1
    Some notes: strict (===) checks are faster. And you should remove the action when you found the string. This filter runs several thousand times during each request and has a big impact on performance.
    – kaiser
    Feb 25, 2014 at 11:43
2

Your request, if I'm not mistaken, is to create an empty selection as the default for the page template drop-down, then force a user to select a custom page template.

This may be an over-simplified answer, but you should create a default template that is a valid choice, then offer other templates to supplement the "Default Template". Just make default the most basic possible page layout. This will eliminate confusion for the user as well. Just determine which will be the most common page layout and make it the "Default".

As Chip points out below, the "Default Template" always exists, even if you simply try to change the name using the traditional template naming methods. If you change the template name of page.php, you will simply be given the options of "Default Template" and the "New & Cool Page Template".

2
  • The "Default Template" always exists, as defined by the WordPress Template Hierarchy for static Pages. Apr 12, 2012 at 14:08
  • OK thanks. As I commented above, we really need them to choose a theme as there is no good style we could automatically set as "Default" and be happy with. We will have to leave it how it is, and tell them to change from "Default Theme" to any other.
    – Shaun
    Apr 12, 2012 at 16:31
0

I hit problems with using a new page.php in the child theme to define a different default template, so instead I using an

add_action( 'after_setup_theme'...

to setup a global option using

update_option ('default_template','page-content.php');

I then used

function replace_page_attributes_metaboxes() {
    add_meta_box('post-parent', 'Page attributes', 'attributes_meta_box', 
             'page', 'side', 'high');
    remove_meta_box('pageparentdiv', 'page', 'side'); 
} 
add_action( 'admin_menu' , 'replace_page_attributes_metaboxes' );

to delete the standard Page Attributes metabox that is used to define the Page Template and replace it with a copy of the code (see http://wpseek.com/page_attributes_meta_box/). I replaced the section of the code that finds the page template name with:

if ( ($post->post_type == 'page')  && (count( get_page_templates() ) != 0) ) {
    if (!empty($post->page_template))
        $template = $post->page_template;
    else
        $template =     get_option ('default_template');
}

if no option is found $template is set to false, which makes the underlying code pick up the standard default template.

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.