• Resolved AndrejKMI

    (@andrejkmi)


    How would I go about setting S3 upload file paths based off of post type?

    It seems like this approach is a good starting point, but I haven’t been able to make it work, perhaps the tag i’m trying to hook into is wrong :
    https://www.ads-software.com/support/topic/custom-upload-path-by-file-type

    Here’s my code:

    function wp_update_attachment_metadata_s3( $data, $post_id ) {
      global $post;
      $type = get_post_type( $post->ID );
      $path = '';
      $post_types = array( 'project', 'post', 'page' );
      switch ($type) {
        case 'project':
          $path = 'project';
          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 'project';
    }
    function dynamic_path_default( $value ) {
      // set up upload/year/month path
      $t = date('m Y');
      $time = explode(" ", $t);
      $month = $time[0];
      $year = $time[1];
      return 'uploads/' . $year . '/' . $month;
    }
    
    add_filter( 'wp_update_attachment_metadata','wp_update_attachment_metadata_s3', 20, 2 );

    Thanks

    https://www.ads-software.com/plugins/amazon-s3-and-cloudfront/

Viewing 15 replies - 1 through 15 (of 18 total)
  • I think you have the filter wp_update_attachment_metadata mistaken. That filter is used for image attachment only.

    Thread Starter AndrejKMI

    (@andrejkmi)

    Ok, that makes sense now, but i’m not sure what filter to use now.
    Do you have any suggestions on how to do what i’m trying to do?

    Much appreciated.

    I need to understand properly what you are trying to do. Are you trying to set dynamically the s3 upload path of an image file uploaded from the edit page/post screen using the set featured image link?…

    Thread Starter AndrejKMI

    (@andrejkmi)

    Sure,
    I’m trying to set custom file upload paths based on what post type they are getting uploaded/attached to.

    For instance, I have a post type called ‘project’, I would like all files uploaded/attached to a project post to be uploaded to /projects/ in S3.

    As a default, I would like regular post types (post,page) to use the default upload path (wp-content/uploads/year/month).

    Hope that is clear.

    Thank You

    Sorry for the delay, was a little busy with stuff. So this is what you should do,insert the below code in your function.

    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) {
       // the rest of your codes...

    Also make sure the plugin settings has path — turned on — , year/month — turned off —

    Thread Starter AndrejKMI

    (@andrejkmi)

    Thanks for the reply, no problem on the delay, your help is greatly appreciated.
    I’m still not getting custom paths. Everything uploads to wp-content/uploads/ still.
    This is my code now:

    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;
        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 'project';
    }
    function dynamic_path_default( $value ) {
      $t = date('m Y');
      $time = explode(" ", $t);
      $month = $time[0];
      $year = $time[1];
      return 'uploads/' . $year . '/' . $month;
    }
    
    add_filter( 'wp_update_attachment_metadata','wp_update_attachment_metadata_s3', 20, 2 );

    The codes are working on my end. Did you turn on the path option in the plugin settings page and also the year/month folder turned off?..

    The codes are working on my end. Did you turn on the path option in the plugin settings page and also the year/month folder turned off?..

    Thread Starter AndrejKMI

    (@andrejkmi)

    Yeah, I have Path on and Year/Month turned off.

    Where exactly did you insert the codes in your theme’s functions.php file?..

    Thread Starter AndrejKMI

    (@andrejkmi)

    I’m putting it at the very bottom of the functions.php file.

    Thread Starter AndrejKMI

    (@andrejkmi)

    Ok, I debugged it and found that it works if EWWW Image Optimizer is disabled.
    Is this a conflict in priority between EWWW and add_filter in this function?

    How can I make the two work together?

    Thanks

    Thread Starter AndrejKMI

    (@andrejkmi)

    When EWWW is enabled this is the $data that is returned.

    PHP Console show:
    Object {ewww_image_optimizer: “Unknown type: application/zip”, file: “2015/03/filename.zip”}

    I have not used the EWWWW plugin, but I managed to go through the files, and yes the plugin is also using the filter wp_update_attachment_metadata. I also noticed the plugin is making calls to the s3 plugin inside this particular filter with a priority number 15 but yours is 20.

    // 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>';
    	        }

    So the ewww plugin is triggering upload of files to s3 by itself,if the s3 plugin is installed, and I think this is the reason for the issue. So change your priority to 14 or less because your filter needs to run first before the ewwww filter and then test with a fresh upload and report your results.

    Thread Starter AndrejKMI

    (@andrejkmi)

    Thanks for all of the help.
    So, changing the priority to below 15 doesn’t work, but commenting out the lines you initially suggested does work. The issue is that commenting out those lines seems to break EWWW in terms of retrieving the image from S3 and it’s changing core plugin code.

    Really appreciate your help.

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Custom Upload Path by Post Type’ is closed to new replies.