Skip to content

Instantly share code, notes, and snippets.

@aquelito
Created October 18, 2012 06:52
Show Gist options
  • Save aquelito/3910221 to your computer and use it in GitHub Desktop.
Save aquelito/3910221 to your computer and use it in GitHub Desktop.
WordPress Tiny MCE - Change Language Attributes
/*
* plugin/tinymce/editor_plugin.js
*/
(function(){
tinymce.PluginManager.requireLangPack('languages');
tinymce.create('tinymce.plugins.languages', {
createControl: function(n, cm){
/*var list, listlang = {
'en': 'English',
'es': 'Spanish',
'fr': 'French'
}, t = this;*/
switch (n) {
case 'languages':
var mlb = cm.createListBox('languages', {
title : optiontitle,
onselect : function(v){
var matches, formatNames = [];
var sel_txt = tinyMCE.activeEditor.selection.getContent();
tinyMCE.activeEditor.execCommand('mceInsertContent', false, '<span lang="' + v + '">' + sel_txt + '</span>');
return false;
}
});
for(var x in listlang) {
mlb.add(listlang[x], x);
}
return mlb;
}
return null;
},
getInfo: function() {
return {
longname: 'TinyMCE Languages Change',
author: 'aquelito',
authorurl: 'http://aquelito.fr',
infourl: 'http://aquelito.fr',
version: tinymce.majorVersion + "." + tinymce.minorVersion
};
},
});
tinymce.PluginManager.add('languages', tinymce.plugins.languages);
})();
/*
* plugin/tinymce/langs/en.js
*/
tinyMCE.addI18n({ en: {
tiny_languages: {
desc : 'Languages'
}
}});
/*
* plugin/tinymce/langs/fr.js
*/
tinyMCE.addI18n({ fr: {
languages: {
desc : 'Langues',
}
}});
<?php
/*
Plugin Name: Change Language Attributes
Plugin URI: http://github.com/aquelito
Description: Tiny MCE - Change Language Attributes
Author: aquelito
Version: 1.0
Author URI: http://aquelito.fr
License: GPLv2 or later
*/
/*
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 2
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, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
class PluginLanguageAttributes {
const LANG = 'PluginLanguageAttributes';
function __construct() {
load_plugin_textdomain(self::LANG, false, dirname(plugin_basename( __FILE__ ) ) . '/languages/');
$this->advanced_buttons = array(
'theme_advanced_buttons1' => 'bold,italic,strikethrough,|,bullist,numlist,blockquote,|,sub,sup,|,justifyleft,justifycenter,justifyright,|,link,unlink,|,spellchecker,fullscreen,wp_adv',
'theme_advanced_buttons2' => 'abbreviation,languages,styleselect,formatselect,underline,forecolor,|,pastetext,pasteword,removeformat,|,charmap,|,outdent,indent,|,undo,redo,wp_help',
);
$this->advanced_blockformats = 'p,h2,h3,h4,div,h5,h6,pre,code,samp';
$this->langs_formats = array(
'en' => __("English", self::LANG),
'es' => __("Spanish", self::LANG),
'it' => __("Italian", self::LANG),
'fr' => __("French", self::LANG),
'de' => __("German", self::LANG)
);
$this->langs_spellchecker = array(
'fr' => '+'.__("French", self::LANG),
'en' => __("English", self::LANG)
);
$this->style_formats = array(
//array( 'title' => __('Red text', self::LANG), 'inline' => 'span', 'styles' => array('color' => '#ff0000')),
array( 'title' => __('Align Left', self::LANG), 'block' => 'p', 'classes' => 'aleft' ),
array( 'title' => __('Align Center', self::LANG), 'block' => 'p', 'classes' => 'acenter' ),
array( 'title' => __('Align Right', self::LANG), 'block' => 'p', 'classes' => 'aright' ),
array( 'title' => __('Typography Style', self::LANG) ),
array( 'title' => __('Highlight', self::LANG), 'inline' => 'span', 'classes' => 'highlight' ),
array( 'title' => __('Caps', self::LANG), 'inline' => 'span', 'classes' => 'caps' ),
array( 'title' => __('Small-caps', self::LANG), 'inline' => 'span', 'classes' => 'small-caps' ),
array( 'title' => __('No-caps', self::LANG), 'inline' => 'span', 'classes' => 'no-caps' ),
array( 'title' => __('Regular', self::LANG), 'inline' => 'span', 'classes' => 'regular' ),
array( 'title' => __('Bold', self::LANG), 'inline' => 'span', 'classes' => 'bold' ),
array( 'title' => __('Italic', self::LANG), 'inline' => 'span', 'classes' => 'italic' ),
array( 'title' => __('Underline', self::LANG), 'inline' => 'span', 'classes' => 'underline' ),
array( 'title' => __('Strike', self::LANG), 'inline' => 'span', 'classes' => 'strike' ),
array( 'title' => __('Alignement', self::LANG) ),
array( 'title' => __('Float Left', self::LANG), 'block' => 'p', 'classes' => 'alignleft' ),
array( 'title' => __('Float Right', self::LANG), 'block' => 'p', 'classes' => 'alignright' ),
);
add_action('plugins_loaded', array($this, 'admin_plugins_loaded'));
}
public function admin_plugins_loaded () {
if( is_admin() && current_user_can('edit_posts' ) &&
current_user_can('edit_pages') && get_user_option('rich_editing') == 'true') {
$this->admin_hooks();
}
}
private function admin_hooks() {
add_filter( 'tiny_mce_before_init', array(&$this, 'editor_before_init') );
add_filter( 'mce_external_plugins', array(&$this, 'editor_external_plugins') );
add_action( 'admin_head', array(&$this, 'editor_header_var') );
}
public function editor_before_init( $settings ) {
// advanced_buttons_bars
foreach($this->advanced_buttons as $key => $value) {
$settings[$key] = $value;
}
// advanced_blockformat
$settings['theme_advanced_blockformats'] = $this->advanced_blockformats;
// spellchecker
foreach($this->langs_spellchecker as $lkey => $lvalue) {
$r[] = $lvalue.'='.$lkey.'';
}
$settings['spellchecker_languages'] = join(', ', $r);
$settings['style_formats'] = json_encode( $this->style_formats );
return $settings;
}
public function format_string( $array, $inn = ':', $sep = ', ') {
foreach($array as $lkey => $lvalue) {
$output[] = '"'.$lkey.'"'.$inn.'"'.$lvalue.'"';
}
return join($sep, $output);
}
public function editor_external_plugins($plugin_array) {
$plugin_array['languages'] = plugins_url('/tinymce/editor_plugin.js', __FILE__ );
return $plugin_array;
}
public function editor_header_var(){
foreach($this->langs_formats as $key => $value) {
if($key != substr(WPLANG, 0, 2) )
$output[] = $key.':"'.$value.'"';
}
echo '<script type="text/javascript">var optiontitle = "'.__('Languages', self::LANG).'", listlang = {'. join(', ', $output) .'}</script>';
}
}
$PluginLanguageAttributes = new PluginLanguageAttributes();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment