Remove buttons from the WordPress Rich Text Editor toolbar

A simple task for someone up to his shoulders in WordPress code on a weekly basis, one would guess. And as the WordPress codex suggests, there is an easy filter available where you can use unset to remove the undesired buttons by name from an array.

The arrays for the different (advanced) toolbars can be found in class-wp-editor.php (just hit Ctrl+F and enter “mce_buttons_”) about half way down the page. In the following example, I try to remove buttons from the advanced toolbar which are defined by default as

array( 'formatselect', 'underline', 'justifyfull', 'forecolor', 'pastetext', 'pasteword', 'removeformat', 'charmap', 'outdent', 'indent', 'undo', 'redo', 'wp_help' )

The WordPress Codex page on MCE Toolbar filters gave me (NOTE: It won’t anymore, I updated the codex page) this example:

function myplugin_tinymce_buttons($buttons) {
      //Remove the text color selector
      unset($buttons['forecolor']);

      return $buttons;
}
add_filter('mce_buttons_2','myplugin_tinymce_buttons')

But no. This simple example does not cut it. It simply had NO effect 🙁

After a long hard search, I found that unset() needs to reference an array value by its key while in the example the value ‘forecolor’ is used. So unset($buttons[‘forecolor’]) does not do anything while unset($buttons[3]) (3 is the key for the 4th value in the default array) would work fine. But it’s no fun like that and if the order of buttons would ever change, this solution would remove another button.

This example, using array_search() to get the appropriate key would solve that issue:

function myplugin_tinymce_buttons($buttons) {
	//Remove the text color selector
	if ( ( $key = array_search('forecolor',$buttons) ) !== false )
		unset($buttons[$key]);

	return $buttons;
}
add_filter('mce_buttons_2','myplugin_tinymce_buttons');

But here is a simpler example that will work, while also allowing other buttons to be removed at the same time. Just add their name — see the default array above — to the $remove array.

function myplugin_tinymce_buttons($buttons) {
	//Remove the format dropdown select and text color selector
	$remove = array('formatselect','forecolor');

	return array_diff($buttons,$remove);;
}
add_filter('mce_buttons_2','myplugin_tinymce_buttons');

By the way, if you need to add a new button or element, use for example array_unshift($buttons,’styleselect’); to place it at the start of the toolbar or array_push($buttons,’styleselect’); to place it at the end. In both cases, the second argument can also be an array to append multiple elements at the same time.

Have fun!

One thought on “Remove buttons from the WordPress Rich Text Editor toolbar

Leave a Reply