你所要做的就是加载几个脚本,准确地说是四个。
首先注册您的widget类加载媒体库,传递一些变量以正确翻译内容(有关详细信息,请参阅WordPress的翻译标准)在顶部添加您自己的脚本以触发模式窗口并处理文件上载,首先将您的小部件注册为类,然后放入脚本:
<?php
// We register our widget
function wpse_register_my_custom_widget(){
register_widget(\'The_Custom_Image_Upload_Widget\');
}
add_action(\'widgets_init\', \'wpse_register_my_custom_widget\');
// in your plugin main file or functions.php when in a theme ..
function wpse_load_media_library(){
// Set $pagenow to global to check, where to load the scripts..
global $pagenow;
// make a variable for the path in case you don\'t have it
$dir = plugins_url(\'/my-plugin-folder\');
// register your own script first
wp_register_script(
// WP internal name (so called "handle") this is important as you need to relate to this name later on
\'my-js-file-with-code\',
$dir . \'/components/js/the-custom-image-upload-actions.js\',
array(\'jquery\'),
false,
true
);
// Now let\'s kick in our scripts
if($pagenow === \'widgets.php\'){
// Load/enqueue the media library, this is what you didn\'t find and struggled with..
wp_enqueue_media();
// load your own script with the trigger of the modal
wp_enqueue_script(\'my-js-file-with-code\');
// Then pass some variables to wordpress in order to translate things in Javascript
wp_localize_script(
// WP Handle/ID of the script to translate
\'my-js-file-with-code\',
// random varname (array) which holds the strings
\'scripttext\',
array( // array with strings
\'title\' => __(\'Logo upload\', \'textdomain\'),
\'button\' => __(\'use this logo\', \'textdomain\'),
\'multiple\' => __(\'use these images\', \'textdomain\')
)
);
}
add_action(\'admin_enqueque_scripts\', \'wpse_load_media_library\');
?>
// My JS file with code
<script>
// image Upload
$(\'#trigger_to_open_the_modal\').click(function(e){
var single_image_frame;
e.preventDefault();
if ( single_image_frame ) {
single_image_frame.open();
return;
}
single_image_frame = wp.media.frames.single_image_frame = wp.media({
title: scripttext.title,
button: { text: scripttext.button },
library: { type: \'image\' }
});
single_image_frame.on(\'select\', function(){
var media_attachment = single_image_frame.state().get(\'selection\').first().toJSON();
// Adjust this to your needs, pops in the url to the img tag
$(\'#custom-upload-logo\').attr(\'src\', media_attachment.url);
$(\'#logo_url\').val(media_attachment.url);
});
single_image_frame.open();
});
</script>
因为我不知道您的标记,所以我没有编写任何小部件的HTML示例。我写这篇文章花了大约一个小时,希望你不要介意。