I hope this documentation , is very soon obsolete. You can follow the status of the actual hook implementation at trac.wordpress.org under bug #3563 . Follow procedure to integrate a button in the TinyMCE Editor . Everytime you see the phrase “myplugin” you need to replace it with your plugin name :

1. This is the main script to integrate the button control. It’s checks first the version of WordPress and activate then additional hooks and filter. If the Rich Text Editor (RTE) is deactivated , then we can use again buttonsnap.php

function myplugin_addbuttons() {

global $wp_db_version;

// Check for WordPress 2.1+ and activated RTE
if ( 3664 <= $wp_db_version && 'true' == get_user_option('rich_editing') ) {

// add the button for wp21 in a new way
add_filter("mce_plugins", "myplugin_button_plugin", 0);
add_filter('mce_buttons', 'myplugin_button', 0);
add_action('tinymce_before_init','myplugin_button_script');
}
else {

// Do it in the old way with buttonsnap
$button_image_url = PLUGINURL . '/javascript/buttonpic.gif';
buttonsnap_separator();
buttonsnap_jsbutton($button_image_url, __('myplugin Gallery', 'myplugin'), 'myplugin_buttonscript();');

}

}

2. This script you need to call via a add_action hook

// init process for button control
add_action('init', 'myplugin_addbuttons');

3. The two filters mce_plugins and mce_button integrate the button in the correct way (Described in the Codex). The plugins are loaded by the rich text editor. Filters using this hook should append the array they receive. The input and output is an array of strings. Important is the “-myplugin” with the dash symbol, cause this will tell TinyMCE that there is a external plugin.

// used to insert button in wordpress 2.1x editor
function myplugin_button($buttons) {
array_push($buttons, "separator", "myplugin");
return $buttons;
}

// Tell TinyMCE that there is a plugin (wp2.1)
function myplugin_button_plugin($plugins) {
array_push($plugins, "-myplugin","bold");
return $plugins;
}

4. Thanks to An-archos and Viper007Bond there exist a way to redirect the script path for the TinyMCE plugin. Normally all plugins are located in the path “/wp-includes/js/tinymce/plugins/”, but with this hook we redirect the path for this plugin to your plugin folder. This script is called “before” the page load the TinyMCE instance.

// Load the TinyMCE plugin : editor_plugin.js (wp2.1)
function myplugin_button_script() {
$pluginURL = PLUGINURL.'javascript/';
echo 'tinyMCE.loadPlugin("myplugin", "'.$pluginURL.'");'."\n";
return;
}

5. Now we can have a look for the main JavaScript which activate the button. It must be called “editor_plugin.js” and it must be placed in your plugin folder (or subfolder as you can see in the example). More information about the script you can find in the documentation from MoxieCode . Important is the call routine of your plugin for the button , the language pack and your own javascript (which perfom finally the action which you need). Here I call this script myplugin_buttonscript();

// Load the language file
tinyMCE.importPluginLanguagePack('myplugin', 'en');
var TinyMCE_mypluginscript = {
getInfo : function() {
return {
longname : 'myplugin',
author : 'Nobody',
authorurl : 'http://example.com',
infourl : 'http://example.com',
version : "1.0"
};
},
getControlHTML : function(cn) {
switch (cn) {
case "myplugin":
return tinyMCE.getButtonHTML(cn, 'lang_myplugin_desc', '{$pluginurl}/button.gif', 'mcemyplugin');
}
return "";
},
execCommand : function(editor_id, element, command, user_interface, value) {
switch (command) {
// Remember to have the "mce" prefix.
case "mcemyplugin":
// Call the script
myplugin_buttonscript();
return true;
}
return false;
}
};
// Adds the plugin class to the list of available TinyMCE plugins
tinyMCE.addPlugin("myplugin", TinyMCE_mypluginscript );

Note to the language pack
The Moxiecode wiki says that the related language file is placed in a subfolder “/langs” and it’s named with the language code which is predefined also in WPLANG (i.e. en_us.js) in your wp-config.php. This is a possible content :

// English lang variables
tinyMCE.addToLang('myplugin', {
desc : 'YOUR TEXT'
} );

6. Now we go back to your yourplugin.php and implement the myplugin_buttonscript();

// Load the Script for the Button(wp2.1)
function insert_myplugin_script() {
echo "\n".'

<script type="text/javascript">          function myplugin_buttonscript() {  window.open("URL TO A FILE");  }</script>';
return;
}

7. And finally we integrate this script into the header of the admin section.

add_action('edit_page_form', 'insert_myplugin_script');
add_action('edit_form_advanced', 'insert_myplugin_script');

8. Ups, don’t forget to integrate the buttonsnap class, to use it for the plain text editor. You can find a actual copy of buttonsnap.php form Owen Winkler and a good explanation at maxpower.ca .

// Needs to be loaded outside the class in order to work right
require(PLUGINURL. 'javascript/buttonsnap.php');

You can see it’s a lot of work for the integration, but I hope this small documentation help to get this working for you. If you have any recommendation or spelling correction for this article, it would be great if you leave a comment.

NOTE :

Sometime the new way of button integration failed and you didn’t see any buttons at all and the browser return a JavaScript Error : “TinyMCE not defined”.  I posted a workaround of this behaviour at trac here : http://trac.wordpress.org/ticket/3882

11 Responses to “TinyMCE Buttonsnap for WordPress 2.1”


  1. Gravatar Icon 1 Stephen Rider Posted January 23rd, 2007 - 20:20

    You absolutely, totally, and completely ROCK. Thank you!

    This is the clearest set of instructions I’ve been able to find on this. As you say, a lot of steps, but clearly explained and workable.

  2. Gravatar Icon 2 jovelstefan Posted January 27th, 2007 - 22:38

    As far as I can see, this doesn’t work, when there is no language file for the used WordPress language.
    For example, if you are using ‘de-de’ for WP (like me), the tinyMCE.importPluginLanguagePack function tries to load the corresponding de-de.js file in the plugin directory. If this doesn’t exists, the server gives back the 404-Page, that is of course translating nothing. So all variables, that should be translated remain something like ($lang_blabla), which is not very nice. Any idea to solve that problem?

  3. Gravatar Icon 3 alex.rabe Posted January 28th, 2007 - 12:20

    Be sure that you write the file name correct :

    Deutsch : de_DE.js

    I checked it on my localhost and it works in wordTube.
    (I have not yet updated a german blog to WP2.1)

    For a “default” language I have no solution…. (as far I study this integration)

  4. Gravatar Icon 4 Jeff Walters Posted February 5th, 2007 - 01:47

    Hi

    Thanks – this has been very useful. Do you by any chance know how to implement a custom ‘cleanup’ routine – similar to ButtonSnap.

    The specific requirement – is to be able add cleanup filters – so that the content of Rich Text editor can be manipulated to display a marker to indicate where an HTML tag has been inserted.

    The ButtonSnap function that does this is : TinyMCE_buttonsnap_cleanup

  5. Gravatar Icon 5 Ravan Posted February 19th, 2007 - 04:07

    Hi,

    First, thanks for this excellent tutorial!

    Now my question: With buttonsnap there was a way to give some CSS styling to a quicktag to recognize it when editing a post. This was done with function register_marker()…

    Do you know a way to do this for WP2.1 too?

    Ravan

  6. Gravatar Icon 6 Ajay Posted February 20th, 2007 - 13:20

    You’re a whiz!

    Thanks for this post… now begins my tough job of getting the button to work… wish things were simpler…

  7. Gravatar Icon 7 alex.rabe Posted February 20th, 2007 - 13:38

    Good luck ajay,

    @Ravan and Jeff

    I only study a way for the simple button integration. The Buttonsnap itself is so complex , that I didn’t able to supply you with all functions.

  8. Gravatar Icon 8 Brent Csutoras Posted March 25th, 2007 - 15:58

    I think the plugin is very clean and easy to implement and use.

    I have a question/suggestion.

    Any way to tie in a post link option.

    a. setup with post from last 7 days
    b. have the highlighted word searched for post with same word in the title or body

    Think it would be a nice add to this or as a seperate tool to help with inner linking to other posts for blogs.

    thanks for the great tool.

  9. Gravatar Icon 9 Leo Germani Posted May 29th, 2007 - 22:05

    Hi,

    I did everything on the how to and got my page loading without errors and with my new button showing up. Nevertheless, I cant get the button working. When I click it, I got the javascript error bellow and nothing happens. If you have any clues…

    Thanks for the documentation,

    Leo,,

    Erro: uncaught exception: [Exception... "Component returned failure code: 0x80004001 (NS_ERROR_NOT_IMPLEMENTED) [nsIDOMNSHTMLDocument.execCommand]” nsresult: “0×80004001 (NS_ERROR_NOT_IMPLEMENTED)” location: “JS frame :: http://localhost/audana/site/wp-includes/js/tinymce/tiny_mce_gzip.php?ver=20061113&theme=advanced&language=&plugins=inlinepopups%2Cautosave%2Cspellchecker%2Cpaste%2Cwordpress%2Cbold&lang=&index=0 :: anonymous :: line 161″ data: no]

Who's linking?

  1. 1 Is Wordpress 2.1 Naughty or Nice? at alex.rabe Pingback on Jan 20th, 2007
    "[...] Today I finished a short description, for a work around the button problem. See this post ..."
  2. 2 Wordpress 2.1 released ! Time to update the plugins… at alex.rabe Pingback on Jan 23rd, 2007
    "[...] I installed already some time ago a test version and didn’t encounter big problems with my plugins. So I hope ..."

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.