Guest User

Untitled

a guest
Dec 15th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. <?php
  2. add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
  3. add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
  4.  
  5. function my_show_extra_profile_fields( $user ) {
  6. ?>
  7. <script type="text/javascript">
  8. var form = document.getElementById('your-profile');
  9. form.encoding = "multipart/form-data";
  10. form.setAttribute('enctype', 'multipart/form-data');
  11. </script>
  12. <table class="form-table">
  13. <tr>
  14. <th><label for="profile_photo">Upload your profile photo</label></th>
  15. <td>
  16. <p>
  17. <input type="file" name="profile_photo" id="profile_photo" />
  18. <input type="hidden" name="action" value="save">
  19. <input type="submit" name="submitprofilephoto" id="submitprofilephoto" class="button" value="بارگذاری">
  20. </p>
  21. <span class="description">
  22. <?php
  23. $author_profile_photo = get_author_profile_photo($user->ID);
  24. if(is_array($author_profile_photo)):
  25. ?>
  26. <a href="<?php echo $author_profile_photo["url"];?>" target="_blank">
  27. <img src="<?php echo $author_profile_photo["file"];?>" height="100" width="100" />
  28. </a>
  29. <?php
  30. endif;
  31. ?>
  32. </span>
  33. </td>
  34. </tr>
  35. </table>
  36.  
  37. <?php
  38. }
  39.  
  40. function get_author_profile_photo($user_ID) {
  41.  
  42. $author_data = get_the_author_meta( 'profile_photo', $user_ID );
  43. $uploads = wp_upload_dir();
  44. $author_data["file"] = $uploads["baseurl"] . $author_data["file"];
  45. return $author_data;
  46. }
  47.  
  48. add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
  49. add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
  50.  
  51. function my_save_extra_profile_fields( $user_id ) {
  52.  
  53. if ( !current_user_can( 'edit_user', $user_id ) )
  54. return false;
  55.  
  56. $upload=$_FILES['profile_photo'];
  57. $uploads = wp_upload_dir();
  58. if(isset($_POST) && $_POST['submitprofilephoto']!='') {
  59. if ($upload['tmp_name'] && file_is_displayable_image( $upload['tmp_name'] )) {
  60.  
  61. // handle the uploaded file
  62. $overrides = array('test_form' => false);
  63. $file=wp_handle_upload($upload, $overrides);
  64. $file["file"] = $uploads["subdir"]."/".basename($file["url"]);
  65.  
  66. // Setup the array of supported file types. In this case, it's just Images.
  67. $supported_types = array( 'image/jpeg', 'image/pjpeg', 'image/png' );
  68.  
  69. // Get the file type of the upload
  70. $arr_file_type = wp_check_filetype(basename($upload['name']));
  71. $uploaded_type = $arr_file_type['type'];
  72.  
  73. // Check if the type is supported. If not, throw an error.
  74. if( $file && in_array($uploaded_type, $supported_types) ) {
  75. if($upload['size'] > 204800) {
  76. wp_die('<strong>ERROR</strong>: Maximum size allowed is 200KB.');
  77. }
  78.  
  79. //remove previous uploaded file
  80. $author_profile_photo = get_author_profile_photo($user_id);
  81. @unlink($author_profile_photo["file"]);
  82. // I even tried unlink without the @ and it still doesn't delete the file!
  83.  
  84. update_user_meta( $user_id, 'profile_photo', $file );
  85.  
  86. } else wp_die('<strong>ERROR</strong>: Allowd image formats are JPEG, JPG and PNG.');
  87.  
  88. } elseif (!file_is_displayable_image( $upload['tmp_name'] )) wp_die('<strong>ERROR</strong>: The file you selected is not an image!');
  89. }
  90. }
  91. ?>
  92.  
  93. function url_to_path_test($url){
  94. $url=str_replace(rtrim(get_site_url(),'/').'/', ABSPATH, $url);
  95. return $url;
  96. }
  97.  
  98. //remove previous uploaded file
  99. $author_profile_photo = get_author_profile_photo($user_id);
  100. $url=str_replace(rtrim(get_site_url(),'/').'/', ABSPATH, $author_profile_photo["file"]);
  101. @unlink($url);
  102.  
  103. //remove previous uploaded file
  104. $author_data = get_the_author_meta( 'profile_photo', $user_id );
  105. $author_data["file"] = $uploads["path"] . $author_data["file"];
  106.  
  107. $imagepath = $author_data["file"];
  108. @unlink($imagepath);
  109.  
  110. unlink(public_path('file/to/delete'))
  111.  
  112. @unlink($author_profile_photo["file"]);
Add Comment
Please, Sign In to add comment