In a previous post we talked about how to add multiple TinyMCE editors to your WordPress theme or plugin. For this post we’re going to stick with the TinyMCE topic and show you how to add Google Web Fonts to your TinyMCE editor.

The Default TinyMCE Editor

Before we add some Google Web Fonts, let’s first look again at what our default TinyMCE editor looks like:

tinymce-default

All is well and good, but the important thing is what you don’t see: there are no buttons to select a font. Everything else is there for content formatting, but you can’t specify a specific font if you wanted to.

Add Font Selection to TinyMCE

The first thing we need to do is simply get TinyMCE to display the font selection option, which we can accomplish with the following code:

 1: add_filter('mce_buttons', 'add_font_selection_to_tinymce');
 2:
 3: function add_font_selection_to_tinymce($buttons) {
 4:     array_push($buttons, 'fontselect');
 5:     return $buttons;
 6: }

This code shows how we can hook into the mce_buttons filter and push the fontselect option onto our TinyMCE editor, and doing so will give us the font family dropdown list:

tinymce-fontfamily-dropdown

If you click the font family dropdown list you’ll see all of the fonts that are baked into TinyMCE by default, which are: Andale Mono, Arial, Arial Black, Book Antiqua, Comic Sans MS, Courier New, Georgia, Helvetica, Impact, Symbol, Tahoma, Terminal, Times New Roman, Trebuchet MS, Verdana, Webdings, and Wingdings.

Adding Google Web Fonts

For this exercise we’re going to add three Google Web Fonts to our TinyMCE editor: Aclonica, Michroma, and Paytone One. We’re going to do this in a few steps so that it’s very clear how this works.

Step 1: Use the theme_advanced_fonts Option

As mentioned in our previous post, TinyMCE has a bunch of configuration options, and the one we need to add Google Web Fonts is the theme_advanced_fonts option. The code below shows how we invoke a method named load_tiny_mce during the admin_head WordPress hook:

 1: add_action('admin_head', 'load_tiny_mce');
 2:
 3: function load_tiny_mce() {
 4:     $theme_advanced_fonts = 'Aclonica=Aclonica, sans-serif;';
 5:     $theme_advanced_fonts .= 'Michroma=Michroma, sans-serif;';
 6:     $theme_advanced_fonts .= 'Paytone One=Paytone One, sans-serif';
 7:
 8:     // The 'mode' and 'editor_selector' options are for adding
 9:     // TinyMCE to any textarea with class="tinymce-textarea"
 10:     wp_tiny_mce(false, array(
 11:         'mode' => 'specific_textareas',
 12:         'editor_selector' => 'tinymce-textarea',
 13:         'theme_advanced_fonts' => $theme_advanced_fonts
 14:     ));
 15: }

As you can see, the theme_advanced_fonts option is just a string delimited by a semicolon. TinyMCE then splits each item on the equal sign. The left side of the equal sign is what you see in the dropdown list, and the right side of the equal sign is what gets inserted into the editor when that font is selected.

So when this code runs and we refresh our TinyMCE editor we get the following:

tinymce-google-fonts-plain

Sweet! Well, sort of. Our Google Web Fonts are listed, but they aren’t formatted with their font styles. Not only that, but all of the default fonts are gone as well. So let’s fix those two things.

Step 2: Add the Font Styles

For each Google Web Font you want to include, you’ll need its @import CSS rule so you can add it to your own stylesheets. For example, here are the @import statements required for our three Google Web Fonts:

 1: @import url(http://fonts.googleapis.com/css?family=Aclonica);
 2: @import url(http://fonts.googleapis.com/css?family=Michroma);
 3: @import url(http://fonts.googleapis.com/css?family=Paytone+One);

Put these in one of your own stylesheets (i.e. admin.css) and even your style.css file and refresh your TinyMCE editor to get this:

tinymce-google-fonts-formatted

That’s more like it. But if we type something in the editor and try to make it Michroma, this is what we get:

tinymce-google-font-not-rendered

Those words are definitely not rendered with the Michroma Google Web Font. So let’s fix that next.

Step 3: Use the content_css Option

We need to use another TinyMCE option, this time the content_css option. Basically we’re going to take the URLs from our CSS @import rules and add them to the TinyMCE editor, as such:

 1: function load_tiny_mce() {
 2:     $theme_advanced_fonts = 'Aclonica=Aclonica, sans-serif;';
 3:     $theme_advanced_fonts .= 'Michroma=Michroma, sans-serif;';
 4:     $theme_advanced_fonts .= 'Paytone One=Paytone One, sans-serif';
 5:
 6:     $content_css = 'http://fonts.googleapis.com/css?family=Aclonica,';
 7:     $content_css .= 'http://fonts.googleapis.com/css?family=Michroma,';
 8:     $content_css .= 'http://fonts.googleapis.com/css?family=Paytone+One';
 9:
 10:     // The 'mode' and 'editor_selector' options are for adding
 11:     // TinyMCE to any textarea with class="tinymce-textarea"
 12:     wp_tiny_mce(false, array(
 13:         'mode' => 'specific_textareas',
 14:         'editor_selector' => 'tinymce-textarea',
 15:         'theme_advanced_fonts' => $theme_advanced_fonts,
 16:         'content_css' => $content_css
 17:     ));
 18: }

Pretty straightforward, but what’s happening is that TinyMCE will now include the styles found in those URLs when it renders content in the editor box. So if we go back to our TinyMCE editor we see that our text is properly formatted with the Michroma Google Web Font:

tinymce-google-font-properly-rendered

Now all we need is to get the default fonts back in the list and we’re good to go (unless of course you don’t want them, in which case you can ignore the next step).

Step 4: Put the Default Fonts Back in There

Adding back in the default TinyMCE fonts helps us put this all together, so now our load_tiny_mce function looks like this:

 1: function load_tiny_mce() {
 2:     // Google Web Fonts
 3:     $theme_advanced_fonts = 'Aclonica=Aclonica, sans-serif;';
 4:     $theme_advanced_fonts .= 'Michroma=Michroma, sans-serif;';
 5:     $theme_advanced_fonts .= 'Paytone One=Paytone One, sans-serif;';
 6:
 7:     // Default fonts for TinyMCE
 8:     $theme_advanced_fonts .= 'Andale Mono=Andale Mono, Times;';
 9:     $theme_advanced_fonts .= 'Arial=Arial, Helvetica, sans-serif;';
 10:    $theme_advanced_fonts .= 'Arial Black=Arial Black, Avant Garde;';
 11:    $theme_advanced_fonts .= 'Book Antiqua=Book Antiqua, Palatino;';
 12:    $theme_advanced_fonts .= 'Comic Sans MS=Comic Sans MS, sans-serif;';
 13:    $theme_advanced_fonts .= 'Courier New=Courier New, Courier;';
 14:    $theme_advanced_fonts .= 'Georgia=Georgia, Palatino;';
 15:    $theme_advanced_fonts .= 'Helvetica=Helvetica;';
 16:    $theme_advanced_fonts .= 'Impact=Impact, Chicago;';
 17:    $theme_advanced_fonts .= 'Symbol=Symbol;';
 18:    $theme_advanced_fonts .= 'Tahoma=Tahoma, Arial, Helvetica, sans-serif;';
 19:    $theme_advanced_fonts .= 'Terminal=Terminal, Monaco;';
 20:    $theme_advanced_fonts .= 'Times New Roman=Times New Roman, Times;';
 21:    $theme_advanced_fonts .= 'Trebuchet MS=Trebuchet MS, Geneva;';
 22:    $theme_advanced_fonts .= 'Verdana=Verdana, Geneva;';
 23:    $theme_advanced_fonts .= 'Webdings=Webdings;';
 24:    $theme_advanced_fonts .= 'Wingdings=Wingdings, Zapf Dingbats';
 25:
 26:    // Styles for the Google Web Fonts
 27:    $content_css = 'http://fonts.googleapis.com/css?family=Aclonica,';
 28:    $content_css .= 'http://fonts.googleapis.com/css?family=Michroma,';
 29:    $content_css .= 'http://fonts.googleapis.com/css?family=Paytone+One';
 30:
 31:    // The 'mode' and 'editor_selector' options are for adding
 32:    // TinyMCE to any textarea with class="tinymce-textarea"
 33:    wp_tiny_mce(false, array(
 34:        'mode' => 'specific_textareas',
 35:        'editor_selector' => 'tinymce-textarea',
 36:        'theme_advanced_fonts' => $theme_advanced_fonts,
 37:        'content_css' => $content_css
 38:    ));
 39: }

We don’t have to worry about adding anything else to the content_css option for the default fonts because those styles are already included as part of TinyMCE. And now when we look at our editor again we see all of the fonts listed like we expect:

tinymce-google-fonts-everything

And there you have it, a nice walkthrough for adding Google Web Fonts to your TinyMCE editors. Enjoy.

 

November 7, 2011
Written by: Dave Donaldson
  • Josh

    Thank you so much for the FANTASTIC breakdown!!  I have been messing with this for about a week.  Your tutorial here was exactly what I needed in order to progress.  It took a little tweaking, but I got everything working exactly the way I envisioned.

    Thanks again, and one million gold stars to you!

    • http://maxfoundry.com Max Foundry

      Hi Josh – Thanks for the comment, glad you found it helpful.

  • Gregg Franklin

    I want to get this working but I am having a problem. I want to make sure I understand something. Step 4 is all the code you would place in your Functions file… right? I added the Font selection to TinyMCE (drop down font list) worked great, shows all the default font’s. But then when adding the code from step 4 I do not get the additional Google fonts.

    • http://maxfoundry.com Max Foundry

      Hi Gregg – Did you simply copy the code from step 4? If so, that might be the problem because it had the wrong function name (copy-paste error on my part). Try it again and see how that goes.

      • Gregg Franklin

        Max, thanks for the quick reply and I noticed that when I added the code but I still am not getting the result. Here is the code: http://pastie.org/2842591. Thank you so much for helping me out with this.

        • http://maxfoundry.com Max Foundry

          Are you trying to do this for the standard TinyMCE editor box or for your own custom editor box (like I talked about in the previous post)? I think this will only work with your own custom TinyMCE editor boxes. I couldn’t get the Google Web Fonts to show in the default WordPress TinyMCE editor either. But once I figure that out I’ll be sure to update this post. Sorry if this was misleading.

          • Gregg Franklin

            Oh.. yes I was trying to use with the standard default box. I hope you figure this out. I think this could be really useful to add and take away fonts for specific clients. – Keep me posted. :-)

        • Josh

          Hi Gregg, 

          Did you do step 2 above?  Did you copy the imports into your stylesheet?

  • Josh

    Hey Max, I used some of your code to add this option to my visual editor plugin.  Please take the time to check it out here:

    http://wordpress.org/extend/plugins/simple-tinymce-button-upgrade/

    I hope you don’t mind me adding this here :)  I’ve already included 25 Google Fonts in it, and I’m working on the rest now.

  • Josh

    If you would like me to explain how I tweaked the code to get it to work, I’ll be happy to share.

    • http://maxfoundry.com Max Foundry

      Hi Josh – Definitely, fire away. Would love to see what you did (as I’m sure everyone else).

  • Josh

    Haha… thanks.  Okay, here goes.

    1.  This hooks our functions to a newly created stylesheet (for your step #2)

    `
    /* * register with hook ‘wp_print_styles’ and ‘admin_print_styles’. */ add_action(‘admin_print_styles’, ‘add_my_stylesheet’); add_action(‘wp_print_styles’, ‘add_my_stylesheet’); /* * Enqueue style-file, if it exists. */ function add_my_stylesheet() { $myStyleUrl = plugins_url(‘josh-font-style.css’, __FILE__); // Respects SSL, Style.css is relative to the current file $myStyleFile = WP_PLUGIN_DIR . ‘/simple-tinymce-button-upgrade/josh-font-style.css’; // This will call a newly created stylesheet from the root directory of my plugin. if ( file_exists($myStyleFile) ) { wp_register_style(‘myStyleSheets’, $myStyleUrl); wp_enqueue_style( ‘myStyleSheets’); } }
    `

    Then you will need to add each google font you want like described in step number 2.

    Next, create a function to “push” the array into the tinymce editor in wordpress:

    `
    function josh_custom_options( $opt ) {Â
    $opt['theme_advanced_fonts'] = ‘Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats;Aclonica=Aclonica, sans-serif;Michroma=Michroma, sans-serif;Paytone One=Paytone One’;
    // I added the default buttons back into the array.
    $opt['content_css'] = ‘http://fonts.googleapis.com/css?family=Aclonica,http://fonts.googleapis.com/css?family=Michroma,http://fonts.googleapis.com/css?family=Paytone+One‘; return $opt; } add_filter(‘tiny_mce_before_init’, ‘josh_custom_options’);
    `

    This pushes both the tinymce `theme_advanced_fonts` and the `content_css` into the array.

  • Josh

    Hmmm, I’m guessing backticks don’t work with the Disqus commenting system :/

    • Gregg Franklin

      I like Disqus but not the best tool for Code. :-) Maybe a link to http://pastie.org

  • Josh

    All right… weeks of work going onto pastebin… lol.

    http://pastie.org/2842908

    Keep in mind this is done through a plugin.  You might be able to skip the step about creating another stylesheet and importing it.  Instead, you can just add the @import url’s into your own theme’s stylesheet.

  • Gregg Franklin

    Thanks Josh! Can’t wait to give it a shot.

  • Josh

    By the way, if you do happen to try my plugin… PLEASE leave me a rating and a “works” on my wordpress plugin page.  I would certainly appreciate it.  Also give me some feedback if you get the change to use it.

    Again, thanks for the pointers!!

  • http://maxfoundry.com Max Foundry

    Josh – Thanks for posting your code. I haven’t tested it yet, but it looks like from your code that using the ‘tiny_mce_before_init’ filter is what makes it work with the default WordPress TinyMCE editor. Is that correct?

  • Josh

    @ Max,

    You’re very welcome.  That is correct; that’s the hook that makes the magic happen.  Then just create a variable to throw into the array upon initialization.  Keep in mind also I had to tweak this code because I am inserting it from a plugin (a little more difficult than having access to local files; because I had to “inject” it into the pre-existing code).

    Anyways, from your local install you wouldn’t need to use the stylesheet function; you can just add the import url’s directly into your theme’s stylesheet.

    • Josh

      I also added a ping to this blog from my plugin page.  Hope you don’t mind.

      • http://maxfoundry.com Max Foundry

        Not at all, thanks for linking to our post. And good luck with the plugin.

        • Gregg Franklin

          Ok I admit it I’m still to green to get this figures out. I took what Josh put up and took out the plugin stylesheet stuff. But then I am not clear on how to integrate what you have above in the tutorial. Josh’s code does not appear to create the drop down list. Do I need to add:
              // Adds our fonts to the drop-down list, including re-adding the original default fonts.
              add_filter(‘mce_buttons’, ‘add_font_selection_to_tinymce’);
                  function add_font_selection_to_tinymce($buttons) {
                  array_push($buttons, ‘fontselect’);
                  return $buttons;
              }

          • Josh

            Hi Gregg.  Add this to your functions.php:

            function tinymce_add_buttons($buttons) {

                 $buttons[] = ‘fontselect’;

               

                 return $buttons;

                }

               

            add_filter(“mce_buttons_3″, “tinymce_add_buttons”);

          • Gregg Franklin

            Thanks Josh! Here is what I ended up with.
                // Add google fonts to menu
                function tinymce_add_buttons($buttons) {

                 $buttons[] = ‘fontselect’;
                 return $buttons;
                }
              Â
                add_filter(“mce_buttons”, “tinymce_add_buttons”);

                function josh_stbu_custom_options( $opt ) {
                // Adds our fonts to the drop-down list, including re-adding the original fonts buttons.
                $opt['theme_advanced_fonts'] = ‘Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats;Aclonica=Aclonica, sans-serif;Michroma=Michroma, sans-serif;Paytone One=Paytone One, sans-serif;Lancelot=Lancelot’;Â
                                                 Â
                // Adds the stylesheet imports into the array.
                $opt['content_css'] = ‘http://fonts.googleapis.com/css?family=Aclonica,http://fonts.googleapis.com/css?family=Michroma,http://fonts.googleapis.com/css?family=Paytone+One,http://fonts.googleapis.com/css?family=Lancelot‘;Â
                 return $opt;  // Display our results
              Â
                 }
                 add_filter(‘tiny_mce_before_init’, ‘josh_stbu_custom_options);  // Hook our function to tinymce.

          • Norbert

             Gregg,
            $opt['content_css'] =  is a reference to a custom css file (e.g. editor-style.css) that styles all elements in the editor. It overwrites the standard css File of tinymce (….wp-includesjstinymcethemesadvancedskinsdefaultcontent.css).

  • Pingback: Adding Buttons to TinyMCE in Wordpress » Josh Lobe

  • Pingback: wp-coder.net » Theme.fm Weekly Roundup #12 (11/11/11)

  • Pingback: A Free wordpress newsletter » Theme.fm Weekly Roundup #12 (11/11/11)

  • Pingback: WordPress-Newsletter Nr. 30 | WordPress & Webwork

  • Pingback: How to Add Google Web Fonts to Your TinyMCE Editor in WordPress | Max Foundry | WordpressTips | Scoop.it

  • Pingback: How to Add Google Web Fonts to Your TinyMCE Editor in WordPress | Max Foundry

  • Pingback: Ultimate TinyMCE » Josh Lobe

  • Pingback: This Week In WordPress: Nov 11, 2011 | Max Foundry

  • http://256studio.com/ 256studio

    Could someone please update the complete code?

  • Josh

    MAX,

    So, I’ve noticed your ads like everywhere recently.  I don’t know if they have always been there and I just didn’t see them, or if you did something new.  I see them on blogs everywhere, pof, wp, and others.

    So, what do you use for banner rotation?  Do you pay for advertising or do you use something available for free?  Thanks for any advice.

  • Adam

    Terrible… After a design has been created, there should only be about 2 to 3 different fonts maximum used in a design. EVERY DESIGNER KNOWS AND EXECUTES THIS DESIGN FACT. Do you really want to give the options of Arial, Times New Roman, Comic Sans, Aclonica, Michroma, Paytone One, etc. to a client when you’ve already defined the site design’s fonts as Ubuntu and Verdana? Style the paragraph and the Headings (H1, H2, etc.) and be done. As long as the client uses those, there’s a much lesser chance the website design will be totally screwed up. Also, cut out the color and font size options. That’s another great way to have 36pt magenta and sapphire blue text litter a perfectly good website.

    Always ask yourself, “just because I can, does it mean I should?’

    • http://maxfoundry.com Max Foundry

      Adam – Valid points, but there’s nothing wrong with providing other font, size, and color options. People like to play with and tweak options, it’s just human nature. As the designer, it’s our job to put in place the best design possible, with the default fonts, colors, sizes that goes with the design. If a user plays around with the options and ends up with 36pt magenta and sapphire blue text, well, that’s their problem.

  • Pingback: Extensis plug-in now supports Google Web Fonts - Useful Documents – Useful Documents

  • Pingback: Navigate the font directory more easily and submit your fonts - Useful Documents – Useful Documents

  • Pingback: New Fonts for the New Year - Useful Documents – Useful Documents

  • Pingback: How To Make A Attractive Blog Post

  • http://sds.tumblr.com sds

    This tutorial is exactly what I have been scouring the web for over the last few days. The only thing I can’t figure out–and forgive me if this is an idiotic question–is what wordpress files the code snippets need to be added to? It doesn’t indicate anywhere in the post….

    • http://maxfoundry.com Max Foundry

      You should just be able to add the code to the functions.php file in your theme.

  • http://www.facebook.com/profile.php?id=100002371570328 Melike Gurkan

    This tutorial is exactly what I have been scouring the web for over the last few days. The only thing I can’t figure out–and forgive me if this is an idiotic question–is what wordpress files the code snippets need to be added to? It doesn’t indicate anywhere in the post

  • Pingback: LESFOOTIX | Pearltrees

  • Yahson

    you fucking rock!

  • http://wpsites.net/ Brad Dalton

    I guess i can use this for non Google custom fonts as well?

  • http://wpsites.net/ Brad Dalton

    Why can you just use this?

    function plugin_mce_addfont($mce_css) {

    if (! empty($mce_css)) $mce_css .= ‘,’;

    $mce_css .= ‘”,theme_advanced_fonts:”Custom Font=CustomFont,arial,helvetica,sans-serif’;

    return $mce_css;

    }

    add_filter(‘mce_css’, ‘plugin_mce_addfont’);

  • Greg

    When I try and use this my TinyMCE editor loads white on white text, only when I click on Visual/Text does it initialize fully loading everything. Any ideas?