本文的内容主要以简化后端为主。

首先我们需要创建一个空主题,随便新建一个文件夹,创建三个文件

文件夹/
├── index.php          # 主文件,用于展示页面内容
├── style.css          # 主题样式表,用于页面的样式控制
└── functions.php      # 功能文件,用于注册主题支持,加载脚本和样式等

index.php 内容:

<?php
// Headless WordPress - no frontend template.
wp_die('This is a headless WordPress site. Frontend rendering is disabled.', 'Headless', ['response' => 404]);

style.css 内容:

/*
Theme Name: Headless_Blank
Description: Minimal theme for Headless WordPress
Version: 1.1
Author: kiFte 这四块内容都随便换的,大致结构符合就行。
*/

functions.php 内容:因为需要禁用一些功能和开启旧版菜单,所以会看起来复杂一点。

<?php
// 拦截所有非 API、非后台请求
add_action('template_redirect', function () {
    $is_api = defined('REST_REQUEST') ||
              strpos($_SERVER['REQUEST_URI'], '/wp-json') !== false ||
              strpos($_SERVER['REQUEST_URI'], '/graphql') !== false;
    if (!is_admin() && !$is_api) {
        wp_die('Headless WordPress. Frontend rendering disabled.', 'Headless', ['response' => 404]);
    }
});

// 禁用头部输出
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rest_output_link_wp_head');
remove_action('template_redirect', 'rest_output_link_header', 11);
remove_action('wp_head', 'wp_shortlink_wp_head');

// 禁止加载主题编辑器样式
add_action('after_setup_theme', function () {
    remove_theme_support('editor-styles');
    remove_theme_support('wp-block-styles');
});

// 启用传统菜单支持(非 Customizer 模式)
add_action('after_setup_theme', function () {
    // 注册菜单位置
    register_nav_menus([
        'primary' => 'Primary Menu',
    ]);
});

// 禁用 block patterns(样板)
remove_theme_support('core-block-patterns');

// 禁用 block template 功能
remove_theme_support('block-templates');

// 注销所有已注册的 patterns(保险)
add_action('init', function () {
    foreach (WP_Block_Patterns_Registry::get_instance()->get_all_registered() as $name => $pattern) {
        unregister_block_pattern($name);
    }
}, 20);

// 彻底拦截 Customizer 页面访问
add_action('load-customize.php', function () {
    wp_die('Customizer is disabled in Headless mode.', 'Headless', ['response' => 403]);
});

// 从后台菜单中移除“自定义”
add_action('after_setup_theme', function () {
    // 移除 WordPress 注入“自定义”菜单的钩子(这是关键)
    remove_action('admin_menu', '_add_themes_utility_last', 101);
});

// 保底移除菜单项(有些主题仍可能注册)
add_action('admin_menu', function () {
    remove_submenu_page('themes.php', 'customize.php');
});

// 最后一层保险:用 CSS 隐藏菜单(针对 JS 注入)
add_action('admin_head', function () {
    echo '<style>#customize-themes { display: none !important; }</style>';
});

// 禁用与 Customizer 相关的主题特性(防止默认加载)
add_action('after_setup_theme', function () {
    remove_theme_support('custom-logo');
    remove_theme_support('custom-background');
    remove_theme_support('custom-header');
    remove_theme_support('customize-selective-refresh-widgets');
    remove_theme_support('editor-styles');
    remove_theme_support('widgets');
});

将这个文件夹打包成 zip,在 word­press 菜单的外观/主题中点击安装新主题,然后点击上传主题,点击选中文件上传后启用即可。

image.png

By kiFte

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注