Custom Upload Path by Post Type (S3) EWWW
-
Hi, Thanks for the great plugin.
I’m using the Amazon S3 and Cloudfront plugin to upload to S3 with some custom paths.
The code below uploads the attachement to a specific directory based off of what post type the attachment is uploaded to.
I’m having issues when EWWW is enabled when uploading files. This works when EWWW is disabled.
I’m using ‘wp_update_attachment_metadata’ just as EWWW does, but with a higher priority (11 vs. EWWW’s 15) but am still having issues.
Also, when I go to the Library, non-image files (zip) report “Could not retrieve file path.” under the Image Optimizer column.Here is the block in EWWW common.php that seems to be competing with what i’m trying to do.
Is there a way to make my custom code work well with EWWW?
// file common.php line:1571 if ( ! preg_match( '/' . __( 'Previously Optimized', EWWW_IMAGE_OPTIMIZER_DOMAIN ) . '/', $meta['ewww_image_optimizer'] ) && class_exists( 'Amazon_S3_And_CloudFront' ) ) { global $as3cf; if ( method_exists( $as3cf, 'wp_update_attachment_metadata' ) ) { $as3cf->wp_update_attachment_metadata( $meta, $ID ); } elseif ( method_exists( $as3cf, 'wp_generate_attachment_metadata' ) ) { $as3cf->wp_generate_attachment_metadata( $meta, $ID ); } $ewww_debug .= 'uploading to Amazon S3<br>'; }
Custom Upload Path based off of post_type
function wp_update_attachment_metadata_s3( $data, $post_id ) { $parent_id = get_post_field( 'post_parent', $post_id ); $type = get_post_type( $parent_id ); $path = ''; switch ($type) { case 'project': $path = 'project'; break; case 'product': $path = 'product'; break; default: $path = 'default'; break; } if ( $path ) { add_filter( 'as3cf_setting_object-prefix','dynamic_path_'.$path, 10,1 ); } return $data; } function dynamic_path_project( $value ) { return 'projects'; } function dynamic_path_product( $value ) { return 'product'; } function dynamic_path_default( $value ) { $t = date('m Y'); $time = explode(" ", $t); $month = $time[0]; $year = $time[1]; return 'wp-content/uploads/' . $year . '/' . $month; } add_filter( 'wp_update_attachment_metadata','wp_update_attachment_metadata_s3', 11, 2 );
- The topic ‘Custom Upload Path by Post Type (S3) EWWW’ is closed to new replies.