Skip to content

Instantly share code, notes, and snippets.

@trepmal
Created October 3, 2011 23:17
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/1260520 to your computer and use it in GitHub Desktop.
Save trepmal/1260520 to your computer and use it in GitHub Desktop.
WordPress: TinyMCE button that wraps selection in given tag
// http://tinymce.moxiecode.com/wiki.php/API3:class.tinymce.Plugin
(function() {
tinymce.create('tinymce.plugins.WRAP', {
/**
* Initializes the plugin, this will be executed after the plugin has been created.
* This call is done before the editor instance has finished its initialization so use the onInit event
* of the editor instance to intercept that event.
*
* @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
* @param {string} url Absolute URL to where the plugin is located.
*/
init : function(ed, url) {
//this command will be executed when the button in the toolbar is clicked
ed.addCommand('mceWRAP', function() {
selection = tinyMCE.activeEditor.selection.getContent();
//prompt for a tag to use
//tag = prompt('Tag:');
//tinyMCE.activeEditor.selection.setContent('<' + tag + '>' + selection + '</' + tag + '>');
tinyMCE.activeEditor.selection.setContent('<div class="classy-div">' + selection + '</div>');
});
ed.addButton('WRAP', {
title : 'WRAP.desc',
cmd : 'mceWRAP',
//image : url + '/your-icon.gif'
image : 'http://mdawg123.wikispaces.com/i/icon_16_gear.gif'
});
},
});
// Register plugin
tinymce.PluginManager.add('WRAP', tinymce.plugins.WRAP);
})();
<?php
/*
Plugin Name: MCE Wrapper
Plugin URI: http://trepmal.com/
Description: TinyMCE button that wraps selection in given tag
Author: Kailey Lampert
Version: 1.0
Author URI: http://kaileylampert.com/
Copyright (C) 2011 Kailey Lampert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
add_action( 'init', 'mce_wrapper_init', 9);
function mce_wrapper_init() {
if (is_admin())
new mcewrapper();
}
class mcewrapper {
var $pluginname = 'WRAP';
var $internalVersion = 600;
/**
* mcewrapper::mcewrapper()
* the constructor
*
* @return void
*/
function mcewrapper() {
// Modify the version when tinyMCE plugins are changed.
add_filter('tiny_mce_version', array (&$this, 'change_tinymce_version') );
// init process for button control
add_action('init', array (&$this, 'addbuttons') );
}
/**
* mcewrapper::addbuttons()
*
* @return void
*/
function addbuttons() {
// Don't bother doing this stuff if the current user lacks permissions
if ( !current_user_can('edit_posts') && !current_user_can('edit_pages') )
return;
// Add only in Rich Editor mode
if ( get_user_option('rich_editing') == 'true') {
// add the button for wp2.5 in a new way
add_filter("mce_external_plugins", array (&$this, 'add_tinymce_plugin' ));
add_filter('mce_buttons', array (&$this, 'register_button' ), 0);
}
}
/**
* mcewrapper::register_button()
* used to insert button in wordpress 2.5x editor
*
* @return $buttons
*/
function register_button($buttons) {
array_push($buttons, 'separator', $this->pluginname );
return $buttons;
}
/**
* mcewrapper::add_tinymce_plugin()
* Load the TinyMCE plugin : editor_plugin.js
*
* @return $plugin_array
*/
function add_tinymce_plugin($plugin_array) {
$plugin_array[$this->pluginname] = plugins_url( 'editor_plugin.js', __FILE__ );
return $plugin_array;
}
/**
* mcewrapper::change_tinymce_version()
* A different version will rebuild the cache
*
* @return $version
*/
function change_tinymce_version($version) {
$version = $version + $this->internalVersion;
return $version;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment