MRW Web Design Simple TinyMCE

Description

When publishing content with a CMS, all formatting should be semantic and promote highly-readable content. Out of the box, WordPress’s TinyMCE configuration does not encourage either.

This plugin creates a single row of buttons containing the following (see also: plugin banner):

“Styleselect,”* Bold, Italic, Add/Edit Link, Break Link, Horizontal Rule (added 1.2.0), Paste as Plain Text,** Remove Styles, Special Characters, Undo, Redo, Help, Distraction Free Mode.

This plugin also provides a simple-yet-powerful filter (see below) for developers to add the ability to apply custom styles with the editor.

* The Styleselect contains Headings 2-4 and Blockquote as well as Strikethrough, Subscript, Superscript, Preformatted, and Code (added in v1.1.0) in an “Other Formats” submenu.

** This plugin pairs deliciously with Paste as Plain Text.

I built this plugin for use on client sites and share it in hopes that others will find it helpful. I’m highly motivated to maintain it since I use it for other people.

However, this is an opinionated plugin. You can read an in-depth reasoning behind the decisions made by this plugin in the post “A WordPress Formatting Manifesto.”

Major feature additions are unlikely and support will be limited to bugs and basic use of the filters.

Filters

The plugin hooks early to the standard mce_buttons, mce_buttons_2, and tiny_mce_before_init filters so that it’s easy to override.

This plugin replaces the “formatselect” with the “styleselect” for its added support of custom CSS styles. There is easy-to-use filter for allowing the application of CSS classes in the editor: mrw_mce_text_style. You can find an example of the filter’s usage on the “Other Notes” tag. See also:

Other Plugins by MRWweb

Compatibility

The plugin requires WordPress 4.1+ because of the new Distraction Free Writing Mode. The plugin should otherwise work well in WordPress 3.9+. The plugin will not work in any earlier versions.

Code Recipes

Below we have examples for adding back a button and adding a new “Text Styles” section for custom styles in the “Format” drop down.

Add the “Insert More Tag” Button

This is the one button that might legitimately be missing from this plugin for a small subset of users. If you need it, use the following snippet in your theme’s functions.php file. (Since the More Tag is used by a theme, the functions.php files is a good place for it.)

/* Add "Insert More Tag" Button in Text Editor After charmap */
add_filter( 'mce_buttons', 'mrw_mce_add_more_tag_button' );
function mrw_mce_add_more_tag_button( $buttons ) {
    $buttons[57] = 'wp_more';
    ksort($buttons);
    return $buttons;
}

Add Custom Styles to “Text Styles” submenu of “Formats” menu

Warning: The following feature is almost completely useless without an accompanying set of CSS rules in an editor-style.css file.

Here’s how the mrw_mce_text_style filter works:

add_filter( 'mrw_mce_text_style', 'mrw_add_text_styles_example' );
/**
 * Example filter to add text styles to TinyMCE filter with Mark's "MRW TinyMCE Mods" plugin
 * 
 * Adds a "Text Styles" submenu to the "Formats" dropdown
 * 
 * This would go in a functions.php file or mu-plugin so you don't have to modify the original plugin.
 * 
 * @param array $styles Contains arrays of style_format arguments to define styles.
 *                      Note: Should be an "array of arrays"
 * 
 * @see tinymce.com/wiki.php/Configuration:style_formats
 * @see tinymce.com/wiki.php/Configuration:formats
 * @see wordpress.stackexchange.com/a/128950/9844
 */
function mrw_add_text_styles_example( $styles ) {
    $new_styles = array(
        /* Inline style that only applies to links */
        array(
            'title' => "Link Button", /* Label in "Formats" menu */
            'selector' => 'a', /* this style can ONLY be applied to existing <a> elements in the selection! */
            'classes' => 'button' /* class to add */
        ),
        /* Inline style applied with a <span> */
        array(
            'title' => "Callout Text",
            'inline' => 'span', /* "inline" key for inline phrasing elements */
            'classes' => 'callout'
        ),
        /* Block style applied to paragraph. Each paragraph in selection gets the classes. */
        array(
            'title' => "Warning Message",
            'block' => 'p', /* "block" key for block-level elements. these don't always behave */
            'classes' => 'message warning'  /* two classes work (space-separated) but can't be undone easily via editor */
        ),
        /* Block style capable of containing other block-level elements */
        array(
            'title' => "Feature Box",
            'block' => 'section', 
            'classes' => 'feature-box',
            'wrapper' => true
        )
    );
    return array_merge( $styles, $new_styles );
}

Screenshots

  • The editor in all its minimal glory. This shows the default set of buttons and styles.

  • "Link Button" is an example of a text style that can be added with the mrw_mce_text_style filter. In this example, it's grayed-out by default since it can only be applied to links!

Installation

  1. Upload /mrwweb-simple-tinymce/ to the /wp-content/plugins/ directory
  2. Activate the plugin through the “Plugins” menu in WordPress
  3. Write away, right away!

- OR –

  1. From your WordPress site’s dashboard, go to Plugins > Add New.
  2. Search for “MRW Web Design Simple TinyMCE.”
  3. The plugin should be the first result. Click “Install.”
  4. Allow the plugin to install, then click “Activate.”
  5. Write away, right away!

FAQ

There are plenty of questions but none asked yet. Will update as appropriate.

Contributors & Developers

“MRW Web Design Simple TinyMCE” is open source software. The following people have contributed to this plugin.

Contributors

Changelog

1.2.0 (Nov 13, 2016)

  • Update “Heading” list to emphasize implied hierarchy. (See #38049 for background and potential core inclusion.)
  • Add horizontal rule button.
  • Rename “Text Styles” to “Custom Styles” for clarity.
  • Remove Indent/Outdent buttons. (Use TAB / SHIFT + TAB for list indentation.)
  • Remove code and pre from “Other Formats”. (Use backticks for code.)
  • Update textdomain to support language packs.
  • Escape all translations

1.1.0 (Mar 13, 2015)

  • Hide wp_help button on mobile just like WordPress 4.2 (#31161).
  • Add code element to available “Other Formats.”

1.0.5 (Feb 13, 2015)

  • Improved code formatting thanks to @robneu!
  • Add keys to the $buttons array filtered by mce_buttons for more intuitive button insertion.
  • Example of above and working with “Text Styles” submenu added as “Code Recipes” in “Other Notes”.
  • [Breaking Change] Remove mrw_mce_style_formats filter. It was a stupid idea and I doubt anyone’s used it yet.
  • Fixed PNG mime-types for files used by repository in /assets/

1.0.4 (Feb 6, 2015)

  • Cleaned up and submitted to the repository.
  • Renamed “MRW Web Design Simple TinyMCE”
  • New readme, screenshots, etc.
  • Feb 12, 2015: No version update, but revised screenshots and improved readme.

1.0.3 (Jan 5, 2015)

  • Change “fullscreen” to “dfw” for Distraction Free Writing Mode support in 4.1.

1.0.2 (Sept 16, 2014)

  • Fix “Header” to “Heading.” D’oh!

1.0.1 (May 9, 2014)

  • [Fix] Fix Help Icon

1.0 (May 5, 2014)

  • Initial release