2013-07-05 57 views
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; 

    } 

} 

回答

0

好吧,我发现以下解决方案:

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; 
相关问题