0

I was using this code from Pastebin here to add a form with a file input field to the user profile admin page and it was working perfectly fine, but the problem is that i can't change it to an array of input files.

I want to change the profile_photo to be an array of photos and implement it to the user profile page.

Can someone help me with this?

add_action( 'user_edit_form_tag','make_uploadable_form');

function make_uploadable_form() {
       echo ' enctype="multipart/form-data"';
}

add_action( 'show_user_profile', 'UploadField' );
add_action( 'edit_user_profile', 'UploadField' );

function UploadField( $user ) {
       if ( ! current_user_can( 'edit_user' ) )
               return false;

       $pid = get_user_meta( $user->ID, 'profile_photo', true );
       $img = wp_get_attachment_image( $pid );

       if ( ! empty( $img ) )
        echo "<hr/>".$user->ID;
        echo "<hr/>".$pid;
        echo( $img );
?>

<h3>Extra profile information</h3>

<table class="form-table">
   <tr>
       <th><label for="Upload">Upload</label></th>
           <td>
               <input name="profile_photo" type="file" id="profile_photo" value="" />
           </td>
</table>

<?php
}

add_action( 'personal_options_update', 'save_user_custom' );
add_action( 'edit_user_profile_update', 'save_user_custom' );

function save_user_custom($user_id){

       if($user_id == false)
               return false;

       // If the upload field has a file in it
       if(isset($_FILES['profile_photo'])){
               if(!function_exists('wp_handle_upload'))
                       require_once(ABSPATH.'wp-admin/includes/file.php');

               // Get the type of the uploaded file. This is returned as "type/extension"
               $arr_file_type = wp_check_filetype(basename($_FILES['profile_photo']['name']));
               $uploaded_file_type = $arr_file_type['type'];
               // Set an array containing a list of acceptable formats
               $allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
               // If the uploaded file is the right format
               if(in_array($uploaded_file_type, $allowed_file_types)) {
                       // Options array for the wp_handle_upload function. 'test_upload' => false
                       $upload_overrides = array( 'test_form' => false );

                       // Handle the upload using WP's wp_handle_upload function. Takes the posted file and an options array
                       $uploaded_file = wp_handle_upload($_FILES['profile_photo'], $upload_overrides);

                       // If the wp_handle_upload call returned a local path for the image
                       if(isset($uploaded_file['file'])) {

                               // The wp_insert_attachment function needs the literal system path, which was passed back from wp_handle_upload
                               $file_name_and_location = $uploaded_file['file'];

                               // Generate a title for the image that'll be used in the media library
                               $file_title_for_media_library = 'your title here';

                               // Set up options array to add this file as an attachment
                               $attachment = array(
                                               'post_mime_type' => $uploaded_file_type,
                                               'post_title' => 'Uploaded image ' . addslashes($file_title_for_media_library),
                                               'post_content' => '',
                                               'post_status' => 'inherit'
                               );

                               // Run the wp_insert_attachment function. This adds the file to the media library and generates the thumbnails. If you wanted to attch this image to a post, you could pass the post id as a third param and it'd magically happen.
                               $attach_id = wp_insert_attachment( $attachment, $file_name_and_location );
                               require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                               $attach_data = wp_generate_attachment_metadata( $attach_id, $file_name_and_location );
                               wp_update_attachment_metadata($attach_id,  $attach_data);

                               update_user_meta( $user_id, 'profile_photo', $attach_id );

                       }
               }

       }

}
Tarek
  • 115
  • 13

1 Answers1

0

Try this:
if you write your inputs like this:

<input name="profile_photo[1]" type="file" id="profile_photo" value="" />
<input name="profile_photo[2]" type="file" id="profile_photo" value="" />

You'll have an array named profile_photo with your input files. See there.

Yes92
  • 301
  • 4
  • 15
  • Can you please tell me how to apply it to the rest of the code? – Tarek Oct 22 '17 at 14:30
  • Read the answer to the following question, it explain ho to implement the server side script: https://stackoverflow.com/questions/2704314/multiple-file-upload-in-php – Yes92 Oct 23 '17 at 19:29