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

WordPress Gutenberg RichText组件添加文字颜色选择功能失败求助

RichText组件文字颜色选择功能实现方案
  • 问题根源:RichText默认工具栏不包含文字颜色选择控件,仅配置allowedFormats无法调出该功能。
  • 正确实现方式:手动通过toolbarControls添加颜色选择控件,同时保留allowedFormats的配置:
    <RichText
      tagName="h5"
      value={props.attributes.title}
      onChange={handleTitleChange}
      allowedFormats={['core/bold', 'core/text-color']}
      toolbarControls={[
        'bold',
        {
          name: 'text-color',
          title: '文字颜色',
          icon: 'editor-textcolor',
        },
      ]}
    />
    
  • 补充说明:
    • 该配置无需依赖<InspectorControls>,颜色选择器会直接显示在RichText的工具栏中。
    • 若需要自定义颜色选项,可通过add_theme_support('editor-color-palette', [...])在主题或插件中注册颜色预设,否则将使用默认颜色集合。
    • 确保使用的Gutenberg版本支持text-color工具栏控件(WordPress 5.8及以上版本均支持)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 05:21:28