mardi 24 mars 2015

how to Update multiple Image in registration plugin?


I try to update multiple image upload in my plugin. I hook my code in admin profile part. I get image from usermeta table. My insert multiple image is work but update is not working. I create code for update multiple images as below:



<?php

/*
* Admin Registration profile.
*
*/

add_action( 'show_user_profile', 'extra_user_profile_fields' );
add_action( 'edit_user_profile', 'extra_user_profile_fields' );
function extra_user_profile_fields( $user ) { ?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>

<table class="form-table">
<tr>
<th><label><?php _e("User Profile:"); ?></label></th>
<td><input type="file" name="user_upload[]" id="user_upload" multiple="true" /></td>
</tr>
<tr>
<th></th>
<td>
<?php $images = get_the_author_meta( 'Images', $user->ID );
//echo $images; //output is '279,280,281'.
$image = explode(',',$images);
foreach($image as $img)
{
?>
<img src="<?=wp_get_attachment_url( $img );?>" width="100" height="100" />
<input type="checkbox" name="usergallery[]" value="<?=$img?>" />
// check image for delete

<?php } ?>


</td>
</tr>

</table>


<?php
}
add_action( 'personal_options_update', 'save_extra_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_extra_user_profile_fields' );

function save_extra_user_profile_fields( $user_id ) {


$attaches='';

if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}

$upload_overrides = array( 'test_form' => false );
$attach_array=array();

$files=$_FILES['user_upload'];
print_r($files);
if(count($files['name'])>0)
{
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]
);
$movefile=wp_handle_upload($file, $upload_overrides);
$attach_array[]=$movefile['file'];
}
}
$attach_id_array=array();
require_once( ABSPATH . 'wp-admin/includes/image.php' );
foreach($attach_array as $aa)
{
$filename = $aa;
$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,0 );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
$attach_id_array[] = $attach_id;
}
$attaches=implode(',',$attach_id_array);
}

$existimages=get_the_author_meta( 'Images', $user_id );


$existimages=explode(',',$existimages);
$removeimages=$_POST['usergallery'];
if(count($removeimages)>0)
{
$subset = array_diff($existimages,$removeimages);
if(count($subset)>0)
{
$existimages = $subset;
}
else
{
$existimages='';
}
}
$existimages=implode(',',$existimages);
foreach($removeimages as $ri)
{
wp_delete_attachment( $ri,true);
}
if($attaches!='')
{
$attaches = $attaches.','.$existimages;
}
else
{
$attaches = $existimages;
}
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }

update_user_meta( $user_id, 'Images', $attaches);
}

?>


This code is work at front end but not work in admin part. my other field is update successfully and work proper in admin part. So, please guide me.





Aucun commentaire:

Enregistrer un commentaire