I successfully added a TinyMCE stylesheet using add_editor_style() so that I can preview the styles in the body of the TinyMCE editor.
However, am I right in assuming that the add_editor_style() function can only access style for the body of the editor?
If so, is there another method or function that I can use to access the styling of the TinyMCE Format dropdown as well?
You can not enhance the drop-down list
formatselect
. But you can enhance with the hooktiny_mce_before_init
the second drop downstyleselect
, there is to hide in a default WordPress install.The documentation list all defaults and possibilities.
feature when used. This is needed for some CSS inheritance issues
such as text-decoration for underline/strike though.
elements. For example, a div wrapper or blockquote.
The Style Button
Note that, by default, the Style dropdown is hidden on WordPress. At first add the button for custom formats to a menu bar of TinyMCE, in example line 2 with hook
mce_buttons_2
.The Custom Styles
Then enhance the drop-down of this button. Also useful the enhancement via additional value in the array, see the codex for this example.
Result
Enhanced Drop down menu
You can also enhance the drop-down with a tree menu. Create the var from the example source above with more structure in the array, like the follow source.
For more possibilities and parameters, see the default values of the Style Format drop-down field (writing in JavaScript).
Add Custom Stylesheet to the editor
Now is the last point, that you include the custom css for this formats, via hook
mce_css
.Don’t forget to add this stylesheet rules also to the front end stylesheet.
Remove the Format Button
As enhancement you can remove the
formatselect
drop down button via check for the value in the button array. Add the follow source to the functionfb_mce_editor_buttons
at the hookmce_buttons_2
.Per this answer, the key to getting the styles to appear in the dropdown is to
unset($settings['preview_styles']);
Very helpful and thanks for
defaultstyles
pointers. I found merging arrays wasn’t working until converting those default options to valid JSON (not valid JavaScript). Below allows appending the WordPress tinymce’s drop-down instead of replacingDefault options (JSON):
In functions.php return the larger options hash: