Make WordPress Core

Opened 5 years ago

Closed 3 years ago

Last modified 3 years ago

#50089 closed enhancement (invalid)

Allow filtering of image uploads

Reported by: malthert's profile malthert Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.5
Component: Media Keywords: has-patch close
Focuses: Cc:

Description

I'd like to be able to filter the response of wp_ajax_upload_attachment to add copyright and duplicate checks to prevent the upload of the image if either fails.

To do this I require an apply_filters at the end of the function.

Suggested fix for ajax-actions.php function wp_ajax_upload_attachment:

Replace:

echo wp_json_encode( array(
        'success' => true,
        'data'    => $attachment,
) );

with:

$response = array(
    'success' => true,
    'data'    => $attachment,
);
$response = apply_filters( 'filter_ajax_upload_attachment_response', $response );

echo wp_json_encode( $response );

Change History (4)

#1 @SergeyBiryukov
5 years ago

Hi there, welcome back to WordPress Trac! Thanks for the ticket.

Just noting there are some existing hooks called when uploading a new image:

#2 @malthert
5 years ago

But these are hooks and they can NOT be used to set the success to false in case the upload was cancelled by my custom code. (unless I die() in them, but this makes it really hard to debug code for other people, bc if someone hooked already & died, later hooked actions may actually never run)

#3 @malthert
3 years ago

  • Keywords close added
  • Resolution set to invalid
  • Status changed from new to closed

This has been implemented now already with

<?php
return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta );

in a sub-function.

#4 @desrosj
3 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.