WordPress.org

Ready to get started?Download WordPress

Forums

How to register new page template with a plugin? (5 posts)

  1. hogo
    Member
    Posted 1 year ago #

    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?

  2. Andrew Bartel
    Member
    Posted 1 year ago #

  3. hogo
    Member
    Posted 1 year ago #

    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?

  4. ak.singla
    Member
    Posted 1 year ago #

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

  5. hogo
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic