I have asked this question about a year ago and I am hoping there is some type of simple solution which will allow me achieve my objective. So here it goes:
I often utilize shortcodes within the Admin Editor but when I turn this over to client they often don’t understand how they work.
What I am looking for is a solution which would simply automatically render the associating output of shortcodes within the admin WYSIWYG editor.
From a visual perspective, I would like for this to display similar to when the “more” line show up or when an image is displayed within the editor. By this I mean that the user would see the output but would only be able to delete it but not edit the content of the rendered shortcode.
It’s actually not too bad to do what you’re asking. This should take you about an hour to do your first one, and 10 minutes to do subsequent ones.
Ultimately what you’re going to do is create a TinyMCE plugin. Here’s what you should arm yourself with to start:
You now have all the tools to get ‘er done! Of all this, the code that will be of most interest to you is this block in the WP example code:
Here, the shortcode for a gallery gets replaced with an
img
tag. Theimg
tag has the classwp-gallery
, which gets styled by the CSS found here.Edit 2016-04-06: Updated content and links for TinyMCE 4 and WordPress 4.4
This is not a complete answer, just an design direction. I think the best approach is something like this:
In the admin edit post
Rip all the shortcodes from the saved post and render it inside a metabox, aside from the editor. Make shure they appears in the same order as the shortcodes occur in the tiny Editor.
In tinyMCE javascript API
Make a jQuery function, when user click on a shortcode, it swaps the HTML from the metabox into the editor. And vice versa. The order itselfs should be ok as association, but Im not shure about enclosing shortcodes. However there is many ways to design a nice ID-connection. The updates of shortcodes can be done on the fly with ajax.
Never save the rendered shortcode state
Before switch editors, save drafts, autodrafts and publish, make a API call to trigger the restore, so the rendered shortcode state never get saved…
This can be done, but you need to be familar with the tinyMCE API to understand where and when to access the contents of the editor, and hook into javascript actions before ‘save’ and more.
There can be several tinyMCE editor on the same edit post pageload.
The restore part can be investigated by looking at the
[gallery]
shortcode beaviour. But the click on[MY_SHORTCODE]
has to be done by some jQuery tricks.in the admin_footer script, access the content of where the cursor is active with:
is a hint of how to start.
I was looking for a way to graphically display and tweak shortcodes too. And now, finally, I found a tutorial that does exactly that: https://generatewp.com/take-shortcodes-ultimate-level/
I add the description so that search engines pick it up: