static::getFileName( $metaIdentifier ), 'type' => 'image/jpg', 'tmp_name' => $temp_file, 'error' => 0, 'size' => filesize( $temp_file ), ); $overrides = [ // Tells WordPress to not look for the POST form // fields that would normally be present as // we downloaded the file from a remote server, so there // will be no form fields // Default is true 'test_form' => false, // Setting this to false lets WordPress allow empty files, not recommended // Default is true 'test_size' => true, ]; // Move the temporary file into the uploads directory $results = wp_handle_sideload( $file, $overrides ); if ( ! empty( $results['error'] ) ) { return false; } $localUrl = $results['url']; // URL to the file in the uploads dir // Perform any actions here based in the above results return $localUrl; } /** * Generate file name based on passed meta data. * File name could not be 100% in this method. * This method is considered to be used together with wp_handle_sideload(), * which is responsible for uniquness and postfix file when non-unique. * * @param array $meta Some meta data to be used to generate the hash. * * @return false|string False on failure. */ public static function getFileName( $meta ) { if ( empty( $meta ) ) { return false; } $meta[] = uniqid( time() ); $fileName = md5( serialize( $meta ) ); return sprintf( '%s.jpg', $fileName ); } }