这是我们如何在Easy Digital Downloads中进行操作的完整示例:
/**
* Set Upload Directory
*
* Sets the upload dir to edd. This function is called from
* edd_change_downloads_upload_dir()
*
* @since 1.0
* @return array Upload directory information
*/
function edd_set_upload_dir( $upload ) {
$upload['subdir'] = '/edd' . $upload['subdir'];
$upload['path'] = $upload['basedir'] . $upload['subdir'];
$upload['url'] = $upload['baseurl'] . $upload['subdir'];
return $upload;
}
/**
* Change Downloads Upload Directory
*
* Hooks the edd_set_upload_dir filter when appropriate. This function works by
* hooking on the WordPress Media Uploader and moving the uploading files that
* are used for EDD to an edd directory under wp-content/uploads/ therefore,
* the new directory is wp-content/uploads/edd/{year}/{month}. This directory
* provides protection to anything uploaded to it.
*
* @since 1.0
* @global $pagenow
* @return void
*/
function edd_change_downloads_upload_dir() {
global $pagenow;
if ( ! empty( $_REQUEST['post_id'] ) && ( 'async-upload.php' == $pagenow || 'media-upload.php' == $pagenow ) ) {
if ( 'download' == get_post_type( $_REQUEST['post_id'] ) ) {
add_filter( 'upload_dir', 'edd_set_upload_dir' );
}
}
}
add_action( 'admin_init', 'edd_change_downloads_upload_dir', 999 );