Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

wpse98464

By: a guest on May 6th, 2013  |  syntax: PHP  |  size: 3.21 KB  |  views: 52  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. add_action( 'user_edit_form_tag','make_uploadable_form');
  2.  
  3. function make_uploadable_form() {
  4.         echo ' enctype="multipart/form-data"';
  5. }
  6.  
  7. add_action( 'show_user_profile', 'UploadField' );
  8. add_action( 'edit_user_profile', 'UploadField' );
  9.  
  10. function UploadField( $user ) {
  11.         if ( ! current_user_can( 'edit_user' ) )
  12.                 return false;
  13.  
  14.         $pid = get_user_meta( $user->ID, 'profile_photo', true );
  15.         $img = wp_get_attachment_image( $pid );
  16.  
  17.         if ( ! empty( $img ) )
  18.                 echo( $img );
  19. ?>
  20.  
  21. <h3>Extra profile information</h3>
  22.  
  23. <table class="form-table">
  24.     <tr>
  25.         <th><label for="Upload">Upload</label></th>
  26.             <td>
  27.                 <input name="profile_photo" type="file" id="profile_photo" value="" />
  28.             </td>
  29. </table>
  30.  
  31. <?php
  32. }
  33.  
  34. add_action( 'personal_options_update', 'save_user_custom' );
  35. add_action( 'edit_user_profile_update', 'save_user_custom' );
  36.  
  37. function save_user_custom($user_id){
  38.  
  39.         if($user_id == false)
  40.                 return false;
  41.  
  42.         // If the upload field has a file in it
  43.         if(isset($_FILES['profile_photo'])){
  44.                 if(!function_exists('wp_handle_upload'))
  45.                         require_once(ABSPATH.'wp-admin/includes/file.php');
  46.  
  47.                 // Get the type of the uploaded file. This is returned as "type/extension"
  48.                 $arr_file_type = wp_check_filetype(basename($_FILES['profile_photo']['name']));
  49.                 $uploaded_file_type = $arr_file_type['type'];
  50.                 // Set an array containing a list of acceptable formats
  51.                 $allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
  52.                 // If the uploaded file is the right format
  53.                 if(in_array($uploaded_file_type, $allowed_file_types)) {
  54.                         // Options array for the wp_handle_upload function. 'test_upload' => false
  55.                         $upload_overrides = array( 'test_form' => false );
  56.  
  57.                         // Handle the upload using WP's wp_handle_upload function. Takes the posted file and an options array
  58.                         $uploaded_file = wp_handle_upload($_FILES['profile_photo'], $upload_overrides);
  59.  
  60.                         // If the wp_handle_upload call returned a local path for the image
  61.                         if(isset($uploaded_file['file'])) {
  62.  
  63.                                 // The wp_insert_attachment function needs the literal system path, which was passed back from wp_handle_upload
  64.                                 $file_name_and_location = $uploaded_file['file'];
  65.  
  66.                                 // Generate a title for the image that'll be used in the media library
  67.                                 $file_title_for_media_library = 'your title here';
  68.  
  69.                                 // Set up options array to add this file as an attachment
  70.                                 $attachment = array(
  71.                                                 'post_mime_type' => $uploaded_file_type,
  72.                                                 'post_title' => 'Uploaded image ' . addslashes($file_title_for_media_library),
  73.                                                 'post_content' => '',
  74.                                                 'post_status' => 'inherit'
  75.                                 );
  76.  
  77.                                 // 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.
  78.                                 $attach_id = wp_insert_attachment( $attachment, $file_name_and_location );
  79.                                 require_once(ABSPATH . "wp-admin" . '/includes/image.php');
  80.                                 $attach_data = wp_generate_attachment_metadata( $attach_id, $file_name_and_location );
  81.                                 wp_update_attachment_metadata($attach_id,  $attach_data);
  82.  
  83.                                 update_user_meta( $user_id, 'profile_photo', $attach_id );
  84.  
  85.                         }
  86.                 }
  87.  
  88.         }
  89.  
  90. }