{"id":134434,"date":"2021-03-09T08:38:20","date_gmt":"2021-03-09T08:38:20","guid":{"rendered":"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/richtext\/"},"modified":"2024-10-30T16:55:52","modified_gmt":"2024-10-30T16:55:52","slug":"richtext","status":"publish","type":"blocks-handbook","link":"https:\/\/developer.wordpress.org\/block-editor\/reference-guides\/richtext\/","title":{"rendered":"RichText Reference"},"content":{"rendered":"
RichText is a component that allows developers to render a The RichText component is extremely powerful because it provides built-in functionality you won’t find in other components:<\/p>\n Unlike other components that exist in the Component Reference<\/a> section, RichText lives separately because it only makes sense within the block editor, and not within other areas of WordPress.<\/p>\n For a list of the possible properties to pass your RichText component, check out the component documentation on GitHub<\/a>.<\/p>\n 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.<\/p>\n While using the RichText component a number of common issues tend to appear.<\/p>\n If the HTML tags from text formatting such as Before moving forward, consider if using the RichText component makes sense at all. Would it be better to use a basic If you’d still like to use RichText, you can eliminate all of the formatting options by specifying the If you want to limit the formats allowed, you can specify using 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 contenteditable<\/code> input<\/a>, providing users with the option to format block content to make it bold, italics, linked, or use other formatting.<\/p>\n
\n
div<\/code>,
h2<\/code> or
p<\/code> 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.<\/li>\n
Property reference<\/h2>\n
Core blocks using the RichText component<\/h2>\n
\n
Example<\/h2>\n
import { registerBlockType } from '@wordpress\/blocks';\nimport { useBlockProps, RichText } from '@wordpress\/block-editor';\n\nregisterBlockType( \/* ... *\/, {\n \/\/ ...\n\n attributes: {\n content: {\n type: 'string',\n source: 'html',\n selector: 'h2',\n },\n },\n\n edit( { attributes, setAttributes } ) {\n const blockProps = useBlockProps();\n\n return (\n <RichText\n { ...blockProps }\n tagName=\"h2\" \/\/ The tag here is the element output and editable in the admin\n value={ attributes.content } \/\/ Any existing content, either from the database or an attribute default\n allowedFormats={ [ 'core\/bold', 'core\/italic' ] } \/\/ Allow the content to be made bold or italic, but do not allow other formatting options\n onChange={ ( content ) => setAttributes( { content } ) } \/\/ Store updated content as a block attribute\n placeholder={ __( 'Heading...' ) } \/\/ Display this text before any content has been added by the user\n \/>\n );\n },\n\n save( { attributes } ) {\n const blockProps = useBlockProps.save();\n\n return <RichText.Content { ...blockProps } tagName=\"h2\" value={ attributes.content } \/>; \/\/ Saves <h2>Content added in the editor...<\/h2> to the database for frontend display\n }\n} );\n<\/code><\/pre>\n
Common issues and solutions<\/h2>\n
HTML formatting tags display in the content<\/h3>\n
<strong><\/code> or
<em><\/code> 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 } \/><\/code> (JSX) within your save function instead of simply outputting the value with
<h2>{ heading }<\/h2><\/code>.<\/p>\n
Unwanted formatting options still display<\/h3>\n
input<\/code> or
textarea<\/code> element? If you don’t think any formatting should be possible, these HTML tags may make more sense.<\/p>\n
withoutInteractiveFormatting<\/code> property.<\/p>\n
allowedFormats<\/code> property in your code, see the example above or the component documentation<\/a> for details.<\/p>\n
Disable specific format types in Editor<\/h3>\n
unregisterFormatType<\/code> function. For example to disable inline images, use:<\/p>\n
wp.richText.unregisterFormatType( 'core\/image' );\n<\/code><\/pre>\n