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?
Andrew Bartel
Member
Posted 5 months 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?
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.