Posted By

roman on 08/07/07


Tagged

plugin wordpress


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

jauhari
willcodeforfood


Modified profile-picture.php plugin for WordPress


 / Published in: PHP
 

This is a modified version of the plugin labeled Author Profile Plugin in the WordPress support forum. The modifications allows anyone who can edit_users to view and upload profile picture. I did not write this from scratch. I only modified it.

  1. <?php
  2. /*
  3. Plugin Name: Author Profile Picture
  4. Plugin URI: http://geekgrl.net/2007/01/02/profile-pics-plugin-release/
  5. Description: Adds picture to Author profile
  6. Version: 0.1
  7. Author: Hannah Gray
  8. Author URI: http://geekgrl.net
  9. */
  10.  
  11. // Get stored options -- substitute defaults if none exist
  12. $profile_picture_options = get_option("profile_picture_options");
  13. $image_dir = (isset($profile_picture_options['image_dir']) && ($profile_picture_options['image_dir'] != '') ? $profile_picture_options['image_dir'] : '/wp-content/uploads/authors/');
  14. $image_extensions = (isset($profile_picture_options['image_extensions']) && ($profile_picture_options['image_extensions'] != '')? $profile_picture_options['image_extensions'] : 'gif png jpg');
  15. $image_default = (isset($profile_picture_options['image_default']) && ($profile_picture_options['image_default'] != '') ? $profile_picture_options['image_default'] : 'default.jpg');
  16. $gravatar_width = (isset($profile_picture_options['gravatar_width']) && ($profile_picture_options['gravatar_width'] != '') ? $profile_picture_options['gravatar_width'] : '80');
  17.  
  18. // Add actions to appropriete hooks
  19. add_action('show_user_profile', 'add_userpic_fields');
  20. add_action('edit_user_profile', 'add_userpic_fields');
  21. add_action('profile_update','upload_pic',1);
  22. add_action('admin_menu', 'profile_picture_config');
  23.  
  24. //*** GUI FUNCTION: add menu item for plugin config to Options page
  25. function profile_picture_config() {
  26. global $wpdb;
  27. if (function_exists('add_options_page')){
  28. add_options_page('Profile Picture', 'Profile Picture', 8, __FILE__, 'profile_picture_conf_page');
  29. }
  30. }
  31.  
  32. //*** GUI FUNCTION: Show config form
  33. function profile_picture_conf_page() {
  34. global $image_dir, $image_extensions, $gravatar_width, $image_default;
  35. ?>
  36. <div class="wrap">
  37. <h2>Profile Picture Options</h2>
  38. <?php
  39. // if submit was pressed, process config data
  40. if ( isset($_POST['submit']) ) {
  41. // check user permissions
  42. if ( !current_user_can('manage_options') ) {
  43. die(__('Cheatin&#8217; uh?'));
  44. // if okay, store data
  45. } else {
  46. $profile_picture_options = array();
  47. $profile_picture_options['image_extensions'] = (isset($_POST['image_extensions']) ? strtolower($_POST['image_extensions']) : '');
  48. $profile_picture_options['image_dir'] = (isset($_POST['image_dir']) ? $_POST['image_dir'] : '');
  49. $profile_picture_options['image_default'] = (isset($_POST['image_default']) ? $_POST['image_default'] : '');
  50. $profile_picture_options['gravatar_width'] = (isset($_POST['gravatar_width']) ? $_POST['gravatar_width'] : '');
  51. update_option('profile_picture_options', $profile_picture_options);
  52. echo "<b>Settings saved</b>";
  53. }
  54. // if submit not pressed, display config options
  55. } else {
  56.  
  57. ?>
  58.  
  59. <form action="" method="post" id="picture_uploader" style="margin: auto;">
  60. <p><b><label>Profile Pics Upload Directory: * </label></b><input size="45" name='image_dir' value='<?php _e(($image_dir == "") ? "wp-content/uploads/authors/" : $image_dir); ?>' style="font-family: 'Courier New', Courier, mono;" /><br />
  61. Recommended: wp-content/uploads/authors/ &nbsp; *must be set to chmod 777 </p>
  62.  
  63. <p><b><label>Allowed File Extensions: </label></b><input size="45" name='image_extensions' value='<?php _e(($image_extensions == "") ? 'png gif jpg' : $image_extensions); ?>' style="font-family: 'Courier New', Courier, mono;" /><br />
  64. Seperate each three digit extension with a space; field is case-insensitive</p>
  65.  
  66. <p><b><label>Standard Width for Comment Author "Gravatar": </label></b><input size="45" name='gravatar_width' value='<?php _e(($gravatar_width == "") ? '80' : $gravatar_width); ?>' style="font-family: 'Courier New', Courier, mono;" /><br />
  67. Width in px</p>
  68.  
  69. <p><b><label>Default Image: </label></b><input size="45" name='image_default' value='<?php _e(($image_default == "") ? 'default.jpg': $image_default); ?>' style="font-family: 'Courier New', Courier, mono;" /><br />
  70. Must be stored in the profile pics directory specified above</p>
  71.  
  72. <p class="submit"><input type="submit" name="submit" value="<?php _e('Update Settings&raquo;'); ?>" /></p>
  73. </form>
  74. </div>
  75. <?php
  76. }
  77. }
  78.  
  79. //*** GUI FUNCTION: displays "add picture" box when editing your profile
  80. function add_userpic_fields() {
  81. global $user_ID, $user_id, $image_extensions;
  82.  
  83. $user_to_show = $user_ID;
  84. if(!empty($user_id))
  85. $user_to_show = $user_id;
  86.  
  87. // build extension check string for the js
  88. $image_extensions_array = explode(' ', $image_extensions);
  89. $checkstr = "";
  90. foreach ($image_extensions_array as $count => $exe) {
  91. $checkstr .= "(ext != '.$exe') && ";
  92. }
  93. $checkstr = rtrim($checkstr, ' && ');
  94.  
  95. // HTML GUI, js changes form encoding and adds error check
  96. ?>
  97. <script type="text/javascript" language="javascript">
  98. <!--
  99.  
  100. function uploadPic() {
  101. document.profile.enctype = "multipart/form-data";
  102. var upload = document.profile.picture.value;
  103. upload = upload.toLowerCase();
  104. var ext = upload.substring((upload.length-4),(upload.length));
  105. if (<?php _e($checkstr) ?>){
  106. alert('Please upload an image with one of the following extentions: <?php _e($image_extensions); ?>');
  107.  
  108. }
  109. }
  110. //-->
  111. </script>
  112. <fieldset>
  113. <legend>Profile Picture</legend>
  114. <p><label>Current: <br />
  115. <img src="<?php _e(author_image_path($user_to_show)); ?>" width="150" /><br /></label></p>
  116. <p><label>Upload a New Picture: <input type="file" name="picture" onchange="uploadPic();" /><br />
  117. </label></p>
  118. </fieldset>
  119. <?php
  120. }
  121.  
  122. //*** INTERNAL FUNCTION: stores pic submitted via profile editing page
  123. function upload_pic() {
  124. global $image_dir, $user_ID, $image_extensions, $user_id;
  125.  
  126. $user_to_edit = $user_ID;
  127. if ( !empty($user_id) && current_user_can('edit_user', $user_id) )
  128. $user_to_edit = $user_id;
  129.  
  130. $raw_name = (isset($_FILES['picture']['name'])) ? $_FILES['picture']['name'] : "";
  131. // if file was sumbitted, continue
  132. if ($raw_name != "") {
  133. // delete previous image if it's there
  134. $image_extensions_array = explode(' ', $image_extensions);
  135. foreach ($image_extensions_array as $image_extension) {
  136. $old_pic_path = clean_path(ABSPATH . '/' . $image_dir . '/' . $user_to_edit . '.' . $image_extension);
  137. if ( file_exists($old_pic_path) ) {
  138. unlink($old_pic_path);
  139. }
  140. }
  141. // build the path and filename
  142. $clean_name = ereg_replace("[^a-z0-9._]", "", ereg_replace(" ", "_", ereg_replace("%20", "_", strtolower($raw_name))));
  143. $file_ext = substr(strrchr($clean_name, "."), 1);
  144. $file_path = clean_path(ABSPATH . '/' . $image_dir . '/' . $user_to_edit . '.' . $file_ext);
  145. // store file
  146. move_uploaded_file($_FILES['picture']['tmp_name'], $file_path);
  147. // Set correct file permissions
  148. $stat = @ stat(dirname($file_path));
  149. $perms = $stat[mode] & 0007777;
  150. $perms = $perms & 0000666;
  151. @ chmod($file_path, $perms);
  152.  
  153.  
  154. } else {
  155. return false;
  156. }
  157. }
  158.  
  159. //*** TEMPLATE FUNCTION: returns requested dimension from specific image
  160. // USAGE:
  161. // path: absolute path to image from server root',
  162. // dimension: the dimension you want, can be either 'height' or width'
  163. // display: display results (ie. echo)? true or false
  164. function author_image_dimensions($path, $dimension, $display = false) {
  165. $size = getimagesize($path);
  166. $width = $size[0];
  167. $height = $size[1];
  168.  
  169. switch ($dimension) {
  170. case 'width':
  171. if ($display) { echo $width; } else { return $width; }
  172. break;
  173. case 'height':
  174. if ($display) { echo $height; } else { return $height; }
  175. break;
  176. }
  177. }
  178.  
  179.  
  180.  
  181. //*** TEMPLATE FUNCTION: returns image for comment author
  182. // USAGE:
  183. // authorID: id number of author
  184. // tags: attributes to include in img tag (optional, defaults to no tags)
  185. function author_gravatar_tag($authorID, $tags = '') {
  186. global $gravatar_width;
  187. if ($authorID != 0) {
  188. $path = author_image_path($authorID, false, 'absolute');
  189. $width = $gravatar_width;
  190. $height = author_image_dimensions($path, 'height') * ($gravatar_width / author_image_dimensions($path, 'width'));
  191. $tag = '<img src="' . author_image_path($authorID, false, 'url') . '" width=' . $width . ' height=' . $height . ' '. $tags . ' />';
  192. return $tag;
  193. } else {
  194. return false;
  195. }
  196. }
  197.  
  198.  
  199. //*** TEMPLATE FUNCTION: returns image for author wrapped in image tag
  200. // USAGE:
  201. // authorID: id number of author
  202. // tags: attributes to include in img tag (optional, defaults to no tags)
  203. // display: display results (ie. echo)? true or false (optional, defaults to true)
  204. function author_image_tag($authorID, $tags = '', $display = true) {
  205. $path = author_image_path($authorID, false, 'absolute');
  206. $width = author_image_dimensions($path, 'width');
  207. $height = author_image_dimensions($path, 'height');
  208. $tag = '<img src="' . author_image_path($authorID, false, 'url') . '" width=' . $width . ' height=' . $height . ' '. $tags . ' ' . ' id="authorpic" />';
  209. if ($display) { echo $tag; } else { return $tag; }
  210. }
  211.  
  212. //*** TEMPLATE FUNCTION: returns url or absolute path to author's picture
  213. // USAGE:
  214. // authorID: id number of author
  215. // display: display results (ie. echo)? true or false (optional, defaults to true)
  216. // type: specify what kind of path requested: 'url' or 'absolute' (optional, defaults to url)
  217. function author_image_path($authorID, $display = true, $type = 'url') {
  218. switch($type) {
  219. case 'url' :
  220. $ref = clean_path(get_settings('siteurl') . pick_image($authorID));
  221. if ($display) { echo $ref; } else { return $ref; }
  222. break;
  223. case 'absolute':
  224. $ref = clean_path(ABSPATH . pick_image($authorID));
  225. if ($display) { echo $ref; } else { return $ref; }
  226. break;
  227. }
  228. }
  229.  
  230.  
  231. //*** INTERNAL FUNCTION: strips extra slashes from paths; means user-end
  232. // configuration is not picky about leading and trailing slashes
  233. function clean_path($dirty_path) {
  234. $nasties = array(1 => "///", 2 => "//", 3 => "http:/");
  235. $cleanies = array(1 => "/", 2 => "/", 3 => "http://");
  236. $clean_path = str_replace($nasties, $cleanies, $dirty_path);
  237. return $clean_path;
  238. }
  239.  
  240. //*** INTERNAL FUNCTION: finds the appropriete path to the author's picture
  241. function pick_image($authorID) {
  242. global $image_dir, $image_extensions, $image_default;
  243. $image_extensions_array = explode(' ', $image_extensions);
  244. // look for image file based on user id
  245. $path = "";
  246. foreach ($image_extensions_array as $image_extension) {
  247. $path_fragment = '/' . $image_dir . '/' . $authorID . '.' . $image_extension;
  248. $path_to_check = clean_path(ABSPATH . $path_fragment);
  249. if ( file_exists($path_to_check) ) {
  250. $path = $path_fragment;
  251. break;
  252. }
  253. }
  254. // if not found, use default
  255. if ($path == "") {
  256. $path = '/' . $image_dir . '/' . $image_default;
  257. }
  258. return $path;
  259. }
  260. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: jauhari on August 31, 2007

Is it possible to add this plugin on Loop? in front of WordPress? if possible, how to do that?

Posted By: roman on September 11, 2007

If you are asking whether this plugin can be used with the loop like inside of index, single or page.php, then not really. This is designed to work with author.php using getuserdata. You could of course run the getuserdata within one of the files mentioned above, before or after the loop to have it show-up.

Posted By: benkepes on November 8, 2007

i get this

Warning: moveuploadedfile(/var/www/html/wp-content/uploads/authors/1.jpg): failed to open stream: No such file or directory in /var/www/html/wp-content/plugins/addUserPhoto.php on line 146

Warning: moveuploadedfile(): Unable to move '/tmp/phpS4dHq7' to '/var/www/html/wp-content/uploads/authors/1.jpg' in /var/www/html/wp-content/plugins/addUserPhoto.php on line 146

Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-content/plugins/addUserPhoto.php:146) in /var/www/html/wp-includes/pluggable.php on line 391

any ideas to a fix???

Posted By: roman on November 23, 2007

The first two are the same problem, the file that you uploaded was supposed to be saved to /tmp/phpS4dHq7 to /var/www/html/wp-content/uploads/authors/1.jpg, my guess would be that the directory /var/www/html/wp-content/uploads/authors/ does not exist and that the directory /var/www/html/wp-content/uploads/ is not writable. To give write permission On unix/linux/solaris... chmod a+w -R /var/www/html/wp-content/uploads The third warning is that the headers were already sent (because there were errors, php outputs text, which means the headers are sent out which means new headers can't be added later), fix the first two and the third one will dispear.

You need to login to post a comment.