Implementing a WYSIWYG Editor in a WordPress Admin Page
A custom WordPress admin page often starts with a plain <textarea>, but that quickly becomes inconvenient when administrators need headings, links, lists, images, or formatted instructions. A What You See Is What You Get editor gives users a familiar editing experience while keeping the content inside the WordPress dashboard.
TinyMCE is a practical choice for this task because WordPress already includes its integration through the wp_editor() function. You can add the editor to a settings page, custom post management screen, WooCommerce workflow, or an internal content tool without loading a separate JavaScript editor library.
The implementation still needs the same care as any other WordPress feature. Access must be restricted, form submissions need nonce verification, saved HTML should be sanitised, and editor settings must be compatible with the WordPress version in use. The following approach works well for Australian businesses managing store notices, service pages, suburb-specific information, and customer-facing content.
| Approach | Editing experience | WordPress integration | Best use |
|---|---|---|---|
| Plain textarea | Basic text only | Very simple | Short values and technical settings |
wp_editor() with TinyMCE |
Rich text with WordPress controls | Built in | Admin content, notices, descriptions |
| Custom TinyMCE JavaScript setup | Highly configurable | Requires more maintenance | Specialised editorial interfaces |
| Gutenberg components | Block-based editing | Modern but more complex | Block-oriented content workflows |
Choose the Right Admin Page Architecture
Before adding TinyMCE, decide where the content should live. A single site-wide message might belong in the WordPress Options API, while repeated records such as locations, staff profiles, or product instructions may deserve a custom post type. Storing content in the correct place makes permissions, revisions, exports, and future migrations easier.
For a simple settings page, register an options page with add_options_page() or add_menu_page(). The callback should output the page title, opening form, settings fields, and the editor. Keep the page callback focused on presentation; registration and saving logic are easier to test when placed in separate functions.
This example creates a settings page for a formatted service notice:
add_action( 'admin_menu', 'acme_register_notice_page' );
function acme_register_notice_page() {
add_options_page(
'Service Notice',
'Service Notice',
'manage_options',
'acme-service-notice',
'acme_render_notice_page'
);
}
The manage_options capability is suitable for a site-wide setting controlled by administrators. A WooCommerce manager, editor, or custom role may need a different capability. Avoid granting access based solely on whether a user can reach a URL; WordPress must check the capability when the page is registered and when data is saved.
Add TinyMCE with wp_editor
WordPress exposes TinyMCE through wp_editor(). The first argument is the existing value, the second is the HTML element ID, and the third is an array of configuration options. The editor can show the Visual and Text tabs, use a reduced toolbar, and support media buttons where appropriate.
function acme_render_notice_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$notice = get_option( 'acme_service_notice', '' );
?>
<div class="wrap">
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
<form method="post" action="options.php">
<?php
settings_fields( 'acme_notice_settings' );
wp_editor(
$notice,
'acme_service_notice',
array(
'textarea_name' => 'acme_service_notice',
'media_buttons' => false,
'textarea_rows' => 12,
'teeny' => false,
'quicktags' => true,
)
);
submit_button( 'Save Notice' );
?>
</form>
</div>
<?php
}
The textarea_name value is important because it determines the submitted field name. The editor ID should contain only characters that work reliably in HTML and JavaScript identifiers. If the ID and submitted option name do not match your sanitisation callback, the page may appear to save successfully while retaining the old value.
Use media_buttons => true only when administrators genuinely need to insert images or other media. That option introduces the media modal and additional controls. For a small announcement, a compact toolbar is usually clearer. A local café in Melbourne, for example, may need formatted opening-hour notices but not a full image library in the settings page.
Register, Validate, and Save the Content
The Settings API can handle the form action and nonce field, provided the setting is registered correctly. Register the option on admin_init, assign a sanitisation callback, and define the settings group used by settings_fields().
add_action( 'admin_init', 'acme_register_notice_setting' );
function acme_register_notice_setting() {
register_setting(
'acme_notice_settings',
'acme_service_notice',
array(
'type' => 'string',
'sanitize_callback' => 'acme_sanitize_notice',
'default' => '',
)
);
}
function acme_sanitize_notice( $value ) {
if ( ! current_user_can( 'manage_options' ) ) {
return '';
}
return wp_kses_post( $value );
}
wp_kses_post() permits HTML that WordPress normally allows in post content, including common formatting tags and safe attributes. It removes dangerous markup such as scripts and unsupported event handlers. This is generally appropriate for administrator-entered notices that will be displayed on the front end.
If the field is intended to contain only a short fragment, use a stricter sanitiser such as sanitize_textarea_field(). Conversely, do not use sanitize_text_field() for TinyMCE content because it strips markup and defeats the purpose of the visual editor. For a custom list of permitted elements, use wp_kses() with an explicit allowlist.
When displaying saved content in the editor, pass the raw stored value to wp_editor() rather than applying esc_html() first. The editor handles the value for its textarea output. On the front end, choose escaping according to context: wp_kses_post() for permitted formatted HTML, esc_url() for URLs, and esc_attr() for HTML attributes.
Configure the Toolbar and Editor Experience
The default WordPress editor is often enough, but an admin page may need fewer controls. TinyMCE settings can be passed through the third argument to wp_editor(). A compact configuration reduces accidental formatting and helps staff focus on the content they are meant to manage.
$editor_settings = array(
'textarea_name' => 'acme_service_notice',
'textarea_rows' => 10,
'media_buttons' => false,
'tinymce' => array(
'toolbar1' => 'formatselect,bold,italic,bullist,numlist,link,unlink',
'toolbar2' => '',
'menubar' => false,
),
'quicktags' => array(
'buttons' => 'strong,em,link,ul,ol,li,close',
),
);
wp_editor( $notice, 'acme_service_notice', $editor_settings );
TinyMCE configuration keys can vary as WordPress updates its bundled editor integration, so test the toolbar after WordPress core updates. Avoid depending on undocumented internal JavaScript objects when the PHP configuration is sufficient. If you need custom buttons, register them through the documented TinyMCE filters and load their scripts only on the relevant admin screen.
A visual editor should also remain usable for people who prefer keyboard navigation or HTML editing. Keep the Text tab available unless there is a strong reason to remove it, use a meaningful label, and avoid relying on colour alone to communicate status. Check the page at common Australian screen widths, including laptop displays used in offices and smaller devices used by staff working between Sydney branches or regional sites.
Settings Worth Reviewing
textarea_rowscontrols the initial editor height.media_buttonsdetermines whether the Add Media button appears.teenyenables a simplified toolbar for small fields.quicktagscontrols the HTML editing buttons.
Handle Assets, Themes, and Dynamic Content
wp_editor() loads the required editor assets when it is rendered in the right admin context. Do not enqueue TinyMCE globally across every dashboard screen. Extra scripts can slow the admin area and create conflicts with WooCommerce, page builders, or other plugins.
If custom CSS is needed, enqueue it only for the page created by your menu callback. The admin_enqueue_scripts hook provides the current screen ID:
add_action( 'admin_enqueue_scripts', 'acme_notice_admin_assets' );
function acme_notice_admin_assets( $hook ) {
if ( 'settings_page_acme-service-notice' !== $hook ) {
return;
}
wp_enqueue_style(
'acme-notice-admin',
plugin_dir_url( __FILE__ ) . 'assets/admin-notice.css',
array(),
'1.0.0'
);
}
Avoid styling TinyMCE by targeting broad selectors such as .mce-content-body across the whole dashboard. Scope custom rules to your editor where possible. The iframe used by the Visual editor has its own document, so front-end styles do not automatically appear inside it. The content_css TinyMCE setting can load a stylesheet, but that stylesheet should be maintained carefully and tested against the active theme.
Dynamic editor fields require extra attention. If an administrator adds repeatable rows with JavaScript, each new editor needs a unique ID and a distinct textarea_name. Initialising multiple TinyMCE instances with duplicate IDs commonly causes missing content, broken toolbar buttons, or data being submitted from the wrong field. For complex repeaters, initialise editors after the row is added and remove the instance before a row is deleted.
Practical Testing Checks
- Open the page with a user who lacks the required capability.
- Save headings, links, lists, and accented Australian place names.
- Switch between Visual and Text modes before submitting.
- Test the output on mobile and in the site’s active theme.
Secure and Maintainable Production Use
Security checks should cover both access and output. The Settings API supplies a nonce through settings_fields(), but a custom form using admin-post.php needs its own wp_nonce_field() and check_admin_referer() call. Always verify the capability before processing data, not only when rendering the form.
If the editor accepts links, remember that permitted HTML does not make every destination desirable. You may wish to restrict allowed protocols, remove inline styles, or review content before publication. For customer-facing content, test links, images, and embedded markup in a staging copy before deploying to a live store or service website.
Browser testing matters because the admin page may be used on different machines and networks. A Perth office, a Brisbane warehouse, and a staff member working remotely may all have different browser versions, cached scripts, and security software. Test in current Chrome, Firefox, Safari, and Edge, and clear caches after changing editor configuration. Resources such as desktop app resources can also be useful when checking how staff move between browser-based administration and other desktop workflows, though the WordPress editor itself should remain the source of truth for saved content.
TinyMCE is appropriate when the requirement is controlled rich text inside an admin form. It may be unnecessary for a single-line option, while Gutenberg is a better fit when users need reusable blocks, columns, or block-specific controls. For many Australian WooCommerce shops, however, a focused wp_editor() field is easier for staff to understand than a complete block editor interface.
Once the editor is working, document the field’s purpose and permitted content for administrators. A short note can prevent users from pasting complex email HTML, inserting oversized images, or adding layout markup that conflicts with the active theme. Keep the implementation close to WordPress APIs, sanitise on save, escape on output, and review it after major core or plugin updates.