admin管理员组文章数量:1130349
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
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
本文标签: uploadsWhat is the hook to obtain the path and the name of the file that is being uploaded
版权声明:本文标题:uploads - What is the hook to obtain the path and the name of the file that is being uploaded? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749086617a2313875.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论