To WordPress’ visual editor (TinyMCE), I would like to add a button that adds four non-breaking spaces when clicked, like this:
I found some plugins that add buttons to the HTML editor, but not to the visual editor (this one for e.g).
or in general, it would be nice to know if there’s a plugin or a codable (but simple) alternative to add custom buttons to the visual tinyMCE editor.
I already demonstrated how you can achieve such thing in this question but I can explain it here again with your request. I have tested this in WordPress 3.5 and it works elegantly. To avoid this answer being the length of a thesis, I have added comments in all codes to help you understand what each function does and I highly recommend that you read and thoroughly understand them.
Firstly, in your theme folder, add a folder named
admin
and inside it, create fileclass.new_tinymce_btn.php
with code:This code will add custom buttons to the visual editor.
Next, in your theme folder, create these folders
adminjs/buttons
and inside, create this JS filespacebutton.js
with code:You will need to add an image for the button (
/images/btn_spacebutton.png
). The above code is a rather simple javascript function for what happens when the button is pressed.Now you will need to load this button class by adding this to your functions file:
That is it. You should find your new custom button in the visual editor. Whenever you want to add more custom buttons, just add a new JS file with the button function and load the button class as demonstrated above.
TL;DR, code is at the bottom.
OK, this should work for you, but it is a beta. It works for me, but I haven’t done any kind of rigorous testing. One thing, it doesn’t output four consecutive
entities; it does a stuttered
[space]
[space]
, but at least it keeps them as-is when switching back-and-forth between Visual & Text modes. It only works in Visual mode, I haven’t taken the time to figure out how to make it work in Text mode.It comes in two files, name as you see fit. I used the
highly imaginativename: 4Spaces. 🙂 The TinyMCE button is on the top-row, far-right, of the editor. It’ll show whatever your flavor of browser shows for non-existent images. That’s easily changed in 4spaces.js, line 8:Change `YOUR_IMAGE_HERE.png’ to your image file, relative to the two files, or use an absolute URI like so:
or
I’ve commented and/or left some existing comments throughout the PHP, the JavaScript comments are sparse. You’ll see in the PHP header section where the PHP code came from and kind of where the JavaScript originated.
Two notable credits, both listed in the PHP header: this WordPress.SE Answer that provided the code that stops TinyMCE (or WordPress, not sure which) from stripping out the spaces, and the link provided in the prior answer by @Alexey, which, while not critical, helped me stumble onto the JS solution.
I couldn’t make the code in that link work, but eventually came back to it and found the nugget that brought it all together (with some tweaking, to be sure).
I think that about sums everything up. Here’s the code:
4spaces.php
4spaces.js
Edit: I forgot to mention, for those who don’t know, put both of those files into a directory under
/wp-content/plugins/
(default path). It should look something like/wp-content/plugins/4spaces/
or whatever name you decide to call it, then activate it in Admin.
P.S. – I’m relatively new to OOP, so welcome any and all criticisms, advice, etc., from anyone viewing this answer.