Custom User Profile Photo - Version 0.4

Version Description

  • Major Update - please be sure to backup your site and db.
  • The plugin now overrides the WordPress avatar by filtering the get_avatar() hook.
  • The get_cupp_meta() function still exists and can be used to customize the output (this will eventually be deprecated).
Download this release

Release Info

Developer 3five
Plugin Icon wp plugin Custom User Profile Photo
Version 0.4
Comparing to
See all releases

Code changes from version 0.3 to 0.4

Files changed (2) hide show
  1. 3five_cupp.php +60 -10
  2. readme.txt +21 -16
3five_cupp.php CHANGED
@@ -7,7 +7,7 @@ Author: 3five
7
  Author URI: http://3five.com
8
  Text Domain: custom-user-profile-photo
9
  Domain Path: /languages/
10
- Version: 0.3
11
  */
12
 
13
  /**
@@ -16,7 +16,11 @@ Version: 0.3
16
  * It is distributed as free software with the intent that it will be
17
  * usefull and does not ship with any WARRANTY.
18
  *
19
- * USAGE:
 
 
 
 
20
  * <?php $imgURL = get_cupp_meta( $user_id, $size ); ?>
21
  * or
22
  * <img src="<?php echo get_cupp_meta( $user_id, $size ); ?>">
@@ -26,11 +30,10 @@ Version: 0.3
26
  * @param $size Default: 'thumbnail'. Accepts all default WordPress sizes and any custom sizes made by the add_image_size() function.
27
  * @return {url} Use this inside the src attribute of an image tag or where you need to call the image url.
28
  *
29
- * Inquiries, suggestions and feedback can be sent to vincent@3five.com
30
  *
31
- * This is plugin is intented for Contributors, Editors and Admin role post/page authors. Thank you for downloading our plugin.
32
- * We hope this WordPress plugin meets
33
- * your needs.
34
  *
35
  * Happy coding!
36
  * - 3five
@@ -39,7 +42,6 @@ Version: 0.3
39
  * • Steven Slack - http://s2web.staging.wpengine.com/226/
40
  * • Pippin Williamson - https://gist.github.com/pippinsplugins/29bebb740e09e395dc06
41
  * • Mike Jolley - https://gist.github.com/mikejolley/3a3b366cb62661727263#file-gistfile1-php
42
- * • Frankie Jarrett - http://frankiejarrett.com/get-an-attachment-id-by-url-in-wordpress/
43
  *
44
  */
45
 
@@ -131,7 +133,7 @@ function cupp_profile_img_fields( $user ) {
131
  </table><!-- end form-table -->
132
  </div> <!-- end #cupp_container -->
133
 
134
- <?php wp_enqueue_media(); // Enqueue the WordPress MEdia Uploader ?>
135
 
136
  <?php }
137
 
@@ -158,13 +160,20 @@ function cupp_save_img_meta( $user_id ) {
158
  * @return {url} Use this inside the src attribute of an image tag or where you need to call the image url.
159
  */
160
  function get_cupp_meta( $user_id, $size ) {
 
161
 
162
  //allow the user to specify the image size
163
  if (!$size){
164
  $size = 'thumbnail'; // Default image size if not specified.
165
  }
166
- if(!$user_id){
167
- $user_id = $post->post_author;
 
 
 
 
 
 
168
  }
169
 
170
  // get the custom uploaded image
@@ -195,4 +204,45 @@ function get_cupp_meta( $user_id, $size ) {
195
  return $image_url;
196
  }
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
  ?>
7
  Author URI: http://3five.com
8
  Text Domain: custom-user-profile-photo
9
  Domain Path: /languages/
10
+ Version: 0.4
11
  */
12
 
13
  /**
16
  * It is distributed as free software with the intent that it will be
17
  * usefull and does not ship with any WARRANTY.
18
  *
19
+ * USAGE
20
+ * // Default:
21
+ * This will override the WordPress get_avatar hook
22
+ *
23
+ * // Custom placement:
24
  * <?php $imgURL = get_cupp_meta( $user_id, $size ); ?>
25
  * or
26
  * <img src="<?php echo get_cupp_meta( $user_id, $size ); ?>">
30
  * @param $size Default: 'thumbnail'. Accepts all default WordPress sizes and any custom sizes made by the add_image_size() function.
31
  * @return {url} Use this inside the src attribute of an image tag or where you need to call the image url.
32
  *
33
+ * Inquiries, suggestions and feedback can be sent to support@3five.com
34
  *
35
+ * This is plugin is intented for Author, Editor and Admin role post/page authors. Thank you for downloading our plugin.
36
+ * We hope this WordPress plugin meets your needs.
 
37
  *
38
  * Happy coding!
39
  * - 3five
42
  * • Steven Slack - http://s2web.staging.wpengine.com/226/
43
  * • Pippin Williamson - https://gist.github.com/pippinsplugins/29bebb740e09e395dc06
44
  * • Mike Jolley - https://gist.github.com/mikejolley/3a3b366cb62661727263#file-gistfile1-php
 
45
  *
46
  */
47
 
133
  </table><!-- end form-table -->
134
  </div> <!-- end #cupp_container -->
135
 
136
+ <?php wp_enqueue_media(); // Enqueue the WordPress Media Uploader ?>
137
 
138
  <?php }
139
 
160
  * @return {url} Use this inside the src attribute of an image tag or where you need to call the image url.
161
  */
162
  function get_cupp_meta( $user_id, $size ) {
163
+ global $post;
164
 
165
  //allow the user to specify the image size
166
  if (!$size){
167
  $size = 'thumbnail'; // Default image size if not specified.
168
  }
169
+ if(!$user_id || !is_numeric( $user_id ) ){
170
+ // Here we're assuming that the avatar being called is the author of the post.
171
+ // The theory is that when a number is not supplied, this function is being used to
172
+ // get the avatar of a post author using get_avatar() and an email address is supplied
173
+ // for the $id_or_email parameter. We need an integer to get the custom image so we force that here.
174
+ // Also, many themes use get_avatar on the single post pages and pass it the author email address so this
175
+ // acts as a fall back.
176
+ $user_id = $post->post_author;
177
  }
178
 
179
  // get the custom uploaded image
204
  return $image_url;
205
  }
206
 
207
+ /**
208
+ * WordPress Avatar Filter
209
+ *
210
+ * Replaces the WordPress avatar with your custom photo using the get_avatar hook.
211
+ */
212
+ add_filter( 'get_avatar', 'cupp_avatar' , 1 , 5 );
213
+
214
+ function cupp_avatar( $avatar, $id_or_email, $size, $default, $alt ) {
215
+ $user = false;
216
+ $id = false;
217
+
218
+ if ( is_numeric( $id_or_email ) ) {
219
+
220
+ $id = (int) $id_or_email;
221
+ $user = get_user_by( 'id' , $id );
222
+
223
+ } elseif ( is_object( $id_or_email ) ) {
224
+
225
+ if ( ! empty( $id_or_email->user_id ) ) {
226
+ $id = (int) $id_or_email->user_id;
227
+ $user = get_user_by( 'id' , $id );
228
+ }
229
+
230
+ } else {
231
+ // $id = (int) $id_or_email;
232
+ $user = get_user_by( 'email', $id_or_email );
233
+ }
234
+
235
+ if ( $user && is_object( $user ) ) {
236
+
237
+ $custom_avatar = get_cupp_meta($id, 'thumbnail');
238
+
239
+ if (isset($custom_avatar) && !empty($custom_avatar)) {
240
+ $avatar = "<img alt='{$alt}' src='{$custom_avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
241
+ }
242
+
243
+ }
244
+
245
+ return $avatar;
246
+ }
247
+
248
  ?>
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: 3five, VincentListrani
3
  Donate link:
4
  Tags: custom profile photo, user profile, profile photo, user profile photo
5
  Requires at least: 3.6.1
6
- Tested up to: 4.2.2
7
- Stable tag: 0.3
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -18,15 +18,15 @@ Some users might not have or want to have a gravatar account or other universal
18
 
19
  With the ability to upload a photo to a user profile via the WordPress Media Uploader or by specifying an external URL to an image, your users and/or authors can have a personalized photo specific to your website.*
20
 
21
- By utilizing the WordPress Media Uploader, you also have the ability to use and of the predefined images sizes that the media uploader creates and any custom images size specific to your theme.
22
-
23
  This plugin will add a custom set of fields to the user profile page which will allow for the use of a custom profile photo.
24
 
25
  You can add/change/edit uploaded photos directly from the user profile page. The external option allows you to provide a URL to the external image or remove it.
26
 
27
- **To use this plugin, you will need basic to intermediate WordPress theme development skills. It *does not* overwrite a theme's gravatar/avatar settings.**
 
 
28
 
29
- Simply go to the users section and select a user. The new fields are added to the bottom of the user profile page. Choose which type of photo you want to use. Upload or add the url, depending on your option. Then press the Update Profile button.
30
 
31
  To retrieve the photo on the front-end use the following example on your template page(s):
32
  `<?php
@@ -41,9 +41,9 @@ To retrieve the photo on the front-end use the following example on your templat
41
  // Print the image on the page
42
  echo '<img src="'. $imgURL .'" alt="">';
43
  ?>`
44
- You will need to place the code above in each area of your theme where you wish to add or overwrite your theme's gravatar/avatar image. This can include but is not limited to single.php, page.php, and comments.php.
45
 
46
- *Future Updates to this plugin include full WordPress avatar integration (posts and comments), allowing other roles to access this feature, and ajax processing of images that use the WordPress Media Uploader.
47
 
48
  == Installation ==
49
 
@@ -53,18 +53,18 @@ You will need to place the code above in each area of your theme where you wish
53
 
54
  == Frequently Asked Questions ==
55
 
56
- = How do I retrieve the image after I've updated the user profile? =
57
-
58
- You can call it in your template page(s) like so: `<?php echo get_cupp_meta($user_ID, $size); ?>`
59
- where the $user_ID is the users ID number and the size is a registered image size like 'thumbnail' or an array like `array(50,50)`
60
-
61
  = Who can upload and manage these images? =
62
 
63
- Currently, only a user with admin privileges can manage this option.
 
 
 
64
 
65
- = I installed the plugin but it doesn't show the new image anywhere. What gives? =
66
 
67
- The plugin does not overwrite the default WordPress gravatar/avatar image. You need at least basic to intermediate WordPress theme editing skills. Please reference the code snippet above or on the Description tab.
 
 
68
 
69
  == Screenshots ==
70
 
@@ -80,6 +80,11 @@ The plugin does not overwrite the default WordPress gravatar/avatar image. You n
80
 
81
  == Changelog ==
82
 
 
 
 
 
 
83
  = 0.3 =
84
  * Changed the function which gets the attachment post ID by GUID to the WordPress core function attachment_url_to_postid() for better reliability. (Props to sqhendr).
85
 
3
  Donate link:
4
  Tags: custom profile photo, user profile, profile photo, user profile photo
5
  Requires at least: 3.6.1
6
+ Tested up to: 4.2.3
7
+ Stable tag: 0.4
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
18
 
19
  With the ability to upload a photo to a user profile via the WordPress Media Uploader or by specifying an external URL to an image, your users and/or authors can have a personalized photo specific to your website.*
20
 
 
 
21
  This plugin will add a custom set of fields to the user profile page which will allow for the use of a custom profile photo.
22
 
23
  You can add/change/edit uploaded photos directly from the user profile page. The external option allows you to provide a URL to the external image or remove it.
24
 
25
+ **As of v0.4**, the plugin now overrides the get_avatar() hook found in most WordPress themes.
26
+
27
+ Simply go to the users section and select a user or select "Your Profile" depending on your permission level. The new fields are added to the bottom of the user profile page. Choose which type of photo you want to use. Upload an image or add an external url. Then press the Update Profile button.
28
 
29
+ If you require a customized approach or your theme does not support the get_avatar() hook, use the example below.
30
 
31
  To retrieve the photo on the front-end use the following example on your template page(s):
32
  `<?php
41
  // Print the image on the page
42
  echo '<img src="'. $imgURL .'" alt="">';
43
  ?>`
44
+ You will need to place the code above in each area of your theme where you wish to add and retrieve your theme's custom avatar image. This can include but is not limited to single.php, page.php, and comments.php.
45
 
46
+ *Future Updates to this plugin include allowing other roles to access this feature, a settings page to allow a custom default image and other options.
47
 
48
  == Installation ==
49
 
53
 
54
  == Frequently Asked Questions ==
55
 
 
 
 
 
 
56
  = Who can upload and manage these images? =
57
 
58
+ Currently, only a user with the upload_files capability can use this option.
59
+ Editors and Admins can upload and edit files.
60
+ Authors can only upload files.
61
+ Subscribers and Contributors cannot do either so an Admin will need to do this for them.
62
 
63
+ = I installed the plugin but I want to customize the output and placement of the image. Is this possible? =
64
 
65
+ Yes, you can still customize the output by using the get_cupp_meta() function. Please reference the code snippet below or on the Description tab.
66
+ `<?php echo get_cupp_meta($user_ID, $size); ?>`
67
+ Where the $user_ID is the users ID number and the size is a registered image size like 'thumbnail' or an array like `array(50,50)`.
68
 
69
  == Screenshots ==
70
 
80
 
81
  == Changelog ==
82
 
83
+ = 0.4 =
84
+ * Major Update - please be sure to backup your site and db.
85
+ * The plugin now overrides the WordPress avatar by filtering the get_avatar() hook.
86
+ * The get_cupp_meta() function still exists and can be used to customize the output (this will eventually be deprecated).
87
+
88
  = 0.3 =
89
  * Changed the function which gets the attachment post ID by GUID to the WordPress core function attachment_url_to_postid() for better reliability. (Props to sqhendr).
90