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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

Newer 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