I seem to recall in the back of my mind it uploads first to a temp file and then moves it.
I’m going to look closer.
Ah it seems you are deleting it a bit early your code works – later the files is deleted ..
I got that wrong.
baiscally you are hooking early – and deleting teh file will create warnings because it tries to create the file sizes – warnings are not errors by teh way.
If you WANT TO KEEP the generated file sizes but delete the original this code works
add_action('wp_generate_attachment_metadata', function ($metadata, $attachment_id, $action) {
$file = get_attached_file($attachment_id);
wp_delete_file($file);
}, 10, 3);if you want to delete all generated image stoo you can loop through them
e.g.
add_action( 'wp_generate_attachment_metadata', function ( $metadata, $attachment_id, $action ) {
$file = get_attached_file( $attachment_id );
$path = dirname( $file );
wp_delete_file( $file );
foreach ( $metadata['sizes'] as $size ) {
wp_delete_file( $path . '/' . $size['file'] );
}
}, 10, 3 );
Thread Starter
pasy
(@pasy)
@alanfuller Alan, first of all, thank you for helping me analyze the code, but the code you gave me doesn’t work for me, because it doesn’t seem to listen to the upload of wordpress. In fact, I’m writing a function. I have a picture management website, which provides an api interface. I wrote the code monitoring wordpree media library in function.php. If there is a picture uploaded, upload the picture to api after the picture is uploaded, and then delete it the local picture of wordpress, but save the metadata of the picture. My complete code is as follows:
function custom_upload_to_api($attachment_id) {
$file_path = get_attached_file($attachment_id);
//$file = fopen( $file_path, 'r' );
//$file_name = basename($file_path);
// upload to url
$remote_url="https://images.xxxx.com/api/v1/upload";
$token = generate_token('[email protected]', 'password');
// cURL upload picture
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $remote_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Accept: application/json",
"Content-Type: multipart/form-data",
"Authorization: Bearer " .$token,
));
$post = array(
"file" => new CURLFILE($file_path),
"strategy_id" => 1
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);
$response_data = json_decode($response, true);
if ($response_data["status"]) {
var_dump($response_data);
$key = $response_data["data"]["key"];
add_post_meta($attachment_id, 'wp_attached_api_key', $key, true);
//delete local picture
wp_delete_file($file_path);
} else {
wp_die('error');
}
$upload_url = get_option('upload_url_path');
if( empty( $upload_url ) ) {
update_option( 'upload_url_path', 'https://images.xxxx.com/' );
}
}
add_action( 'add_attachment', 'custom_upload_to_api' );- This reply was modified 8 hours, 50 minutes ago by pasy.
Thread Starter
pasy
(@pasy)
@alanfuller Above is my complete code. May I ask you how can I modify the code? Thank you very much for taking the time to reply to me. I would like to buy you a Starbucks. Please provide an eth virtual coin wallet address, or a paypal payment link. To thank you.
- This reply was modified 8 hours, 41 minutes ago by pasy.
What is wrong with hooking that code to wp_generate_attachment_metadata ? I haven’t tested but to me I can’t see why that would not work.
P.s. there are neater wordpress ways of calling apis.
P.p.s there are neater ways than dropping code into functions.php i.e simple wrap as a plugin.
When I get time I will demonstrate
Thread Starter
pasy
(@pasy)
@alanfuller
‘wp_generate_attachment_metadata’ is a WordPress hook that is triggered when attachment metadata is generated.
‘add_attachment’ is a WordPress hook that is triggered when an attachment (such as an image or a file) is added to the media library.
So if I use wp_ generate_ attachment_ Metadata, this hook is not triggered when uploading pictures from the media files in the background of wordprss
Thread Starter
pasy
(@pasy)
@alanfuller Sorry, I didn’t operate correctly myself. You are right. It works! Please give me your paypal address. I want to buy you a cup of coffee.
Thread Starter
pasy
(@pasy)
Thanks to alan fuller,
Replacing add_attachment with wp_generate_attachment_metadata succeeded.
