Add a rich text editor to admin pages with wp_editor()
wp_editor() prints the classic WordPress editor, TinyMCE with the Visual and Text tabs, anywhere in the admin: on a plugin settings page, in a meta box, on a term edit screen. Users get the toolbar they know, and you get HTML that can be saved like any other field. It is the right tool for formatted text outside the main content: an email template, a notice text, a description of a custom field.
Problem: A plugin settings page needs a formatted email template, and administrators should not have to type HTML.
Solution: Call wp_editor( $content, $editor_id, $settings ) inside the form, set the field name with textarea_name, choose the toolbar in tinymce or use teeny, and save the value through wp_kses_post().
On a settings page
<?php
function ha_render_email_template_field() {
wp_editor(
get_option( 'ha_email_template', '' ),
'ha_email_template', // ID: lowercase letters and underscores only
array(
'textarea_name' => 'ha_email_template',
'textarea_rows' => 12,
'media_buttons' => false, // no "Add Media" button
'tinymce' => array(
'toolbar1' => 'bold,italic,underline,bullist,numlist,link,unlink,undo,redo',
'toolbar2' => '',
),
'quicktags' => true, // the Text tab
)
);
}
register_setting( 'ha_settings', 'ha_email_template', array( 'sanitize_callback' => 'wp_kses_post' ) );
The editor ID becomes the ID of the textarea and the name of the TinyMCE instance. It may contain only lowercase letters and underscores; hyphens or square brackets break the editor’s JavaScript.
In a meta box
<?php
add_action( 'add_meta_boxes', function () {
add_meta_box( 'ha_sidebar_content', __( 'Sidebar content', 'textdomain' ), 'ha_sidebar_content_box', 'page' );
} );
function ha_sidebar_content_box( $post ) {
wp_nonce_field( 'ha_sidebar_content', 'ha_sidebar_content_nonce' );
wp_editor( get_post_meta( $post->ID, '_ha_sidebar_content', true ), 'ha_sidebar_content', array(
'textarea_name' => 'ha_sidebar_content',
'textarea_rows' => 8,
'teeny' => true, // the small toolbar
) );
}
add_action( 'save_post_page', function ( $post_id ) {
if ( empty( $_POST['ha_sidebar_content_nonce'] )
|| ! wp_verify_nonce( $_POST['ha_sidebar_content_nonce'], 'ha_sidebar_content' )
|| ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
|| ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
update_post_meta( $post_id, '_ha_sidebar_content', wp_kses_post( wp_unslash( $_POST['ha_sidebar_content'] ?? '' ) ) );
} );
In the block editor, classic meta boxes still work and are shown under the content, so this also works on sites that use the block editor for the main content.
Showing the content
The saved value is HTML without paragraphs (the editor stores line breaks, not <p> tags). Run it through wpautop() when printing, or apply the full content filters:
<?php echo wp_kses_post( wpautop( get_post_meta( get_the_ID(), '_ha_sidebar_content', true ) ) );
NOTE: wp_editor() prints its initialisation script for editors that are in the page when it loads. An editor added later with JavaScript (in a modal, in a repeater row) is not initialised; for those, WordPress 4.8+ has wp.editor.initialize( id, settings ) in the editor script, which you load with wp_enqueue_editor().