First Of all we need a Form
jQuery('body').on('change','#files',function(){
jQuery(this).parents('form').submit();
});
jQuery("form#data").submit(function(event){
event.preventDefault();
var formData = new FormData(jQuery(this)[0]);
jQuery.ajax({
url: ajaxUrl+'?action=cvf_upload_files',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (returndata) {
var obj = jQuery.parseJSON(returndata);
UploadedImageContain = obj;
var attachment = [];
if(jQuery("#attachment").length>=1){
var attachmentData = jQuery("#attachment").val();
attachmentData = attachmentData.split(',');
}
if(jQuery("ul#imagesContainerGallery").length>=1){
jQuery("#imagesContainerGallery").remove();
}
jQuery("
").insertAfter(jQuery("#files").parent('form'));
var cList = jQuery('ul#imagesContainerGallery');
jQuery('.ImageSection').find('option').remove();
jQuery('.ImageSection').append(jQuery('', {
value: '',
text :'Select'
}));
jQuery('.ImageSection').append(jQuery('', {
value: 'media',
text :'Media Library'
}));
var index = 1;
jQuery.each(obj,function(k,v){
attachment.push(k);
var li =jQuery('
.addClass('ui-menu-item')
.attr('role', 'menuitem')
.appendTo(cList);
var aaa =jQuery('')
.attr('src', v)
.appendTo(li);
jQuery('.ImageSection').append(jQuery('', {
value: k,
text :'Photo'+index
}));
index++;
});
jQuery('.ImageSection').append(jQuery('', {
value: 'NO',
text :'No Photo'
}));
var attachment_new = jQuery('')
.addClass('attachementCustom')
.attr('value',attachment.toString())
.attr('name','attachment')
.attr('id',"attachment")
.attr('type',"hidden")
if(jQuery("#attachment").length>=1){
jQuery("#attachment").remove();
}
jQuery(attachment_new).insertBefore("#files");
}
});
return false;
});
Now next thing would be ajax function
function cvf_upload_files_callback(){
$parent_post_id = isset( $_POST['post_id'] ) ? $_POST['post_id'] : 0;
$valid_formats = array("jpg", "png", "gif", "bmp", "jpeg");
$max_file_size = 1024 * 500;
$max_image_upload = 10;
$wp_upload_dir = wp_upload_dir();
$path = $wp_upload_dir['path'] . '/';
$count = 0;
$attach_id_array = array();
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $parent_post_id,
'exclude' => get_post_thumbnail_id()
) );
if( $_SERVER['REQUEST_METHOD'] == "POST" ){
foreach ( $_FILES['files']['name'] as $f => $name ) {
$extension = pathinfo( $name, PATHINFO_EXTENSION );
$new_filename = $this->cvf_td_generate_random_code( 20 ) . '.' . $extension;
if ( $_FILES['files']['error'][$f] == 4 ) {
continue;
}
if ( $_FILES['files']['error'][$f] == 0 ) {
if ( $_FILES['files']['size'][$f] > $max_file_size ) {
$upload_message[] = "$name is too large!.";
continue;
} elseif( ! in_array( strtolower( $extension ), $valid_formats ) ){
$upload_message[] = "$name is not a valid format";
continue;
} else{
if( move_uploaded_file( $_FILES["files"]["tmp_name"][$f], $path.$new_filename ) ) {
$count++;
$filename = $path.$new_filename;
$filetype = wp_check_filetype( basename( $filename ), null );
$wp_upload_dir = wp_upload_dir();
$attachment = array(
'guid' => $wp_upload_dir['url'] . '/' . basename( $filename ),
'post_mime_type' => $filetype['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, $parent_post_id );
$attach_id_array[] = $attach_id;
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
}
}
}
}
}
if ( isset( $upload_message ) ) :
foreach ( $upload_message as $msg ){
printf( __('
%s
', 'wp-trade'), $msg );
}
endif;
if( $count != 0 ){
//printf( __('
%d files added successfully!
', 'wp-trade'), $count );
}
$attachmentData = array();
if(!empty($attach_id_array)):
for($i=0;$i<count($attach_id_array);$i++){
$attachmentData[$attach_id_array[$i]] = wp_get_attachment_url($attach_id_array[$i]);
}
endif;
echo json_encode($attachmentData);
exit();
}
function cvf_td_generate_random_code($length=10) {
$string = '';
$characters = "23456789ABCDEFHJKLMNPRTVWXYZabcdefghijklmnopqrstuvwxyz";
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters)-1)];
}
return $string;
}