Reputation: 1
I work on a new a plugin which get possible to move uploade image/file immediately after the uploading to custom folder. I have problem with a new media uploader (from WP 3.5). I have function which move the uploaded file and generate attachment metadata. I call this function via add_action(add_attachment, '_the_function')
. The problem is that for finishing this action the classic media library need to print the attachment ID on the end of the function: echo $post_ID
and the new media uploader need return json data return wp_send_json_success( $attachment )
.
How can I check if I upload media via new media uploader or in Media->Add New.
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;
}
}
Upvotes: 0
Views: 348
Reputation: 1
Well, I've found following solution:
if ( basename($_SERVER['HTTP_REFERER']) == 'media-new.php' ):
# upload from media library
echo $post_ID;
# don't forget
exit;
else :
# upload from post via new media uploader, since 3.5
$attachment = wp_prepare_attachment_for_js( $post_ID );
return wp_send_json_success( $attachment );
endif;
Upvotes: 0