Use TinyMCE Throughout WordPress Application

We are famil­iar with TinyMCE from the Word­Press WYSIWYG edi­tor, and it is a fan­tas­tic tool to quickly gen­er­ate markup that is seman­tic and XHTML com­pli­ant. But what if what we want to offer it in other parts of our appli­ca­tion? One notice­able exam­ple would be on sin­gle post pages so your vis­i­tors can use the edi­tor. Here is the code to use, and it goes in your functions.php file in your cur­rent theme direc­tory (if you don’t have one then just add it).

function addtinymce() {
    echo '<script language="javascript" type="text/javascript" src="/wp-includes/js/tinymce/tiny_mce.js"></script>';
    echo '<script language="javascript" type="text/javascript">';
    echo 'tinyMCE.init({mode : "textareas", theme : "advanced", theme_advanced_buttons1 : "bold,italic,strikethrough,bullist,numlist,outdent,indent,link,unlink", theme_advanced_buttons2 : "", theme_advanced_buttons3 : "", language : "en",theme_advanced_toolbar_location : "top", theme_advanced_toolbar_align : "left"});';
    echo '</script>';
} ?>

Then you sim­ple add the fol­low­ing func­tion in your header.php before the clos­ing head statement.

<?php addtinymce(); ?>

Notice that I didn’t have to install any­thing, because I’m using the TinyMCE code that exists in the core (under the wp-includes direc­tory). So, when­ever WP updates the code then you will receive the updates auto­mat­i­cally, and this is going to hap­pen when 2.3 is released.

You might want to con­sider where you want to actu­ally load all that JavaScript as it could slow down the load­ing of your site and increase band­width. To cir­cum­vent this we should load it only on the pages that it is used via WP con­di­tional tags. Here is an exam­ple to load it only on the post reply page. This logic can replace the sec­ond code block above.

<?php
if (is_single()) {
  addtinymce();
}
else {}
?>

Feel free to add mul­ti­ple con­di­tions to this logic, and you can also con­fig­ure the TinyMCE options all your heart desires.

14 responses to “Use TinyMCE Throughout WordPress Application”

  1. Cool! Do you know whether it’s pos­si­ble to just use it on post pages in Quick­Tags mode, i.e. with sim­ple but­tons but with­out the full rich text editor?

    I know you’ve linked to the docs above, but just thought you might be able to give me a leg up, if you’ve played with this before. Thanks.

  2. Chris — Too heavy/slow load­ing. I think most peo­ple pre­fer com­ment­ing in plain text, but wouldn’t mind a lit­tle assist with hrefs etc.

    So there’s no way in that init line to spec­ify the sim­pli­fied version?

  3. Hmm. Basi­cally I was hop­ing to repro­duce a sim­pli­fied ver­sion of the post edi­tor in wp-admin when rich text edit­ing is dis­abled in the pro­file. WP gets it to appear above the text area, rather than below.

    Hard to explain, but for some rea­son I have lit­tle inter­est in using the rich edi­tor for user com­ments (for the same rea­son you’re not using it? :)

  4. Thanks for your tips on this. I’ll give it a whirl one of these days.

  5. There is another way, using WP native func­tions (loads the edi­tor with the default WP configurations):

    if(user_can_richedit()) { // if rich edi­tor is enabled
    wp_enqueue_script(‘tiny_mce’);
    wp_enqueue_script(‘wp_tiny_mce’);
    }

  6. Hey,

    your high­light func­tion for google search related vis­i­tors also puts the high­light in the code block!

    tinyMCE.init

    just want to let you know!

  7. yeah if you arrive here from Google the stu­pid search term high­light deal makes the code unus­able. Can’t copy paste it and most of it is invisible.

  8. I would like to have some syntaxhighlighting-button to this script, is that possible?

    So you can show php/C#/etc. code with col­ors in your page?

Leave a Reply