RichText 是一个组件,允许开发者渲染一个可编辑的输入框,为用户提供格式化块内容(如加粗、斜体、链接等)的功能。它专为块编辑器设计,提供一致样式、占位文本和控制格式化选项等内置功能。
import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps, RichText } from '@wordpress/block-editor';
registerBlockType( /* ... */, {
// ...
attributes: {
content: {
type: 'string',
source: 'html',
selector: 'h2',
},
},
edit( { attributes, setAttributes } ) {
const blockProps = useBlockProps();
return (
<RichText
{ ...blockProps }
tagName="h2" // 这里的标签是输出元素,在管理端可编辑
value={ attributes.content } // 任何现有内容,来自数据库或属性默认值
allowedFormats={ [ 'core/bold', 'core/italic' ] } // 允许内容加粗或斜体,但不允许其他格式化选项
onChange={ ( content ) => setAttributes( { content } ) } // 将更新后的内容存储为块属性
placeholder={ __( 'Heading...' ) } // 在用户添加内容前显示此文本
/>
);
},
save( { attributes } ) {
const blockProps = useBlockProps.save();
return <RichText.Content { ...blockProps } tagName="h2" value={ attributes.content } />; // 将 <h2>编辑器添加的内容...</h2> 保存到数据库以供前端显示
}
} );RichText is a component that allows developers to render a contenteditable input, providing users with the option to format block content to make it bold, italics, linked, or use other formatting.
The RichText component is extremely powerful because it provides built-in functionality you won’t find in other components:
div, h2 or p tag. This allows the styles you apply in style.css to more easily apply on the frontend and admin, without having to rewrite them in editor.css.Unlike other components that exist in the Component Reference section, RichText lives separately because it only makes sense within the block editor, and not within other areas of WordPress.
For a list of the possible properties to pass your RichText component, check out the component documentation on GitHub.
There are a number of core blocks using the RichText component. The JavaScript edit function linked below for each block can be used as a best practice reference while creating your own blocks.
import { registerBlockType } from '@wordpress/blocks';
import { useBlockProps, RichText } from '@wordpress/block-editor';
registerBlockType( /* ... */, {
// ...
attributes: {
content: {
type: 'string',
source: 'html',
selector: 'h2',
},
},
edit( { attributes, setAttributes } ) {
const blockProps = useBlockProps();
return (
<RichText
{ ...blockProps }
tagName="h2" // The tag here is the element output and editable in the admin
value={ attributes.content } // Any existing content, either from the database or an attribute default
allowedFormats={ [ 'core/bold', 'core/italic' ] } // Allow the content to be made bold or italic, but do not allow other formatting options
onChange={ ( content ) => setAttributes( { content } ) } // Store updated content as a block attribute
placeholder={ __( 'Heading...' ) } // Display this text before any content has been added by the user
/>
);
},
save( { attributes } ) {
const blockProps = useBlockProps.save();
return <RichText.Content { ...blockProps } tagName="h2" value={ attributes.content } />; // Saves <h2>Content added in the editor...</h2> to the database for frontend display
}
} );
While using the RichText component a number of common issues tend to appear.
If the HTML tags from text formatting such as <strong> or <em> are being escaped and displayed on the frontend of the site, this is likely due to an issue in your save function. Make sure your code looks something like <RichText.Content tagName="h2" value={ heading } /> (JSX) within your save function instead of simply outputting the value with <h2>{ heading }</h2>.
Before moving forward, consider if using the RichText component makes sense at all. Would it be better to use a basic input or textarea element? If you don’t think any formatting should be possible, these HTML tags may make more sense.
If you’d still like to use RichText, you can eliminate all of the formatting options by specifying the withoutInteractiveFormatting property.
If you want to limit the formats allowed, you can specify using allowedFormats property in your code, see the example above or the component documentation for details.
The RichText component uses formats to display inline elements, for example images within the paragraph block. If you just want to disable a format from the editor, you can use the unregisterFormatType function. For example to disable inline images, use:
wp.richText.unregisterFormatType( 'core/image' );
To apply, you would need to enqueue the above script in your plugin or theme. See the JavaScript tutorial for how to load JavaScript in WordPress.