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

求可添加到functions.php的代码:禁用特定CPT经典编辑器仅启用区块编辑器

特定自定义文章类型强制用区块编辑器,其余保留经典编辑器的实现代码

直接上可复用的PHP代码,添加到你的主题(或子主题)的functions.php文件即可:

// 控制区块编辑器的启用:仅目标CPT允许使用
add_filter( 'use_block_editor_for_post_type', 'force_block_editor_for_specific_cpt', 10, 2 );
function force_block_editor_for_specific_cpt( $use_block_editor, $post_type ) {
    // 替换成你需要强制用区块编辑器的CPT的slug
    $target_cpt = 'your_custom_post_type';
    
    if ( $post_type === $target_cpt ) {
        return true; // 目标CPT启用区块编辑器
    }
    return false; // 其他所有内容类型禁用区块编辑器(保留经典)
}

// 控制经典编辑器的启用:仅目标CPT禁用经典编辑器
add_filter( 'classic_editor_enabled_for_post_type', 'disable_classic_editor_for_specific_cpt', 10, 2 );
function disable_classic_editor_for_specific_cpt( $enabled, $post_type ) {
    $target_cpt = 'your_custom_post_type'; // 和上面保持一致
    
    if ( $post_type === $target_cpt ) {
        return false; // 目标CPT禁用经典编辑器
    }
    return true; // 其他所有内容类型保留经典编辑器
}

关键说明

  1. 替换CPT Slug:把代码里的'your_custom_post_type'替换成你实际的自定义文章类型slug(比如注册CPT时的第一个参数,或者后台编辑页面URL里post_type=xxx中的xxx)。
  2. 钩子逻辑:
    • use_block_editor_for_post_type钩子决定是否允许使用区块编辑器,我们只给目标CPT开绿灯。
    • classic_editor_enabled_for_post_type钩子决定是否允许使用经典编辑器,我们给目标CPT关上门,确保只能用区块。
  3. 注意事项:
    • 建议用子主题的functions.php,避免主题更新时代码被覆盖。
    • 修改前记得备份原文件,防止语法错误导致网站崩溃。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 00:40:23