Alex
Alex

Reputation: 29

Adding font sizes to TinyMCE Advanced

I am using Wordpress 4.9.8 and TinyMCE Advanced 4.8.0

I don't seem to find the answer to this anywhere so appologies in advance if I have missed something...

I want to add extra font sizes to the dropdown menu in the editor. I managed to do so by selecting the 'Font sizes' option in the plugins settings and by adding the options to the sizes I need to the '/wp-content/plugins/tinymce-advanced/tinymce-advanced.php' file as follows:

private $fontsize_formats = '8px 10px 12px 13px 14px 16px 20px 24px 28px 32px 36px';

This works great but has changed the default font size to 16px (it should be 12px)

Is there another way to do this?

Thanks

Upvotes: 1

Views: 2639

Answers (1)

Howdy_McGee
Howdy_McGee

Reputation: 10643

Check out my answer previously on WordPress Stack Exchange. I will port it over here for transparency.


This is kind of a 2-parter. The first half will show you how to change the style inside TinyMCE when editing. The second half will show you how to remove things from the toolbar.

Style TinyMCE

WordPress give us a neat little function called add_editor_style() which accepts an array of stylesheets, either by URL or relative paths. WordPress default themes usually take advantage of this function and can be seen in the latest TwentySeventeen theme. First let's create a stylesheet. The name doesn't matter but the location does.

body,
button,
input,
select,
textarea {
    font-size: 16px;
}

For simplicity we'll call this editor-style.css and save it in the theme:

/assets/css/editor-style.css

Next we need to tell WordPress to use our stylesheet so we'll open the themes functions.php file and add in:

/**
 * Theme setup functionality
 *
 * @return void
 */
function prefix_theme_setup() {

    // Relative path to the TinyMCE Stylesheet
    add_editor_style( array( 'assets/css/editor-style.css' ) );

}
add_action( 'after_setup_theme', 'iqt_theme_setup' );

Some plugins could interfere with this such as page builders if they implement their own TinyMCE.


Modify Toolbar

Next, we can use the tiny_mce_before_init filter hook to modify the TinyMCE. In this case, all we need to do if override the font sizes. You can add the following function into your functions.php file:

/**
 * Add Formats to TinyMCE
 * - https://developer.wordpress.org/reference/hooks/tiny_mce_before_init/
 * - https://codex.wordpress.org/Plugin_API/Filter_Reference/tiny_mce_before_init
 *
 * @param array $args   - Arguments used to initialize the tinyMCE
 *
 * @return array $args  - Modified arguments
 */
function prefix_tinymce_toolbar( $args ) {

    $args['fontsize_formats'] = "8px 10px 12px 13px 14px 16px 20px 24px 28px 32px 36px";

    return $args;

}
add_filter( 'tiny_mce_before_init', 'prefix_tinymce_toolbar' );

The $args array has an index that accepts a list of font sizes separated by spaces. You can change these to whatever you want, px, em, rem it doesn't matter, just that the list is separated by a space and is a valid font-size value.

Upvotes: 1

Related Questions