Domanda

I am working on a custom endpoint to handle ajax media uploads with media_handle_upload().

I have created a custom endpoint with a route that is tested and works, however when I try require the necessary files so that I can use media_handle_upload(), it returns a 500 internal server error with each request but still uploads the media correctly in to the media library.

I have tried to figure this out for a while now but I dont understand why it's happening.

Hoping somebody here can point me in the right direction!

To clearify, everything works as expected, the internal server error does not hinder the upload to happen correctly, but the error occurs with every request, no matter what.

Here is the function to upload: (path to custom routes file = /inc/custom-routes/custom-route.php)

function uploadFileFE ($data) {

              if ( isset( $_FILES["file"] ) ) {

                require ABSPATH . 'wp-admin/includes/image.php';
                require ABSPATH . 'wp-admin/includes/file.php';
                require ABSPATH . 'wp-admin/includes/media.php';

                  $attachment_id = media_handle_upload( 'file', 0 );

                  if ( is_wp_error( $attachment_id ) ) {
                      // There was an error uploading the image.
                      echo $attachment_id->get_error_message();
                  } else {
                      // The image was uploaded successfully!
                      echo $attachment_id;
                  }

              } else {

                  echo "The empty check failed! Something went horribly wrong!";
              }

             wp_die();

          }

Update (with working code, adjusted from answer below)

So, the answer that Sally CJ gave was correct, I'm updating with the new code so that if anyone else does the same mistake I did, they'll see the complete working code.

function uploadFileFE ($data) {

          if ( isset( $_FILES["file"] ) ) {

            //$glSiteUrl = get_site_url();

            // These files need to be included as dependencies when on the front end.
            require ABSPATH . 'wp-admin/includes/image.php';
            require ABSPATH . 'wp-admin/includes/file.php';
            require ABSPATH . 'wp-admin/includes/media.php';

              $attachment_id = media_handle_upload( 'file', 0 );

              if ( is_wp_error( $attachment_id ) ) {
                  // There was an error uploading the image.
                  return $attachment_id->get_error_message();
              } else {
                  // The image was uploaded successfully!
                  return $attachment_id;
              }

          } else {

              return "The empty check failed! Something went horribly wrong!";
          }

        }
È stato utile?

Soluzione

From the wp_die() reference,

$args
(string|array|int) (Optional) Arguments to control behavior. If $args is an integer, then it is treated as the response code.

'response' (int) The HTTP response code. Default 200 for Ajax requests, 500 otherwise.

So your REST API endpoint returns the error 500 because that's the default header status sent by the wp_die function.

And I recommend you to return a proper value — e.g. a WP_Error instance on error, or a WP_REST_Response instance on success. Try these variations and compare the responses:

Option 1

// Works, but with a 500 status code.
function uploadFileFE() {
    wp_die();
}

Option 2

// Works, but shouldn't be used unless the client is *not* expecting a JSON response.
function uploadFileFE() {
    echo 'foo';
}

Option 3

// I'd use this. WordPress will apply rest_ensure_response() to the returned value.
function uploadFileFE() {
    return 'foo';
    //return WP_REST_Response( 'foo' ); // or use this, or below to send an error..
    //return new WP_Error( 'invalid_file', 'Invalid file!', array( 'status' => 400 ) );
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a wordpress.stackexchange
scroll top