WordPress.org

Support

Support » How-To and Troubleshooting » How to register new page template with a plugin?

How to register new page template with a plugin?

  • Hi,

    I am writing a plugin and I need to add a new page themplate that can be chosen when adding new page. I have read around dozen of topics and I came up with this code:

    add_filter('template_include', 'swpd_login_template');
    function swpd_login_template( $page_template )
    {
        $page_template =  plugin_dir_path( __FILE__ ) . 'page-templates/login-template.php';
        return $page_template;
    }

    But it does not work. Does anyone has any idea what is wrong and how to fix this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Andrew Bartel
    Member

    @andrew-bartel

    Tried is as well. Here how I it looks:

    add_action("template_redirect", 'my_theme_redirect');
    
    function my_theme_redirect() {
        global $wp;
        $plugindir = dirname( __FILE__ );
    
        //A Specific Custom Post Type
        if ($wp->query_vars["post_type"] == 'page') {
            $templatefilename = 'login-template.php';
            if (file_exists(TEMPLATEPATH . '/' . $templatefilename)) {
                $return_template = TEMPLATEPATH . '/' . $templatefilename;
            } else {
                $return_template = $plugindir . '/page-templates/' . $templatefilename;
            }
            do_theme_redirect($return_template);
         }
    }
    
    function do_theme_redirect($url) {
        global $post, $wp_query;
        if (have_posts()) {
            include($url);
            die();
        } else {
            $wp_query->is_404 = true;
        }
    }

    I have really spent hours to solve it and nothing works 🙁 Do I have a mistake in any of above codes, or does someone have any other idea?

    Ankit Singla
    Participant

    @aksingla

    I am not much sure, but does that “die();” statement required??

    This is how it was shown in the example, deleting it does not help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to register new page template with a plugin?’ is closed to new replies.