Skip to content

Instantly share code, notes, and snippets.

@maxkostinevich
Created December 29, 2015 19:24
Show Gist options
  • Save maxkostinevich/dfa0a0f9fe8add57851a to your computer and use it in GitHub Desktop.
Save maxkostinevich/dfa0a0f9fe8add57851a to your computer and use it in GitHub Desktop.
WP-Admin: Add fields to Gallery Settings
<?php
add_action('print_media_templates', function(){
// define your backbone template;
// the "tmpl-" prefix is required,
// and your input field should have a data-setting attribute
// matching the shortcode name
?>
<script type="text/html" id="tmpl-my-custom-gallery-setting">
<label class="setting">
<span><?php _e('My setting'); ?></span>
<select data-setting="my_custom_attr">
<option value="foo"> Foo </option>
<option value="bar"> Bar </option>
<option value="default_val"> Default Value </option>
</select>
</label>
</script>
<script>
jQuery(document).ready(function(){
// add your shortcode attribute and its default value to the
// gallery settings list; $.extend should work as well...
_.extend(wp.media.gallery.defaults, {
my_custom_attr: 'default_val'
});
// merge default gallery settings template with yours
wp.media.view.Settings.Gallery = wp.media.view.Settings.Gallery.extend({
template: function(view){
return wp.media.template('gallery-settings')(view)
+ wp.media.template('my-custom-gallery-setting')(view);
}
});
});
</script>
<?php
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment