Skip to content

Instantly share code, notes, and snippets.

@RiodeJaneiroo
Last active December 25, 2019 18:43
Show Gist options
  • Save RiodeJaneiroo/0f614c2b5002f21fa676e2df9e512afc to your computer and use it in GitHub Desktop.
Save RiodeJaneiroo/0f614c2b5002f21fa676e2df9e512afc to your computer and use it in GitHub Desktop.
[add button to tinymce]
<?php
add_action( 'after_setup_theme', 'mythemeslug_theme_setup' );
if ( ! function_exists( 'mythemeslug_theme_setup' ) ) {
function mythemeslug_theme_setup(){
/********* Registers an editor stylesheet for the theme ***********/
add_action( 'admin_init', 'mythemeslug_theme_add_editor_styles' );
/********* TinyMCE Buttons ***********/
add_action( 'init', 'mythemeslug_buttons' );
}
}
/********* Registers an editor stylesheet for the theme ***********/
if ( ! function_exists( 'mythemeslug_theme_add_editor_styles' ) ) {
function mythemeslug_theme_add_editor_styles() {
// add_editor_style( 'custom-editor-style.css' );
}
}
/********* TinyMCE Buttons ***********/
if ( ! function_exists( 'mythemeslug_buttons' ) ) {
function mythemeslug_buttons() {
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
return;
}
if ( get_user_option( 'rich_editing' ) !== 'true' ) {
return;
}
add_filter( 'mce_external_plugins', 'mythemeslug_add_buttons' );
add_filter( 'mce_buttons', 'mythemeslug_register_buttons' );
}
}
if ( ! function_exists( 'mythemeslug_add_buttons' ) ) {
function mythemeslug_add_buttons( $plugin_array ) {
$plugin_array['columns'] = get_template_directory_uri().'/js/tinymce_buttons.js';
return $plugin_array;
}
}
if ( ! function_exists( 'mythemeslug_register_buttons' ) ) {
function mythemeslug_register_buttons( $buttons ) {
array_push( $buttons, 'columns' );
return $buttons;
}
}
function bmessage( $atts, $content ){
$title = (isset($atts['title'])) ? $atts['title'] : '';
$color = (isset($atts['color'])) ? $atts['color'] : 'green';
$output = '<div class="bMessage bMessage__'.$color.'">';
if($title) $output .= '<div class="bMessage__title subject">'.$title.'</div>';
$output .= '<div class="bMessage__content wysiwyg">'.$content.'</div>';
$output .= '</div>';
return $output;
}
add_shortcode('message', 'bmessage');
(function() {
tinymce.PluginManager.add( 'columns', function( editor, url ) {
function addHtmlShorcode(text, color) {
var output = '';
var open_column = '[message color="'+color+'" title="Заголовок"]';
var close_column = '[/message]';
output = open_column + text + close_column;
return output;
}
// Add Button to Visual Editor Toolbar
editor.addButton('columns', {
text: 'Блок «Сообщение»',
icon: false,
type: 'menubutton',
menu: [
{
text: 'Зеленый',
onclick: function() {
var selected_text = editor.selection.getContent({'format': 'html'});
if ( selected_text.length === 0 ) {
alert( 'Нужно выделить какой то текст' );
return;
}
editor.execCommand('mceReplaceContent', false, addHtmlShorcode(selected_text, 'green'));
}
}, {
text: 'Жёлтый',
onclick: function() {
var selected_text = editor.selection.getContent({'format': 'html'});
if ( selected_text.length === 0 ) {
alert( 'Нужно выделить какой то текст' );
return;
}
editor.execCommand('mceReplaceContent', false, addHtmlShorcode(selected_text, 'yellow'));
}
}, {
text: 'Красный',
onclick: function() {
var selected_text = editor.selection.getContent({'format': 'html'});
if ( selected_text.length === 0 ) {
alert( 'Нужно выделить какой то текст' );
return;
}
editor.execCommand('mceReplaceContent', false, addHtmlShorcode(selected_text, 'red'));
}
}
]
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment