如果要将“添加媒体”按钮添加到admin 面板:
您需要使用wp\\u enqueue\\u media();
add_action ( \'admin_enqueue_scripts\', function () {
if (is_admin ())
wp_enqueue_media ();
} );
然后使用此js:
jQuery(document).ready(function() {
var $ = jQuery;
if ($(\'.set_custom_images\').length > 0) {
if ( typeof wp !== \'undefined\' && wp.media && wp.media.editor) {
$(\'.set_custom_images\').on(\'click\', function(e) {
e.preventDefault();
var button = $(this);
var id = button.prev();
wp.media.editor.send.attachment = function(props, attachment) {
id.val(attachment.id);
};
wp.media.editor.open(button);
return false;
});
}
}
});
使用此html:
<p>
<input type="number" value="" class="regular-text process_custom_images" id="process_custom_images" name="" max="" min="1" step="1">
<button class="set_custom_images button">Set Image ID</button>
</p>