调出上传及选择界面

首先增加显示界面及呼出按钮

<div style="margin:20px">
	<a id="custom-upload" class="button" href="#">上传</a>
	<span id="preview-image"></span>
</div>

在插件中增加JavaScript库引用

<?php
wp_enqueue_media();
?>

为按钮增加打开事件

<script>
jQuery(document).ready(function(){
	var ashu_upload_frame;
	var value_id;
	jQuery('#custom-upload').live('click',function(event){
		value_id =jQuery( this ).attr('id');
		event.preventDefault();
		if( ashu_upload_frame ){
			ashu_upload_frame.open();
			return;
		}
		ashu_upload_frame = wp.media({
			title: '选择或上传图片', // 窗口标题
			button: {
				text: '选择', // 选择按钮文字
			},
			multiple: false // 是否允许多选
		});
		ashu_upload_frame.on('select',function(){
			attachment = ashu_upload_frame.state().get('selection').first().toJSON();
			jQuery('#preview-image').html('<image src="' + attachment.sizes.thumbnail.url + '" />');
		});
		
		ashu_upload_frame.open();
	});
});
</script>

上面的调用中,attachment为JSON对象,格式如下:

{
    "id": 4,
    "title": "DSC_4949",
    "filename": "DSC_4949.jpg",
    "url": "http://manager.nsb.org/wp-content/uploads/2015/12/DSC_4949.jpg",
    "link": "http://manager.nsb.org/dsc_4949/",
    "alt": "",
    "author": "1",
    "description": "",
    "caption": "",
    "name": "dsc_4949",
    "status": "inherit",
    "uploadedTo": 0,
    "date": "2015-12-24T11:06:11.000Z",
    "modified": "2015-12-24T11:06:11.000Z",
    "menuOrder": 0,
    "mime": "image/jpeg",
    "type": "image",
    "subtype": "jpeg",
    "icon": "http://manager.nsb.org/wp-includes/images/media/default.png",
    "dateFormatted": "2015年12月24日",
    "nonces": {
        "update": "a553bc00c8",
        "delete": "c141377e33",
        "edit": "3834f2dc7d"
    },
    "editLink": "http://manager.nsb.org/wp-admin/post.php?post=4&action=edit",
    "meta": false,
    "authorName": "root",
    "filesizeInBytes": 244761,
    "filesizeHumanReadable": "239 kB",
    "sizes": {
        "thumbnail": {
            "height": 150,
            "width": 150,
            "url": "http://manager.nsb.org/wp-content/uploads/2015/12/DSC_4949-150x150.jpg",
            "orientation": "landscape"
        },
        "medium": {
            "height": 225,
            "width": 300,
            "url": "http://manager.nsb.org/wp-content/uploads/2015/12/DSC_4949-300x225.jpg",
            "orientation": "landscape"
        },
        "large": {
            "height": 767,
            "width": 1024,
            "url": "http://manager.nsb.org/wp-content/uploads/2015/12/DSC_4949-1024x767.jpg",
            "orientation": "landscape"
        },
        "full": {
            "url": "http://manager.nsb.org/wp-content/uploads/2015/12/DSC_4949.jpg",
            "height": 767,
            "width": 1024,
            "orientation": "landscape"
        }
    },
    "height": 767,
    "width": 1024,
    "orientation": "landscape",
    "compat": {
        "item": "",
        "meta": ""
    }
}

上传文件大小限制

<?php
add_filter('upload_size_limit', function() {
	return 100 * 1024; // 限制大小 100 kB
});
?>

或者干脆在php.ini文件中进行全局设置,你懂的!

上传文件类型限制

<?php
add_filter('upload_mimes', function($mime_types) {
	return array(
		// Image formats
		'jpg|jpeg|jpe' => 'image/jpeg',
		'gif' => 'image/gif',
		'png' => 'image/png',
		'bmp' => 'image/bmp',
		'tif|tiff' => 'image/tiff',
		'ico' => 'image/x-icon',
		// Video formats
		'asf|asx|wax|wmv|wmx' => 'video/asf',
		'avi' => 'video/avi',
		'divx' => 'video/divx',
		'flv' => 'video/x-flv',
		'mov|qt' => 'video/quicktime',
		'mpeg|mpg|mpe' => 'video/mpeg',
		'mp4|m4v' => 'video/mp4',
		'ogv' => 'video/ogg',
		'mkv' => 'video/x-matroska',
		// Text formats
		'txt|asc|c|cc|h' => 'text/plain',
		'csv' => 'text/csv',
		'tsv' => 'text/tab-separated-values',
		'ics' => 'text/calendar',
		'rtx' => 'text/richtext',
		'css' => 'text/css',
		'htm|html' => 'text/html',
		// Audio formats
		'mp3|m4a|m4b' => 'audio/mpeg',
		'ra|ram' => 'audio/x-realaudio',
		'wav' => 'audio/wav',
		'ogg|oga' => 'audio/ogg',
		'mid|midi' => 'audio/midi',
		'wma' => 'audio/wma',
		'mka' => 'audio/x-matroska',
		// Misc application formats
		'rtf' => 'application/rtf',
		'js' => 'application/javascript',
		'pdf' => 'application/pdf',
		'swf' => 'application/x-shockwave-flash',
		'class' => 'application/java',
		'tar' => 'application/x-tar',
		'zip' => 'application/zip',
		'gz|gzip' => 'application/x-gzip',
		'rar' => 'application/rar',
		'7z' => 'application/x-7z-compressed',
		'exe' => 'application/x-msdownload',
		// MS Office formats
		'doc' => 'application/msword',
		'pot|pps|ppt' => 'application/vnd.ms-powerpoint',
		'wri' => 'application/vnd.ms-write',
		'xla|xls|xlt|xlw' => 'application/vnd.ms-excel',
		'mdb' => 'application/vnd.ms-access',
		'mpp' => 'application/vnd.ms-project',
		'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
		'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
		'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
		'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
		'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
		'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
		'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
		'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template',
		'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
		'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
		'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
		'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
		'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
		'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
		'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
		'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
		'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
		'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
		'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
		'onetoc|onetoc2|onetmp|onepkg' => 'application/onenote',
		// OpenOffice formats
		'odt' => 'application/vnd.oasis.opendocument.text',
		'odp' => 'application/vnd.oasis.opendocument.presentation',
		'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
		'odg' => 'application/vnd.oasis.opendocument.graphics',
		'odc' => 'application/vnd.oasis.opendocument.chart',
		'odb' => 'application/vnd.oasis.opendocument.database',
		'odf' => 'application/vnd.oasis.opendocument.formula',
		// WordPerfect formats
		'wp|wpd' => 'application/wordperfect',
	);
}, 1, 1);
?>

自动重命名上传的文件

<?php
add_filter('wp_handle_upload_prefilter', function ($file) {
	$time=date("Y-m-d");
	$file['name'] = $time . "" . mt_rand(1,100) . "." . pathinfo($file['name'] , PATHINFO_EXTENSION);
	return $file;
});
?>

为媒体文件添加分类

<?php
add_action('init', function() {
	register_taxonomy_for_object_type('category', 'attachment');
});
?>

为媒体文件添加标签

<?php
add_action('init', function() {
	ister_taxonomy_for_object_type('post_tag', 'attachment');
});
?>
标签: