我正在使用这种方法在我的自定义插件中使用媒体上传器。这可能会有所帮助。
在主主题文件(index.php)中添加这些。
wp_enqueue_style('thickbox'); // call to media files in wp
wp_enqueue_script('thickbox');
wp_enqueue_script( 'media-upload');
// load script to admin
function wpss_admin_js() {
$siteurl = get_option('siteurl');
$url = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/js/admin_script.js';
echo "<script type='text/javascript' src='$url'></script>";
}
add_action('admin_head', 'wpss_admin_js');
在admin_script.js文件中,
jQuery('#wpss_upload_image_button').click(function() {
formfield = jQuery('#wpss_upload_image').attr('name');
tb_show('', 'media-upload.php?type=image&TB_iframe=true');
return false;
});
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('#wpss_upload_image').val(imgurl);
tb_remove();
jQuery('#wpss_upload_image_thumb').html("<img height='65' src='"+imgurl+"'/>");
}
管理文件(admin_settings.php),
<div id="wpss_upload_image_thumb" class="wpss-file">
<?php if(isset($record->security_image) && $record->security_image !='') { ?>
<img src="<?php echo $record->security_image;?>" width="65"/><?php } else { echo $defaultImage; } ?>
</div>
<input id="wpss_upload_image" type="text" size="36" name="wpss_upload_image" value="" class="wpss_text wpss-file" />
<input id="wpss_upload_image_button" type="button" value="Upload Image" class="wpss-filebtn" />
更多细节在我的博客