WordPress

How to set your own custom template page as default selected page in WordPress

0
658
2018 / 09 / 27

If you wish to set your own custom template page as default selected WordPress template page, use the snippet below:

// set default template as your own custom page (it will select page autmatically)
add_action('admin_init', 'set_page_templates');
function set_page_templates()
{
    foreach (get_posts('post_type=page&posts_per_page=-1') as $page) {
        $current_template = get_post_meta($page->ID, '_wp_page_template', true);
        $new_template = 'custom-page.php';

        if ($current_template != $new_template)
            update_post_meta($page->ID, '_wp_page_template', $new_template);
    }
}

Here the custom-page.php is the template file which you have created. You can set the file name here.

Whenever you create a new page, that page will be selected automatically by default.

Hi, I am a full-stack web developer with 5+ years of experience in working with different web technologies. Do you want to ask something? just send me a message through the contact form. Please visit my portfolio at hamzamehmood.com. Thanks