You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress插件自定义页面模板无法适配Twenty Twenty Four等预装主题

问题说明

我开发了一款简易WordPress插件,用于启用自定义页面模板,核心代码如下:

插件主文件 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)

?>

模板文件代码

templates/jack.php

<?php
/* Template Name: Jack */
?>
Jack

templates/john.php

<?php
/* Template Name: John */
?>
John

该插件在Hello Elementor、Simple Persona等传统主题中可正常从模板下拉菜单选择自定义模板,但切换至Twenty Twenty Four这类WordPress预装块主题时,无法选择插件提供的自定义模板,当前使用WordPress 6.6.1版本。

问题原因

Twenty Twenty Four属于块主题(Block Theme),这类主题采用WordPress站点编辑器机制,页面模板的注册逻辑和传统主题不同。原插件仅使用theme_page_templates钩子,该钩子仅对传统非块主题生效,块主题需要通过block_theme_page_templates钩子来注册自定义模板。

解决方案

修改插件的模板注册逻辑,同时兼容传统主题和块主题,具体修改如下:

  1. 保留原有的theme_page_templates钩子注册,新增block_theme_page_templates钩子的回调函数
  2. 适配两个钩子的参数差异(block_theme_page_templates仅接收$templates和$post两个参数)

修改后的完整插件代码:

<?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_block_template_register($templates, $post)
{
    $custom_templates = my_template_array();
    foreach ($custom_templates as $tk => $tv)
    {
        $templates[$tk] = $tv; 
    }
    return $templates;
}
add_filter('block_theme_page_templates','my_block_template_register',10,2);

function my_template_select($template)
{
    global $post;

    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;
        }
    }

    return $template;
}
add_filter('template_include', 'my_template_select', 99);

?>
验证修改

修改后重新激活插件,切换到Twenty Twenty Four主题,进入页面编辑界面,即可在模板下拉菜单中看到插件提供的John和Jack模板,选择后可正常生效。

内容的提问来源于stack exchange,提问作者darkhorse

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 20:24:50