Advertisement
CodeDropz

WooCommerce Pro - Validating File After Upload Hook

Aug 31st, 2023 (edited)
1,645
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. /*
  2. @param : $files return
  3. Array
  4. (
  5.     [path] => /tmp_uploads/
  6.     [file] => ten.jpg
  7.     [preview] => http://localhost/wc-pro-old/wp-includes/images/media/default.png
  8.     [show_thumbnail] => 1
  9.     [is_image] => 1
  10.     [ext] => jpg
  11. )
  12. */
  13.  
  14. add_action('dndmfu_wc_pro_upload_complete', function( $files ){
  15.    
  16.     // Begin validation here ( checking filename or validating file extension )
  17.     if( isset( $files['file'] ) ){
  18.        
  19.         // File check or validation here
  20.         if( $files['file'] == 'ten.jpg' ){
  21.            
  22.             // WP upload url
  23.             $base_dir = DNDMFU_WC_PRO_MAIN::get_instance()->wp_upload_dir['basedir'];
  24.            
  25.             // Concat upload path + filename
  26.             $path = realpath( trailingslashit( $base_dir ) . $files['path'] . $files['file'] );
  27.            
  28.             // Delete file
  29.             DNDMFU_WC_PRO_FUNCTIONS::get_instance()->dndmfu_wc_delete_file( $path );
  30.            
  31.             // Return an error (must json error format)
  32.             wp_send_json_error( 'Error: Invalid file etc...' );
  33.            
  34.         }
  35.     }
  36.    
  37.     return $files;
  38. });
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement