Using WordPress ‘before_wp_tiny_mce’ PHP action

The before_wp_tiny_mce WordPress PHP filter allows you to modify the TinyMCE settings array before it is printed.

Usage

add_filter('before_wp_tiny_mce', 'your_custom_function');
function your_custom_function($mce_settings) {
  // your custom code here
  return $mce_settings;
}

Parameters

  • $mce_settings (array) – The TinyMCE settings array to be modified.

More information

See WordPress Developer Resources: before_wp_tiny_mce

Examples

Add custom style formats to TinyMCE dropdown

This example adds custom style formats to the TinyMCE “Formats” dropdown.

add_filter('before_wp_tiny_mce', 'add_custom_style_formats');
function add_custom_style_formats($mce_settings) {
  $style_formats = array(
    array(
      'title' => 'Button',
      'selector' => 'a',
      'classes' => 'button'
    ),
    array(
      'title' => 'Lead',
      'selector' => 'p',
      'classes' => 'lead'
    )
  );
  $mce_settings['style_formats'] = json_encode($style_formats);
  return $mce_settings;
}

Enable paste as plain text by default

Enable “Paste as Plain Text” option by default when pasting content into the editor.

add_filter('before_wp_tiny_mce', 'enable_paste_as_plain_text');
function enable_paste_as_plain_text($mce_settings) {
  $mce_settings['paste_as_text'] = true;
  return $mce_settings;
}

Remove specific buttons from the toolbar

This example removes the “Align center” and “Underline” buttons from the toolbar.

add_filter('before_wp_tiny_mce', 'remove_toolbar_buttons');
function remove_toolbar_buttons($mce_settings) {
  $mce_settings['toolbar1'] = str_replace(',aligncenter,', ',', $mce_settings['toolbar1']);
  $mce_settings['toolbar1'] = str_replace(',underline,', ',', $mce_settings['toolbar1']);
  return $mce_settings;
}

Set custom font sizes

Set custom font sizes in the “Font Sizes” dropdown menu.

add_filter('before_wp_tiny_mce', 'set_custom_font_sizes');
function set_custom_font_sizes($mce_settings) {
  $mce_settings['fontsize_formats'] = '12px 14px 16px 18px 24px 30px 36px 48px';
  return $mce_settings;
}

Disable the “Add Media” button

Disable the “Add Media” button above the editor.

add_filter('before_wp_tiny_mce', 'disable_add_media_button');
function disable_add_media_button($mce_settings) {
  $mce_settings['media_buttons'] = false;
  return $mce_settings;
}