我正在开发一个新的插件,它可以在上传到自定义文件夹后立即移动上传的图像/文件。我对新媒体上传器有问题(来自 WP 3.5)。我有移动上传的文件并生成附件元数据的功能。我通过调用这个函数add_action(add_attachment, '_the_function')
。问题是,为了完成这个动作,经典媒体库需要在函数末尾打印附件 ID:echo $post_ID
而新媒体上传器需要返回 json 数据return wp_send_json_success( $attachment )
。
如何检查我是否通过新媒体上传器或媒体->添加新媒体上传媒体。
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;
}
}