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:
- 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?
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
}
}
}