WordPress Front End Multiple File Upload
Do you like this story?
While customizing Wordpress for our suitable cms solutions. We have certain things required from our customers. So it requires front end media upload. Even few things require to gather details and posts from users. If it requires you can also provide your user to upload media files from the front end of your site. May be of my following code can help you to create front end upload with multiple files simultaneously from single from .
You don't need to use any of the following things : flash ,js, jquery, and ajax . It works on all the browsers and simple HTML 5 codes and php code. This will work well on your old browsers like IE 6 . Also works well on your mobile devices.
create HTML form like this
<form action="upload.php" method="post" name="front_end_upload" >
<label> Attach all your files here :<input type="file" name="upload_attachments[]" multiple="multiple" > </label>
<input type="submit" name="Upload" >
</form>
The above HTML form handles the user upload files at your sites front-end. Now code for your back end to store the files.
//upload.php
if( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
if ( $_FILES ) {
$files = $_FILES["upload_attachments"];
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]
);
$_FILES = array ("screenshots" => $file);
foreach ($_FILES as $file => $array) {
$newupload = insert_attachment($file,$pid);
}
}
}
}
}
The above code will handle the operation of getting files and send to your Wordpress function insert_attachment that we have added on your theme fiel "function.php" or your plugin's init action function.
here is the code for insert attachment function
################################################################################
// image Attachment
################################################################################
function insert_attachment($file_handler,$post_id,$set_thu=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 ($set_thu) set_post_thumbnail($post_id, $attach_id);
return $attach_id;
}
that's it. to perform front end multiple file upload.
Subscribe to:
Post Comments (Atom)
1 Responses to “WordPress Front End Multiple File Upload”
16 July 2015 at 12:15
How Do I Chose The Best WordPress SEO Plugin For My Website?
How Do I choose A Good WordPress Hosting Solution?
Can Free WordPress Themes Hurt My SEO?
What’s better for SEO — Installing WordPress On A New Domain, Subdomain Or Subfolder?
Fix WordPress lost posts “404 Error Found” Problem
How to fix Error in database connection
Fix WordPress lost posts “404 Error Found” Problem
11 Best eCommerce wordpress themes
How to fix wordpress Parse Error: syntax error
15 very basic wordpress errors
9 Important Steps to Increase WordPress Loading Speed
Where can I find help with the CSS problems I'm having?
Why does the password emailed to me look weird?
Why do I get an error message about Sending Referrers?
Are there are any problems with using MySQL 4.1.7 for WordPress?
Error Establishing Database Connection
Pretty Permalinks 404 and Images not Working
Do I need to know PHP to use WordPress?
I’ve Heard That WordPress Is Search Engine Optimized (Out-Of-The-Box). Is That True?
I’ve Installed A WordPress SEO Plugin. That Means I Don’t Need An SEO Consultant, Right?
Will Adding A Post To Many WordPress Categories Be Good For My SEO?
Post a Comment