admin管理员组文章数量:1122846
So I have a simple Wordpress plugin that just allows me to use custom page templates. Here's the code:
my-special-template.php
<?php
/**
* Plugin Name: My Special Template
* Description: Awesome description.
**/
function my_template_array()
{
$temps = [];
$temps['john.php'] = 'John';
$temps['jack.php'] = 'Jack';
return $temps;
}
function my_template_register($page_templates,$theme,$post)
{
$templates = my_template_array();
foreach ($templates as $tk => $tv)
{
$page_templates[$tk] = $tv;
}
return $page_templates;
}
add_filter('theme_page_templates','my_template_register',10,3);
function my_template_select($template)
{
global $post, $wp_query, $wpdb;
if (isset($post->ID))
{
$page_temp_slug = get_page_template_slug($post->ID);
$templates = my_template_array();
if (isset($templates[$page_temp_slug]))
{
$template = plugin_dir_path(__FILE__).'templates/'.$page_temp_slug;
}
}
//echo '<pre>Preformatted:';print_r($page_temp_slug);echo '</pre>';
return $template;
}
add_filter('template_include', 'my_template_select', 99)
?>
And the templates:
<?php
/* Template Name: Jack */
?>
Jack
<?php
/* Template Name: John */
?>
John
When I use a theme like Hello Elementor, or Simple Persona, the custom templates can be selected from the template dropdown and the plugin works perfectly:
However, if I used a theme like Twenty Twenty Four that comes pre-installed with WordPress, then I get this:
I can't actually select my plugin templates. What gives? How can I fix this? My WordPress is 6.6.1, or the latest one at this moment.
So I have a simple Wordpress plugin that just allows me to use custom page templates. Here's the code:
my-special-template.php
<?php
/**
* Plugin Name: My Special Template
* Description: Awesome description.
**/
function my_template_array()
{
$temps = [];
$temps['john.php'] = 'John';
$temps['jack.php'] = 'Jack';
return $temps;
}
function my_template_register($page_templates,$theme,$post)
{
$templates = my_template_array();
foreach ($templates as $tk => $tv)
{
$page_templates[$tk] = $tv;
}
return $page_templates;
}
add_filter('theme_page_templates','my_template_register',10,3);
function my_template_select($template)
{
global $post, $wp_query, $wpdb;
if (isset($post->ID))
{
$page_temp_slug = get_page_template_slug($post->ID);
$templates = my_template_array();
if (isset($templates[$page_temp_slug]))
{
$template = plugin_dir_path(__FILE__).'templates/'.$page_temp_slug;
}
}
//echo '<pre>Preformatted:';print_r($page_temp_slug);echo '</pre>';
return $template;
}
add_filter('template_include', 'my_template_select', 99)
?>
And the templates:
<?php
/* Template Name: Jack */
?>
Jack
<?php
/* Template Name: John */
?>
John
When I use a theme like Hello Elementor, or Simple Persona, the custom templates can be selected from the template dropdown and the plugin works perfectly:
However, if I used a theme like Twenty Twenty Four that comes pre-installed with WordPress, then I get this:
I can't actually select my plugin templates. What gives? How can I fix this? My WordPress is 6.6.1, or the latest one at this moment.
Share Improve this question edited Sep 5, 2024 at 18:27 darkhorse asked Sep 5, 2024 at 17:04 darkhorsedarkhorse 1275 bronze badges1 Answer
Reset to default 2Newer themes including Twenty Twenty Four use block themes/Full Site Editing. This means all the templates are created with HTML and theme.json, not using PHP templates at all anymore.
You can read up on bloc themes, and see ways to create a "hybrid theme" which uses both PHP and HTML, here - https://fullsiteediting.com/courses/full-site-editing-for-theme-developers/
本文标签: phpCustom page template from plugin does not work with preinstalled themes in WordPress 661
版权声明:本文标题:php - Custom page template from plugin does not work with pre-installed themes in WordPress 6.6.1 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736295212a1929523.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论