following PHP code allow users to upload files frontend in wordpress I've searched hours but I could not manage to display URLs of uploaded files.
I put in index.php
<?php
if ( isset( $_POST['upload_attachments'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' && wp_verify_nonce($_POST['secure_upload'], 'upload_attachments_nonce')) {
//checking if upload is empty
//checking if universal filesize is valid
if ($_FILES) { //loop through multiple files.
$files = $_FILES['upload'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$uploaded_file_type = $files['type'][$key];
$allowed_file_types = array('application/x-zip-compressed', 'image/jpg', 'image/jpeg', 'image/png', 'application/pdf', 'application/zip', 'video/mp4');
$uploaded_file_size = $files['size'][$key];
$size_in_kb = $uploaded_file_size / 1024;
$file_size_limit = 10000; // Your Filesize in KB
if(in_array($uploaded_file_type, $allowed_file_types)) {
if( $size_in_kb > $file_size_limit ) {
$upload_error .= 'Image files must be smaller than '.$file_size_limit.'KB';
return;
} else {
$_FILES = array("upload" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post_id);
//return; this loop neds to run multiple times so no return here
}
}
} else { $upload_error .= 'Invalid File type';
return;
}
}
}
}
header ('Location: ' . $_SERVER['REQUEST_URI']);//Post, redirect and get
exit();
}//end of nonce check ?>
and this is functions.php
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
//if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
additional question this code wont allow to upload rar file it uploads zip files only, is it possible to upload with it rar files too?
Thanks in advance
following PHP code allow users to upload files frontend in wordpress I've searched hours but I could not manage to display URLs of uploaded files.
I put in index.php
<?php
if ( isset( $_POST['upload_attachments'] ) && $_SERVER['REQUEST_METHOD'] === 'POST' && wp_verify_nonce($_POST['secure_upload'], 'upload_attachments_nonce')) {
//checking if upload is empty
//checking if universal filesize is valid
if ($_FILES) { //loop through multiple files.
$files = $_FILES['upload'];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$uploaded_file_type = $files['type'][$key];
$allowed_file_types = array('application/x-zip-compressed', 'image/jpg', 'image/jpeg', 'image/png', 'application/pdf', 'application/zip', 'video/mp4');
$uploaded_file_size = $files['size'][$key];
$size_in_kb = $uploaded_file_size / 1024;
$file_size_limit = 10000; // Your Filesize in KB
if(in_array($uploaded_file_type, $allowed_file_types)) {
if( $size_in_kb > $file_size_limit ) {
$upload_error .= 'Image files must be smaller than '.$file_size_limit.'KB';
return;
} else {
$_FILES = array("upload" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$post_id);
//return; this loop neds to run multiple times so no return here
}
}
} else { $upload_error .= 'Invalid File type';
return;
}
}
}
}
header ('Location: ' . $_SERVER['REQUEST_URI']);//Post, redirect and get
exit();
}//end of nonce check ?>
and this is functions.php
function insert_attachment($file_handler,$post_id,$setthumb='false') {
// check to make sure its a successful upload
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attach_id = media_handle_upload( $file_handler, $post_id );
//if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
additional question this code wont allow to upload rar file it uploads zip files only, is it possible to upload with it rar files too?
Thanks in advance
Share Improve this question asked Nov 30, 2019 at 16:13 DaF-StudentDaF-Student 678 bronze badges1 Answer
Reset to default 1To allow uploading files which are not in the 'regular' list of MIME's you have to tell WordPress to add them to that list.
As an example you find below code which adds those extensions to the already existing MIMES list.
Please make a backup first
/**
* Source: @link https://developer.wordpress/reference/hooks/upload_mimes/
*
*/
add_filter( 'upload_mimes', 'add_custom_upload_mimes' );
function add_custom_upload_mimes( $existing_mimes )
{
return array_merge( $existing_mimes, array(
'rar' => 'application/rar'
));
return $existing_mimes;
}
It takes the existing array of allowed file types ($existing_mimes) first and adds the 'new' additionally to it.
The 'new' array will then be returned to WordPress.
I hope this will help you on your way when it is about allowing 'new' file.ext to upload.
edit: Simplified code, and perhaps the MIME type given for .rar extension (changed it into application, see code) was wrong?!