Javascript code inside “” in core WordPress files .php

These are Underscore Templates, handled by the wp.template() JavaScript function (source here) which takes in a template ID corresponding to a <script type=”text/html” id=”tmpl-{template ID}”> element containing such code and compiles it into a function. This function can then be executed with the variables used in the template in order to produce a string of … Read more

Issue with using Underscore in WordPress

You need to set it as a dependency for your script, this is the best way you can make sure, Underscore.js is loaded before your script (which means, the methods will be available). function loadAdminScripts() { wp_register_style( ‘admin-style’, plugins_url(‘admin-style.css’, __FILE__) ); wp_register_script( ‘pluginAdminPage’, plugins_url(‘pluginAdminPage.js’, __FILE__), [ ‘jquery’, ‘underscore’, ] ); wp_enqueue_style(‘admin-style’); wp_enqueue_script(‘pluginAdminPage’); } add_action(‘admin_enqueue_scripts’, ‘loadAdminScripts’); … Read more

What type of template are WP media-modal’s templates?

Here’s an interesting part from http://underscorejs.org/#template If ERB-style delimiters aren’t your cup of tea, you can change Underscore’s template settings to use different symbols to set off interpolated code. Define an interpolate regex to match expressions that should be interpolated verbatim, an escape regex to match expressions that should be inserted after being HTML-escaped, and … Read more

Can I add custom attributes while adding inline scripts?

Here’s one demo suggestion: add_action( ‘admin_enqueue_scripts’, function() { wp_enqueue_script( ‘my-script’, ‘/my-script.js’, [‘underscore’, ‘backbone’], ‘1.0’ ); wp_add_inline_script( ‘my-script’, ‘alert(“hello world”);’ ); // Add our template if( function_exists( ‘wpse_add_inline_tmpl’ ) ) wpse_add_inline_tmpl( $handle=”my-script”, $id = ‘my-tmpl’, $tmpl=”<div class=”section intro”>{{{ data.section.intro }}}</div>” ); } ); where we define our custom wpse_add_inline_tmpl() function as: function wpse_add_inline_tmpl( $handle, $id, $tmpl … 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’})); } });

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