How to make custom TinyMCE button in WordPress change icon on hover

I created TinyMCE plugin for WordPress editor to insert Youtube videos. Everything works fine except this button has no hover state (like the default buttons have). I explored the code and found a difference – default buttons are spans with background-image sprite, and my custom button is a plain image. There’s no option in TinyMCE addButton() function to insert a span, only image:

ed.addButton('p2_youtube_button', {
    title : 'Insert Youtube video',
    cmd : 'mceYoutube',
    image: url + '/shortcode-youtube.png'
});

Is there a way to solve this little problem?

Read More

To illustrate how it looks (the red Youtube icon should be gray and turn red on hover):

http://d.pr/aszC

Related posts

Leave a Reply

2 comments

  1. I noticed that the Crayon Syntax Highlighter plugin has managed to do this. It is a bit of code to read through, I found the tinyMCE specific part in /wp-content/plugins/crayon-syntax-highlighter/util/tag-editor/crayon_tinymce.js . I hope this helps.

    The style which causes the highlight is here:

    .wp_themeSkin span.mce_crayon_tinymce {
        background: url(images/crayon_tinymce.png);
    }
    .wp_themeSkin .mceButtonEnabled:hover span.mce_crayon_tinymce,
    .wp_themeSkin .mceButtonActive span.mce_crayon_tinymce {
        background-position: -20px 0;
    }
    

    The image uses the same size as the other TinyMCE icons:

    enter image description here

  2. There are additional parameters you can pass to the addButton method that give you some options for how you skin your button.

    If you remove the image property and replace it with icon, you can use a font-ified icon instead. This is a multi-step process, which starts with actually building your icon font. Here’s a good tutorial that walks you through the process. The tutorial author recommends IcoMoon as a reliable way to build your icon fonts. There are probably others.

    The way that I use is similar to @feonix83’s approach, using CSS instead. Following the way WordPress itself does it, you lay your icons out in a sprite sheet, with the “hover” state 20px above the “off” state. If you don’t know what I’m talking about, take a look at the defalt WordPress icon sprite sheet: wp-includes/images/wpicons.png

    WordPress default TinyMCE icons

    If you remove the image property altogether, TinyMCE just puts a span of class mceIcon inside the button anchor block. It’s quite easy then to style that element and use the background-image referencing your sprite sheet. You use background-position to set the offset for the appropriate icon.

    There’s one additional trick that you can use to help you target only your buttons. You can add a class property to the addButton call and pass any number of classes. You will need to manually specify a specific class that can be used to target that button in particular, but you can also pass in an additional class that can be used to style all your buttons at once, since they won’t automatically inherit the styles that WordPress uses.

    class: "my-buttons my-specific-button"

    Here’s the CSS that I use. Note that this approach works best when each button has its own individual sprite sheet, as opposed to the WordPress approach that loads all the icons at once, though that approach has some performance benefits that are not to be ignored:

    .mceButtonEnabled:hover span.mceIcon.my-buttons { background-position: 0 0; }
    span.mceIcon.my-buttons.my-specific-button { background: url( images/my_button.png ) no-repeat 0 -20px; }