I’ve added a select box audio option to the default gallery settings. Here’s what I added to the functions.php file:
<?php
function add_custom_gallery_settings() {
global $post;
$attached_audio = get_attached_media( 'audio', $post->ID );
?>
<script type="text/html" id="tmpl-custom-gallery-settings">
<label class="setting">
<span><?php _e('Audio'); ?></span>
<select data-setting="audio">
<option value="">No Audio</option>
<?php foreach($attached_audio as $a): ?>
<option value="<?php esc_attr_e($a->ID); ?>">
<?php echo $a->post_name; ?>
</option>
<?php endforeach; ?>
</select>
</label>
</script>
<script>
jQuery(document).ready(function(){
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
render: function() {
wp.media.view.Settings.prototype.render.apply(this, arguments);
this.$el.append(wp.media.template('custom-gallery-settings'));
wp.media.gallery.defaults.audio = null;
this.update.apply( this, ['audio'] );
return this;
}
});
});
</script>
<?php }
add_action('print_media_templates', 'add_custom_gallery_settings');
?>
As you see, select box populates with audio attachments uploaded to the post. Because I generate those templates on print_media_templates hook, any audio files uploaded to the post, won’t show up until I update/save the post and/or reload the page. Question is, what would be a good way to dynamically update those options when an audio attachment is uploaded?