I want to add a button to the WooCommerce admin page which allows for me to upload a file (e.g. .pdf, .jpg, .png, etc.) to a custom folder which is located in the "Uploads" folder in the directory. The function just keeps reloading to the same page while not uploading the file.
I even tested it out on another WordPress installation, but it strangely redirected to all posts. I’m sure I’m missing something. I’m using Local by Flywheel, locally hosted and everything is up to date. Please help. Thank you in advance.
What I was expecting was to have the ability to upload a document to a custom folder within the uploads folder which I could call/pull documents as need to attach or send to specific users. I have tried the following solutions:
add_action( 'woocommerce_after_order_itemmeta', 'uploaddocs', 10, 1);
function uploaddocs(){
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submittheform">
</form>
<?php
global $wp_filesystem;
WP_Filesystem();
$content_directory = $wp_filesystem->wp_content_dir() . 'uploads/';
$wp_filesystem->mkdir( $content_directory . 'CustomDocuments' );
$target_dir_location = $content_directory . 'CustomDocuments/';
if(isset($_POST["submittheform"])){ //&& isset($_FILES['fileToUpload'])) {
$name_file = $_FILES['fileToUpload']['name'];
$tmp_name = $_FILES['fileToUpload']['tmp_name'];
if( move_uploaded_file( $tmp_name, $target_dir_location.$name_file ) ) {
echo "File was successfully uploaded";
} else {
echo "The file was not uploaded";
}
}
}
Strangely the code works if I place the html form twice.
add_action( 'woocommerce_after_order_itemmeta', 'uploaddocs', 10, 1);
function uploaddocs(){
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submittheform">
</form>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submittheform">
</form>
<?php
global $wp_filesystem;
WP_Filesystem();
$content_directory = $wp_filesystem->wp_content_dir() . 'uploads/';
$wp_filesystem->mkdir( $content_directory . 'CustomDocuments' );
$target_dir_location = $content_directory . 'CustomDocuments/';
if(isset($_POST["submittheform"])){ //&& isset($_FILES['fileToUpload'])) {
$name_file = $_FILES['fileToUpload']['name'];
$tmp_name = $_FILES['fileToUpload']['tmp_name'];
if( move_uploaded_file( $tmp_name, $target_dir_location.$name_file ) ) {
echo "File was successfully uploaded";
} else {
echo "The file was not uploaded";
}
}
}
I even asked ChapGPT for help and I ran into the same issue. The AI code solution is this:
add_action( 'woocommerce_after_order_itemmeta', 'custom_file_upload_form', 10, 3 );
function custom_file_upload_form( $item_id, $item, $product ) {
// Output the form only for line items
if ( $item->get_type() !== 'line_item' ) return;
// Output the form
echo '<form action="" method="post" enctype="multipart/form-data">';
echo '<input type="file" name="custom_document" id="custom_document_' . esc_attr( $item_id ) . '">';
echo '<input type="hidden" name="action" value="upload_custom_document">';
echo '<input type="hidden" name="order_item_id" value="' . esc_attr( $item_id ) . '">';
echo '<input type="submit" value="Upload Document">';
echo '</form>';
}
add_action( 'init', 'handle_custom_document_upload' );
function handle_custom_document_upload() {
// Debugging: Check if the function is triggered
error_log('Form submitted.');
if ( isset( $_POST['action'] ) && $_POST['action'] == 'upload_custom_document' ) {
error_log('Custom document upload action triggered.');
if ( isset( $_FILES['custom_document'] ) ) {
error_log('File detected: ' . print_r($_FILES['custom_document'], true));
$file = $_FILES['custom_document'];
// Handle the file upload
$upload = wp_upload_bits( $file['name'], null, file_get_contents( $file['tmp_name'] ) );
if ( ! $upload['error'] ) {
error_log('File upload successful.');
$upload_dir = wp_upload_dir();
$custom_dir = $upload_dir['basedir'] . '/CustomDocuments';
if ( ! file_exists( $custom_dir ) ) {
wp_mkdir_p( $custom_dir );
}
$new_file = $custom_dir . '/' . basename( $upload['file'] );
if ( move_uploaded_file( $file['tmp_name'], $new_file ) ) {
error_log('File moved to CustomDocuments folder.');
} else {
error_log('Error moving file.');
}
} else {
error_log('Error in file upload: ' . $upload['error']);
}
} else {
error_log('No file detected.');
}
}
}
I unfortunately had the same issues. If I repeat the html form twice then the functions work. However on the new installation it doesn’t work. Here’s the code repeating:
add_action( 'woocommerce_after_order_itemmeta', 'custom_file_upload_form', 10, 3 );
function custom_file_upload_form( $item_id, $item, $product ) {
// Output the form only for line items
if ( $item->get_type() !== 'line_item' ) return;
// Output the form
echo '<form action="" method="post" enctype="multipart/form-data">';
echo '<input type="file" name="custom_document" id="custom_document_' . esc_attr( $item_id ) . '">';
echo '<input type="hidden" name="action" value="upload_custom_document">';
echo '<input type="hidden" name="order_item_id" value="' . esc_attr( $item_id ) . '">';
echo '<input type="submit" value="Upload Document">';
echo '</form>';
// Output the form
echo '<form action="" method="post" enctype="multipart/form-data">';
echo '<input type="file" name="custom_document" id="custom_document_' . esc_attr( $item_id ) . '">';
echo '<input type="hidden" name="action" value="upload_custom_document">';
echo '<input type="hidden" name="order_item_id" value="' . esc_attr( $item_id ) . '">';
echo '<input type="submit" value="Upload Document">';
echo '</form>';
}
add_action( 'init', 'handle_custom_document_upload' );
function handle_custom_document_upload() {
// Debugging: Check if the function is triggered
error_log('Form submitted.');
if ( isset( $_POST['action'] ) && $_POST['action'] == 'upload_custom_document' ) {
error_log('Custom document upload action triggered.');
if ( isset( $_FILES['custom_document'] ) ) {
error_log('File detected: ' . print_r($_FILES['custom_document'], true));
$file = $_FILES['custom_document'];
// Handle the file upload
$upload = wp_upload_bits( $file['name'], null, file_get_contents( $file['tmp_name'] ) );
if ( ! $upload['error'] ) {
error_log('File upload successful.');
$upload_dir = wp_upload_dir();
$custom_dir = $upload_dir['basedir'] . '/CustomDocuments';
if ( ! file_exists( $custom_dir ) ) {
wp_mkdir_p( $custom_dir );
}
$new_file = $custom_dir . '/' . basename( $upload['file'] );
if ( move_uploaded_file( $file['tmp_name'], $new_file ) ) {
error_log('File moved to CustomDocuments folder.');
} else {
error_log('Error moving file.');
}
} else {
error_log('Error in file upload: ' . $upload['error']);
}
} else {
error_log('No file detected.');
}
}
}
2
Answers
WordPress uploads require JavaScript / Ajax to avoid the issue you are facing. Try the following code replacement:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
Related:
I test your code it is work!! but if i want to add a multiple file can?