检查我们是否在新媒体上传器或Media->添加新/ WordPress

时间:2013-07-05 09:10:34

标签: wordpress upload media

我正在处理一个新的插件,可以在上传到自定义文件夹后立即移动上传图像/文件。我有新媒体上传器的问题(来自WP 3.5)。我有移动上传文件并生成附件元数据的功能。我通过add_action(add_attachment, '_the_function')调用此函数。问题是,为了完成此操作,经典媒体库需要在函数末尾打印附件ID:echo $post_ID并且新媒体上传者需要返回json数据return wp_send_json_success( $attachment )

如何通过新媒体上传器或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;

    }

}

1 个答案:

答案 0 :(得分: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;