2

I've got three page templates called "Simple", "Front", and "Form" and I would like to add a shortcut in the admin side bar to directly create a page that would be associated with any of these templates. So below "Add new" there would be:

enter image description here

  • Add new Simple Page
  • Add new Front Page
  • Add new Form Page

I've searched for it on Google but can't find any information. Is it possible to do this in WordPress?

3
  • you can create a custom templates and use that templates while creating the page. Commented Nov 2, 2016 at 10:27
  • What @phpuser said + when you click on "Add new Front Page" you send $_GET param and make changes on post editor page so "Fron" template is automatically selected. But be careful and read few articles on changing admin panel before you do it, so all your work doesn't go up in smoke on next wordpress update. Commented Nov 2, 2016 at 10:37
  • This question would be awesome for WordPress Development
    – Stephen
    Commented Nov 2, 2016 at 10:37

2 Answers 2

2

I found this question very interesting ,So here is what i have done to make this possible.

Fetch page template and add them as sub menu of Post type page.

function addTemplateAddNewSubMenu() {

    global $submenu;

    // here we are fetching all page template from current activated theme.
    $templates = wp_get_theme()->get_page_templates( 'page' );

    foreach ( $templates as $filename => $title ) {

        if ( $filename != 'default' && $filename != '' ) {

                // add page-template filename as query string to add new page link.
                $url = 'post-new.php?post_type=page&template=' . $filename;

                $submenu['edit.php?post_type=page'][] = array( 'Add new ' . $title , 'manage_options', $url );
        }
    }
}

add_action( 'admin_menu', 'addTemplateAddNewSubMenu' );

I have added page template as query string to

/wp-admin/post-new.php?post_type=page&template=template-contact.php

Making page template dropdown selected by jQuery and template Query string.

add_action( 'admin_head','selectPageTemplate' );

function selectPageTemplate() {

    global $pagenow;

    if ( $pagenow == 'post-new.php' ) {

        if ( get_post_type() == 'page' && isset($_GET['template']) ) {

            $template = $_GET['template']; ?>

            <script>
                jQuery(function($){
                    $('#page_template').val('<?php echo $template;?>');
                });
            </script>

            <?php
        }
    }

}

enter image description here

0

Here's an example how to set the page template, for new pages, through the custom wpse_page_template GET parameter:

https://examle.tld/wp-admin/post-new.php?post_type=page&wpse_page_template=tpl-test.php

We can hook into the save_post_page hook and target the auto-draft status:

add_action( 'save_post_page', function (  $post_ID, $post, $update )
{
    // Target 'auto-draft' status only
    if ( 'auto-draft' !== get_post_status( $post ) )
        return $post_ID;

    // User input
    $input_tpl = filter_input( INPUT_GET, 'wpse_page_template', FILTER_SANITIZE_STRING );
    $input_tpl = sanitize_file_name( $input_tpl );

    // Get current page templates
    $page_templates = wp_get_theme()->get_page_templates( $post );

    // Nothing to do if user tries a non a valid page template
    if ( ! isset( $page_templates[$input_tpl] ) )
        return $post_ID;

    // Update the user defined page template
    update_post_meta( $post_ID, '_wp_page_template', $input_tpl );

    return $post_ID;

}, 10, 3 );

This approach could be extended for custom post types in WordPress 4.7+.

Your Answer

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

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