Skip to content

Instantly share code, notes, and snippets.

@romerocs
Created May 6, 2021 16:16
Show Gist options
  • Save romerocs/1fb8d34d5826fff04e4d73b954b4832a to your computer and use it in GitHub Desktop.
Save romerocs/1fb8d34d5826fff04e4d73b954b4832a to your computer and use it in GitHub Desktop.
Extend image editing settings in the Wordpress Editor
(function ($, _) {
var media = wp.media;
var sizes = [
'100%'
];
media.events.on('editor:image-edit', function (data) {
var origImageDetails = media.view.ImageDetails;
media.view.ImageDetails = origImageDetails.extend({
initialize: function () {
origImageDetails.prototype.initialize.apply(this, arguments);
this.on('post-render', this.addSizeClass);
this.initSize = this.model.get('size');
},
addSizeClass: function () {
sizes.forEach((size) => {
var sizeTitle = size.charAt(0).toUpperCase() + size.slice(1);
var select = this.$('.embed-media-settings select.size');
var hasSize = select.find('[value="' + size + '"]').length;
if (!hasSize) {
var $opt = $(document.createElement('option')).attr('value', size).text(sizeTitle);
if (this.initSize == size) $opt.attr('selected', 'selected');
select.append($opt);
}
});
}
});
});
}(jQuery, _));
<?php
function admin_scripts($hook){
if($hook == 'post.php'){
wp_enqueue_script( 'admin-scripts', get_template_directory_uri() . '/path/to/admin-scripts.js', array(), '1.0', true );
}
}
add_action('admin_enqueue_scripts', 'admin_scripts');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment