0

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:

enter image description here

However, if I used a theme like Twenty Twenty Four that comes pre-installed with WordPress, then I get this:

enter image description here

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.

1 Answer 1

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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.