Using the REST API (v2) javascript client on a private namespaced route

To modify the rest url prefix you can filter rest_url_prefix. But that just changes the /wp-json/ prefix that every namespace uses. Trying to modify wp/v2 means modifying the plugin and that namespace is hard-coded in several places like; WP_REST_Post_Statuses_Controller. To add your own custom endpoints, register_rest_route is in core to do just that. <?php add_action( … Read more

Using Underscore Templates in WordPress

Your plugin index file: add_action( ‘print_media_templates’, ‘wpse8170_admin_footer’ ); function wpse8170_admin_footer() { require ‘templates.php’; } Your templates.php: <script id=”tmpl-mytemplate” type=”text/html”> <h1>Hello {{data.name}}!</h1> </script> Your media js file: wp.media.view.MyView = wp.media.View.extend({ template: wp.media.template(‘mytemplate’), render: function() { this.$el.html(this.template({name: ‘world’})); } });

Customizing the 3.5 “Add Media” popup (Backbone.js)

I had do adapt Backbone for a project of mine, so I might help to get you started. If you want to adapt the rendering of your Backbone Templates, you need to rewrite them, because there are no built in filters for existing Templates. add_action(‘print_media_templates’, ‘print_my_media_templates’); function print_my_media_templates() { ?> <script type=”text/html” id=”my-custom-template”> //TODO: Copy … Read more

Open media frame and select an attachment

Well, I found the answer myself. I hope it helps others: I replaced both instances of: if ( selected ) { selection.add( wp.media.attachment( selected ) ); } with: selection.reset( selected ? [ wp.media.attachment( selected ) ] : [] ); Apparently, the reset() function can be used to empty an array and then add elements to … Read more

Add custom class to attachment in media library grid mode

The classes added to each element in the media library grid are dinamically generated in the media-views.js file. Particularly, the code that renders the elements is part of the wp.media.view.Attachment function. It’s a Backbone.js view, so it’s possible to extend the library of that view in order to add the classes or other attributes you … Read more

How does REST API cookie authentication work in WP 4.7?

It looks like you’re missing the nonce part, as explained in the Authentication chapter, in the REST API Handbook: Cookie authentication is the basic authentication method included with WordPress. When you log in to your dashboard, this sets up the cookies correctly for you, so plugin and theme developers need only to have a logged-in … Read more

print_media_templates not applied in media manager plugin

Ok I’ve figured some things out by now: I was not returning the rendered image into anything In the javascript I have a render function media.view.AttachmentsBrowser = media.view.AttachmentsBrowser.extend({ render: function(){ var that = this; if(this.collection){ if(this.collection.models.length > 0){ this.clearImages(); _.each(this.collection.models, function (item){ //old way: that.renderImage(item); //now: this.$el.find(‘#lastli’).before(that.renderImage(item)); }, this); } } } }); Obviously my … Read more