• Resolved 温唯

    (@unbosomjam)


    Hi,

    I want to disallow all types of media to s3 bucket for temporally.
    I’ve followed this page https://wordpress.org/support/topic/prevent-upload-to-s3/
    to disallow images offloaded to s3 bucket.
    But it seems not work,images still had been offloaded to s3 bucket.
    Because I want to disallow all kind of media,so I try to add as many media types as I can think of.

    Here is my code:

    function pre_update_attachment_metadata( $abort, $data, $post_id, $old_as3cf_item ) {
    		$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( 'mp4', 'mov', 'jpg', 'jpeg', 'png', 'pdf', 'psd', 'ai', 'cdr' ) ) ) {
    			$abort = true; // abort the upload
    		}
    
    		return $abort;
    	}

    I use “Code Snippet plugin” to add those code and run snippet everywhere, I have no idea what I did wrong.

    Please help me.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You need to hook it.

    Use the below code along with your code.

    add_filter( 'as3cf_pre_update_attachment_metadata', array( $this, 'pre_update_attachment_metadata' ), 10, 4 );

    You can also use the below function.

    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 );

    The issue here is, it’s disallowing the offload for all the file types irrespective of what file type we provide. So this solves your problem I guess.

    温唯

    (@unbosomjam)

    Hi @rajeevrf
    Thank you for taking time replying.
    But that code is not work to me and cause critical error to my site due to add that code
    add_filter( 'as3cf_pre_update_attachment_metadata', array( $this, 'pre_update_attachment_metadata' ), 10, 4 );

    Fatal error: Uncaught Error: Using $this when not in object context in public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code:21 Stack trace: #0 
    /public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446): eval() #1 
    /public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(534): execute_snippet('function pre_up...', 47) #2 
    /public_html/wp-includes/class-wp-hook.php(287): execute_active_snippets('') #3 
    /public_html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) #4 
    /public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #5 
    /public_html/wp-settings.php(409): do_action('plugins_loaded') #6 
    /public_html/wp-config.php(8 in /public_html/wp-content/plugins/code-snippets/php/snippet-ops.php(446) : eval()'d code on line 21
    There has been a critical error on your website. 
    Please check your site admin email inbox for instructions.

    Hi,

    Instead of code snippet plugin, can you try adding the below code directly in the functions.php file?

    Code is working fine for me. Issue has to be with the Code snipper plugin.

    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 );
    • This reply was modified 1 month, 1 week ago by rajeevrf. Reason: Gramatical error
    温唯

    (@unbosomjam)

    Hi @rajeevrf
    Thank you so much for replying.

    Do you mean this file : “wp-config.php” which is under “public_html”?

    and where to add those codes?

    anywhere or at the end or before /* That's all, stop editing! Happy blogging. */

    No not wp-config.php file.

    Inside your theme folder, there should be a functions.php file. Add the code to the bottom of the file and save it.

    温唯

    (@unbosomjam)

    Hi @rajeevrf
    Thank you for taking the time to help me.
    I’ve added those codes to function.php file of child theme , but still caused critical error the same with in Snippet Code Plugin.

    If I delete add_filter( 'as3cf_pre_update_attachment_metadata', array( $this, 'pre_update_attachment_metadata' ), 10, 4 ); from those codes, it won’t cause error but still cannot prevent the image from being uploaded to s3.

    Maybe there is something wrong with my site or theme(OceanWP).
    Anyway,Thank you so much.
    I’ll look for other solution to prevent images from being uploaded to s3 for temporally.

    rajeevrf

    (@rajeevrf)

    If you are adding the code directly to functions.php, then you should remove the code snippet plugin and see.

    Code snippet plugin just adds the code to your functions.php file without editing it directly. Since you are adding the code directly in the file, you should remove the code snippet plugin and see.

    温唯

    (@unbosomjam)

    Hi @rajeevrf
    Thank you for always helping me, I found the solution finally.
    I followed the code you provided above, and modified “add filter”, then it did work.

    The problem is not related to add codes to “Code Snippet plugin” or to “function.php” file , they are the same.

    It’s weird that array( $this, will cause error to my site , So I changed add_filter('as3cf_pre_upload_attachment', array( $this,'pre_upload_attachment' ), 10, 3 ); to add_filter('as3cf_pre_upload_attachment','pre_upload_attachment', 10, 3 ); . After that, it will prevent specific files from been uploaded to s3.

    Here is my code added to “Code Snippet plugin” :

    add_filter( 'as3cf_pre_upload_attachment', 'pre_upload_attachment', 10, 3 );
    	function pre_upload_attachment( $abort, $post_id, $metadata ) {
    		// Example stops image files 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( 'png', 'jpg', 'jpeg' ) ) ) {
    			$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;
    	}

    Hope to help others

    And thank you very much, I am too stupid to know that it’s necessary to add “add filter” to those codes, so that what I did before is invalid.

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