0
votes

Trying to create a special button on the Tiny MCE editor on the Post Editor of Wordpress. I want to create a button for my created shortcode tooltips.

Check out my desired output:

I have the following code for my Tooltip shortcode:

//Text tooltip
function tooltip($atts, $content = ''){
    $atts = shortcode_atts(
        array(
          'class' => '',
          'title' => ''
        ), $atts);


     $html = '<a class="' . $atts['class'] .'"  title="'. $atts['title']. '" href="#">' . $content . ' <span>' .$atts['title']. '</span> </a>';
    return $html;    
  } 

add_shortcode('tooltip', 'tooltip');

Now when you execute this you will use the following codes for the shortcode:

[tooltips class="top_tooltip" title="Your Tooltip here"] Text here [/tooltip]

What I have done is that created some function to DISPLAY MY CREATED CUSTOM BUTTON TOOLTIP SHORTCODE on the functions.php file on my theme using the following codes.

//Create Tiny MCE buttons
function mce_tooltip($button) {
  $buttons[] = 'superscript';
  return $button;
}
add_filter('mce_buttons_2', 'mce_tooltip');



/*
* Callback function to filter the MCE settings
*/

function mce_tooltip( $init_array ) {  

// Define the style_formats array

  $style_formats = array(  
    // Each array child is a format with it's own settings
    array(  
      'class' => '',  
      'title' => ''      
  );  
  // Insert the array, JSON ENCODED, into 'style_formats'
  $init_array['style_formats'] = json_encode( $style_formats );  

  return $init_array;  

} 
// Attach callback to 'tiny_mce_before_init' 
add_filter( 'tiny_mce_before_init', 'mce_tooltip' ); 

I tried the code but it won't show up my CUSTOM button for my shortcode on the TINY MCE Editor on Wordpress. Any idea how to do it better?

3

3 Answers

0
votes

Check your code. I guess there is invalid return value

function mce_tooltip($button) {
  $buttons[] = 'superscript';
  return $button;
}
0
votes

For TinyMCE Advanced Version: 4.1.1 follow this steps:

  1. In /wp-content/plugins/tinymce-advanced/tinymce-advanced.php (function get_all_buttons()) add $buttons[] = 'yourButton' http://prntscr.com/5ywb4p
  2. In /wp-includes/js/tinymce/tinymce.min.js after some button add your own e.addButton('yourButton',{title:'yourButton',text:"yourButton",'class' : 'yourButton-btn', onclick : function() { e.focus(); e.selection.setContent('<YourTag>' + e.selection.getContent() + '</YourTag>'); } }) http://prntscr.com/5ywc48 Then go into tinymce settings in wp-admin and add your new button to editor.
-1
votes

http://www.gavick.com/blog/adding-your-own-buttons-in-tinymce-4-editor/

Try this tutorial, it works. It teaches how to add a custom button to the new tinymce 4 and how to use it for your own shortcodes