0

I work in a plugin in which I need to obtain information of the current file that is uploaded. To this end, in the plugin I use a filter as follows.

add_filter('wp_handle_upload', 'custom_handle_upload', 10, 1);

function custom_handle_upload($upload) {
    $file = $upload['file'];
    $type = $upload['type'];

    if (hasValidType($type) && hasValidName($file)) {
        parseXLSX($file);
    }
}

If the file has the valid type and the name is correct, then it is processed. Otherwise the import should follow the normal flow.

But it is not the case, the file is uploaded to the upload directory but no data about the file is added to the database.

So I end up with broken references to the file.

When debugging the process and get to the function media_handle_upload located in wp-admin/includes/media.php I discover that the value of $file is null

$file = wp_handle_upload($_FILES[$file_id], $overrides, $time);

If I deactivate the plugin, the import works as expected.

In order to solve, first try to change filter to action but the same thing happens. At this moment I think I should use another hook. I do not know what it could be and I do not know if it is correct.

What is the hook to obtain the path and the name of the file that is being uploaded?

I appreciate your opinion

Update: Actual solution

When uploading, the function media_handle_upload located at wp-admin/includes/media.php is invoked and at some point it call wp_handle_upload method. This method on success, returns an associative array of file attributes, so from the custom method I return the uploaded file. At the moment it looks like this

function custom_handle_upload($upload) {
    $file = $upload['file'];
    $type = $upload['type'];

    if (hasValidType($type) && hasValidName($file)) {
        parseXLSX($file);
    }

    return $upload;
}

And this allow me to upload common, and target files with it data

  • Is this an upload you triggered? Or is this trying to hook into arbitrary media library uploads? Have you confirmed that the problem isn't with your if statement check? – Tom J Nowell Dec 18 '18 at 17:56
  • I am using media library uploads to upload the file, I can confirm the problem is not in the if statement. I am new to WordPress and because I do not know the terms I mention that the file I upload using the options in the media library in the site administration – user615274 Dec 18 '18 at 18:02
  • I got to fix the problem. I will update the question – user615274 Dec 18 '18 at 18:21
  • Can you post the answer as an answer? – Tom J Nowell Dec 18 '18 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.