4

I've noticed that since WP 3.5 that we now have the Backbone.js and Underscore.js libraries included. The new Media Modal uses Underscore templates and I was wondering how would I be able to insert them, especially in the admin side of things.Is there a correct way to insert these?

8

1 Answer 1

7

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'}));
    }
});
1
  • How do we trigger the view?
    – Sisir
    Commented Sep 13, 2020 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.