0
我工作的一個新的插件,可以在上傳到自定義文件夾後立即上傳圖片/文件。我有一個新的媒體上傳器(來自WP 3.5)的問題。我有移動上傳文件並生成附件元數據的功能。我通過add_action(add_attachment, '_the_function')
調用這個函數。問題是,爲完成此操作,經典媒體庫需要在該功能的末尾打印附件ID:echo $post_ID
,新媒體上傳器需要返回json數據return wp_send_json_success($attachment)
。檢查我們是否在新媒體上傳或媒體 - >添加新/ WordPress
如何檢查是否通過新媒體上傳器或媒體 - >添加新媒體上傳媒體。
add_action('add_attachment', array($this, 'up_move_media_after_add_attachment'));
function up_move_media_after_add_attachment($post_ID) {
//... foregoing code (moving to custom folder) ...
# generates metadata for an image attachment and creates a thumbnail and other intermediate sizes of the image attachment
$metadata = wp_generate_attachment_metadata($post_ID, get_attached_file($post_ID));
wp_update_attachment_metadata ($post_ID, $metadata);
if (___???????___) {
# upload from post via new media uploader, since 3.5
$attachment = wp_prepare_attachment_for_js($post_ID);
return wp_send_json_success($attachment);
} else {
# upload from media library
echo $post_ID;
# don't forget
exit;
}
}