Created
May 6, 2021 16:16
-
-
Save romerocs/1fb8d34d5826fff04e4d73b954b4832a to your computer and use it in GitHub Desktop.
Extend image editing settings in the Wordpress Editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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, _)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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