Support » Plugin: WP Offload Media Lite for Amazon S3, DigitalOcean Spaces, and Google Cloud Storage » How to stop offloading specific file type to S3

  • Resolved rajeevrf

    (@rajeevrf)


    Hi,
    I am using this plugin on my website to offload the uploads to S3. I don’t want CSV files alone offloaded to S3. I want all other file types to be offloaded to S3. I went through a few support threads(https://wordpress.org/support/topic/feature-questions-9/) and found the below code to do this job. But when I use this code, all types of files are uploaded to the UPLOADS folder. But it is supposed to do this only for CSV file types.

    function pre_upload_attachment( $abort, $post_id, $metadata ) {
    		// Example stops csv from being offloaded.
    		$file      = get_post_meta( $post_id, '_wp_attached_file', true );
    		$extension = is_string( $file ) ? pathinfo( $file, PATHINFO_EXTENSION ) : false;
    		if ( is_string( $extension ) && in_array( $extension, array( 'csv' ) ) ) {
    			$abort = true; // abort the upload
    		}
    
    		// Example helps bulk offload tool on severely resource restricted shared hosting.
    		// WARNING: Do not uncomment the following code unless you're on shared hosting and getting "too many open files" errors
    		// as <code>gc_collect_cycles()</code> could potentially impact performance of the bulk offload and WordPress.
    		/*
    		if ( false === $abort ) {
    			gc_collect_cycles();
    		}
    		*/
    
    		return $abort;
    	}
    	
    add_filter( 'as3cf_pre_upload_attachment', array( $this, 'pre_upload_attachment' ), 10, 3 );

    I also tried the “as3cf_allowed_mime_types” function as well. Same result. Can someone help me on this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Delicious Brains Inc.

    (@deliciousbrains)

    If you added the above to a child theme’s functions.php file, you need to change that add_filter call to something like the following…

    add_filter( 'as3cf_pre_upload_attachment', 'pre_upload_attachment', 10, 3 );

    Ideally you’d also rename the function to something unique to your site, try adding something the following to your child theme’s functions.php…

    function rajeevrf_as3cf_pre_upload_attachment( $abort, $post_id, $metadata ) {
    	// Example stops csv from being offloaded.
    	$file      = get_post_meta( $post_id, '_wp_attached_file', true );
    	$extension = is_string( $file ) ? pathinfo( $file, PATHINFO_EXTENSION ) : false;
    	if ( is_string( $extension ) && in_array( $extension, array( 'csv' ) ) ) {
    		$abort = true; // abort the upload
    	}
    
    	return $abort;
    }
    	
    add_filter( 'as3cf_pre_upload_attachment', 'rajeevrf_as3cf_pre_upload_attachment', 10, 3 );
    Thread Starter rajeevrf

    (@rajeevrf)

    Thanks. This worked like a charm.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.