Title: TinyMCE views API improvements
Author: Ella Van Durpe
Published: April 23, 2015
Last modified: July 10, 2015

---

 [  ](https://profiles.wordpress.org/ellatrix/) [Ella Van Durpe](https://profiles.wordpress.org/ellatrix/)
2:14 am _on_ April 23, 2015     
Tags: [4.2 ( 67 )](https://make.wordpress.org/core/tag/4-2/),
[dev-notes ( 661 )](https://make.wordpress.org/core/tag/dev-notes/)   

# TinyMCE views API improvements

In WordPress 4.2 there will be changes to the experimental TinyMCE views APIAPI 
An API or Application Programming Interface is a software intermediary that allows
programs to interact with each other and share data in limited, clearly defined 
ways.. We don’t recommend this for use in production unless you closely follow the
development.

If you’re using it we’d love to get some feedback: bugs, suggestions, things you
wish you could do with it… You’re also welcome to leave a comment with a link to
your project. I’d love to know what kind of content you’re using it for and how 
you’re using it.

## Summary of the changes:

 * Simpler registration.
 * An easier way to update the view in the edit method.
 * You can choose to leave the text instead of replacing it with a loader. This 
   is especially useful for our oEmbed views because we don’t know if the URLURL
   A specific web address of a website or web page on the Internet, such as a website’s
   URL www.wordpress.org can be embedded until we get a response from the server.
 * You can now update the view text with a text of a different type (e.g. audio 
   to playlist).
 * No need to use the `setIframes` method directly, `render` and `setContent` will
   handle it for you.
 * Already rendered views will never refresh again while editing other content. 
   They do refresh when undoing or redoing things though, because TinyMCE resets
   the whole content unfortunately.
 * Options set in the `match` method (previously `toView`) will be added automatically
   to the view instance.
 * A couple of memory leaks were fixed and there’s a better way to bind and unbind
   views.
 * We added documentation for all the methods.

## How to use the API

Registration:

    ```notranslate
    window.wp.mce.views.register( 'unique_name', {
    	...
    } );
    ```

If you’re converting shortcodes to views, make sure `unique_name` is the same. They’ll
be matched automatically. If you’re interested in custom matching, take a look at
the [`embedURL` registration in core](https://core.trac.wordpress.org/browser/trunk/src/wp-includes/js/mce-view.js?rev=32271#L865).

If the content of your view is static, you can set the `content` property directly,
if it is dynamic, overwrite `getContent`. In most cases an ajax request is needed
to generate the content, so use the `initialize` method and cache the content. Passing
it through `render` will cache it automatically.

This example will display the most recent images.

    ```notranslate
    initialize: function() {
    	var self = this;

    	wp.ajax.post( 'query-attachments', {
    		query: {
    			post_mime_type: 'image'
    		}
    	} )
    	.done( function( response ) {
    		self.render( _.map( response, function( data ) {
    			return '<img src=' + data.sizes.thumbnail.url + ' alt=' + data.alt + '>';
    		} ).join( '' ) );
    	} );
    }
    ```

To add UIUI User interface for editing a view, you need to add an `edit` method.
This example is taken from the gallery view. Use the callback to update the view
with the modified text (shortcodeShortcode A shortcode is a placeholder used within
a WordPress post, page, or widget to insert a form or function generated by a plugin
in a specific location on your site.).

    ```notranslate
    edit: function( text, update ) {
    	var frame = wp.media.gallery.edit( text );

    	frame.state( 'gallery-edit' ).on( 'update', function( selection ) {
    		update( wp.media.gallery.shortcode( selection ).string() );
    	} );

    	frame.on( 'close', function() {
    		frame.detach();
    	} );
    }
    ```

It is not recommended to run JavaScriptJavaScript JavaScript or JS is an object-
oriented computer programming language commonly used to create interactive effects
within web browsers. WordPress makes extensive use of JS for a better user experience.
While PHP is executed on the server, JS executes within a user’s browser. [https://www.javascript.com](https://www.javascript.com/)
in the editor iframeiframe iFrame is an acronym for an inline frame. An iFrame is
used inside a webpage to load another HTML document and render it. This HTML document
may also contain JavaScript and/or CSS which is loaded at the time when iframe tag
is parsed by the user’s browser.. If you need to run scripts it is best to use a
sandbox iframe inside the view. The API will automatically do this for you if it
detects scripts in your content. For example the Google Maps API might alter the
editor’s iframe body. This is not acceptable because the body content is serialised
by TinyMCE on saving.

In this example the content will be put in an iframe and you can do anything you
want in that document.

    ```notranslate
    content: [
    	'<script src="//maps.googleapis.com/maps/api/js?sensor=false"></script>',
    	'<style>img { max-width: none; }</style>',
    	'<div id="map" style="width: 100%;height: 500px;"></div>',
    	'<script>new google.maps.Map( document.getElementById( "map" ), { zoom: 0, center: new google.maps.LatLng( 0, 0 ) } )</script>'
    ].join( '' )
    ```

If you do want to run scripts, use the bind and unbind methods and make sure there
are no memory leaks.

[#4-2](https://make.wordpress.org/core/tag/4-2/), [#dev-notes](https://make.wordpress.org/core/tag/dev-notes/)

### Share this:

 * [  Threads ](https://make.wordpress.org/core/2015/04/23/tinymce-views-api-improvements/?share=threads)
 * [  Mastodon ](https://make.wordpress.org/core/2015/04/23/tinymce-views-api-improvements/?share=mastodon)
 * [  Bluesky ](https://make.wordpress.org/core/2015/04/23/tinymce-views-api-improvements/?share=bluesky)
 * [  X ](https://make.wordpress.org/core/2015/04/23/tinymce-views-api-improvements/?share=x)
 * [  Facebook ](https://make.wordpress.org/core/2015/04/23/tinymce-views-api-improvements/?share=facebook)
 * [  LinkedIn ](https://make.wordpress.org/core/2015/04/23/tinymce-views-api-improvements/?share=linkedin)

 1.  ![](https://secure.gravatar.com/avatar/5be34748c2b478b8b781dccd39818f0362a3f3d63f47ec518335f4312e33096f?
     s=32&d=mm&r=g)
 2.   [bduclos](https://profiles.wordpress.org/bduclos/)  3:49 pm _on_ April 23, 2015
 3.  Brilliant work!
      However if the shortcodeShortcode A shortcode is a placeholder
     used within a WordPress post, page, or widget to insert a form or function generated
     by a plugin in a specific location on your site. content contains HTMLHTML HyperText
     Markup Language. The semantic scripting language primarily used for outputting
     content in web browsers. code, the TinyMCE View no longer works while it was working
     in 4.1. Any workaround?
 4.   * ![](https://secure.gravatar.com/avatar/603a3c8128c0966d4671b093c07634cf9f0f505644b82bfa5dcf57c27e5ded66?
        s=32&d=mm&r=g)
      *  [Ella Iseulde Van Dorpe](https://profiles.wordpress.org/ellatrix/)  11:44 
        pm _on_ April 23, 2015
      * Follow up: [#32078](https://core.trac.wordpress.org/ticket/32078).
 5.  ![](https://secure.gravatar.com/avatar/e7a3f22f73d86c03561aac02f364de7fa780b0ca747fb40d8ef84958a4925768?
     s=32&d=mm&r=g)
 6.   [Sophy](https://profiles.wordpress.org/sophy/)  3:05 am _on_ April 25, 2015
 7.  After update to 4.2, Ctrl+1 = h1, Ctrl+2 = h2…. in TinyMCE editors
 8.   * ![](https://secure.gravatar.com/avatar/603a3c8128c0966d4671b093c07634cf9f0f505644b82bfa5dcf57c27e5ded66?
        s=32&d=mm&r=g)
      *  [Ella Iseulde Van Dorpe](https://profiles.wordpress.org/ellatrix/)  2:32 pm
        _on_ April 25, 2015
      * It should be ctrl+alt on a mac, shift+alt on Windows. Does this not work for
        you?
 9.  ![](https://secure.gravatar.com/avatar/bca37ce64ca9b092bfe4ecc31700763bbc05d80335f0472d2c29deea2dfe151b?
     s=32&d=mm&r=g)
 10.  [bopxb](https://profiles.wordpress.org/bopxb/)  5:49 am _on_ May 12, 2015
 11. Really liking the new APIAPI An API or Application Programming Interface is a 
     software intermediary that allows programs to interact with each other and share
     data in limited, clearly defined ways., thanks for all the hard work!
 12. What would you recommend is the best way to follow development or be notified 
     of any breaking changes? Is there an RSS feedRSS Feed RSS is an acronym for Real
     Simple Syndication which is a type of web feed which allows users to access updates
     to online content in a standardized, computer-readable format. This is the feed.
     I can subscribe to somewhere?
 13. ![](https://secure.gravatar.com/avatar/1dc3bd7b76ecf10025744acf6248b6333d76e1f6a6e3515cf21146750b3cf1bd?
     s=32&d=mm&r=g)
 14.  [cowfields](https://profiles.wordpress.org/cowfields/)  11:04 am _on_ August 
     4, 2015
 15. Hi,
 16. I’ve been playing with this to get a columns system working and for the most part
     it’s behaving. However it’s not playing with shortcodes within shortcodes…or rather
     views within views.
 17. I think it’s a priority thing. For example I had two views / shortcodes that I
     had created.
 18. For clarity, the [column] shortcodeShortcode A shortcode is a placeholder used
     within a WordPress post, page, or widget to insert a form or function generated
     by a plugin in a specific location on your site. could have a [featurebox] shortcode
     inside but *not* the other way around.
 19. I had enqueued the JSJS JavaScript, a web scripting language typically executed
     in the browser. Often used for advanced user interfaces and behaviors. for the
     featurebox for the shortcode and thus TinyMCE would render the featurebox, then
     the shortcode, and the mismatch would end up with some weird results.
 20. However if I put the [column] JS first, then that would render – I am using an
     AJAX request to prerender the shortcodes within the column shortcode so then it’s
     working fine.
 21. So basically my question is thus:
 22. Is there any way to get TinyMCE to treat my custom shortcode before the coreCore
     Core is the set of software required to run WordPress. The Core Development Team
     builds WordPress. wordpress ones? For example captions, galleries, etc? Because
     I might have a [column] shortcode that contains a [gallery] and I need my column
     view to do its thing before gallery.

Comments are closed.

# Post navigation

[← 4.2 Release Dry Run](https://make.wordpress.org/core/2015/04/22/4-2-release-dry-run/)

[WordPress 4.2 Field Notes →](https://make.wordpress.org/core/2015/04/23/wordpress-4-2-field-notes/)