WP User Avatar - Version 1.1.6

Version Description

  • Bug Fix: Image not showing in user profile edit
Download this release

Release Info

Developer bangbay
Plugin Icon 128x128 WP User Avatar
Version 1.1.6
Comparing to
See all releases

Version 1.1.6

Files changed (5) hide show
  1. css/wp-user-avatar.css +2 -0
  2. index.html +1 -0
  3. js/wp-user-avatar.js +11 -0
  4. readme.txt +205 -0
  5. wp-user-avatar.php +320 -0
css/wp-user-avatar.css ADDED
@@ -0,0 +1,2 @@
 
 
1
+ #wp-user-avatar-message { color: #c00; display: none; }
2
+ #wp-user-avatar-preview img { max-width: 96px; }
index.html ADDED
@@ -0,0 +1 @@
 
1
+ <!-- Thanks for using WP User Avatar! -->
js/wp-user-avatar.js ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ function setWPUserAvatar(attachment, imageURL){
2
+ jQuery('#wp-user-avatar', window.parent.document).val(attachment);
3
+ jQuery('#wp-user-avatar-preview', window.parent.document).find('img').attr('src', imageURL).attr('width', '96').removeAttr('height', '');
4
+ jQuery('#wp-user-avatar-message', window.parent.document).show();
5
+ jQuery('#remove-wp-user-avatar', window.parent.document).show();
6
+ if(typeof(wp) != 'undefined'){
7
+ wp.media.wpUserAvatar.frame().close()
8
+ } else {
9
+ window.parent.tb_remove();
10
+ }
11
+ }
readme.txt ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === WP User Avatar ===
2
+
3
+ Contributors: bangbay
4
+ Donate link: http://siboliban.org/donate
5
+ Tags: author image, author photo, author avatar, avatar, profile avatar, profile image, profile photo, user avatar, user image, user photo
6
+ Requires at least: 3.3
7
+ Tested up to: 3.5
8
+ Stable tag: 1.1.6
9
+ License: GPLv2 or later
10
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
+
12
+ Use any image from your WordPress Media Library as a custom user avatar.
13
+
14
+ == Description ==
15
+
16
+ WordPress currently only allows you to use custom avatars that are uploaded through gravatar.com. WP User Avatar enables you to use any photo uploaded into your Media Library as an avatar. This means you use the same uploader and library as your posts. No extra folders or image editing functions are necessary.
17
+
18
+ To use WP User Avatar, choose a theme that has avatar support. In your theme, manually replace <code>get_avatar</code> with <code>get_wp_user_avatar</code> or leave <code>get_avatar</code> as-is. <code>get_wp_user_avatar</code> has functionality not available in <code>get_avatar</code>. [Read about the differences here](http://wordpress.org/extend/plugins/wp-user-avatar/faq/).
19
+
20
+ You can also use the shortcode <code>[avatar]</code> in your posts. The shortcode will work with any theme, whether it has avatar support or not.
21
+
22
+ Read more about the [get_avatar in WordPress's Function Reference](http://codex.wordpress.org/Function_Reference/get_avatar).
23
+
24
+ This plugin uses the new Media Uploader introduced in WordPress 3.5, but is also backwards-compatible to WordPress 3.3.
25
+
26
+ == Installation ==
27
+
28
+ 1. Download, install, and activate the WP User Avatar plugin.
29
+ 2. On your edit profile page, click "Edit WP User Avatar".
30
+ 3. Choose an image, then click "Set WP User Avatar".
31
+ 4. Click "Update Profile".
32
+ 5. Choose a theme that has avatar support. In your theme, manually replace <code>get_avatar</code> with <code>get_wp_user_avatar</code> or leave <code>get_avatar</code> as-is. [Read about the differences here](http://wordpress.org/extend/plugins/wp-user-avatar/faq/).
33
+ 6. You can also use the shortcode <code>[avatar]</code> in your posts. The shortcode will work with any theme, whether it has avatar support or not.
34
+
35
+ **Example Usage**
36
+
37
+ Within The Loop, you may be using:
38
+
39
+ `<?php echo get_avatar(get_the_author_meta('ID'), 96); ?>`
40
+
41
+ Replace this function with:
42
+
43
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96); ?>`
44
+
45
+ You can also use the values "original", "large", "medium", or "thumbnail" for your avatar size:
46
+
47
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 'medium'); ?>`
48
+
49
+ You can also add an alignment of "left", "right", or "center":
50
+
51
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96, 'left'); ?>`
52
+
53
+ On an author page outside of The Loop, you may be using:
54
+
55
+ `<?php
56
+ $user = get_user_by('slug', $author_name);
57
+ echo get_avatar($user->ID, 96);
58
+ ?>`
59
+
60
+ Replace this function with:
61
+
62
+ `<?php
63
+ $user = get_user_by('slug', $author_name);
64
+ echo get_wp_user_avatar($user->ID, 96);
65
+ ?>`
66
+
67
+ If you leave the options blank, WP User Avatar will detect whether you're inside The Loop or on an author page and return the correct avatar in the default 96x96 size:
68
+
69
+ `<?php echo get_wp_user_avatar(); ?>`
70
+
71
+ The function <code>get_wp_user_avatar</code> will also fall back to <code>get_avatar</code> if no WP User Avatar image is set. For this to work, "Show Avatars" must be checked in your Discussion settings.
72
+
73
+ **Other Available Functions**
74
+
75
+ = [avatar] shortcode =
76
+
77
+ You can use the shortcode <code>[avatar]</code> in your posts. It will detect the author of the post or you can specify an author by username. You can specify a size and alignment, but they are optional.
78
+
79
+ `[avatar user="admin" size="medium" align="left"]`
80
+
81
+ = get_wp_user_avatar_src =
82
+
83
+ Works just like <code>get_wp_user_avatar</code> but returns just the image src. This is useful if you would like to link a thumbnail-sized avatar to a larger version of the image:
84
+
85
+ `<a href="<?php echo get_wp_user_avatar_src($user_id, 'large'); ?>">
86
+ <?php echo get_wp_user_avatar($user_id, 'thumbnail'); ?>
87
+ </a>`
88
+
89
+ = has_wp_user_avatar =
90
+
91
+ Returns true if the user has a WP User Avatar image. You can specify the user ID, or leave it blank to detect the author within The Loop or author page:
92
+
93
+ `<?php
94
+ if ( has_wp_user_avatar($user_id) ) {
95
+ echo get_wp_user_avatar($user_id, 96);
96
+ } else {
97
+ echo '<img src="my-alternate-image.jpg" />';
98
+ }
99
+ ?>`
100
+
101
+ == Frequently Asked Questions ==
102
+
103
+ = How do I use WP User Avatar? =
104
+
105
+ First, choose a theme that has avatar support. In your theme, you have a choice of manually replacing <code>get_avatar</code> with <code>get_wp_user_avatar</code>, or leaving <code>get_avatar</code> as-is. Here are the differences:
106
+
107
+ = get_wp_user_avatar =
108
+
109
+ 1. Allows you to use the values "original", "large", "medium", or "thumbnail" for your avatar size.
110
+ 2. Doesn't add a fixed width and height to the image if you use the aforementioned values. This will give you more flexibility to resize the image with CSS.
111
+ 3. Optionally adds CSS classes "alignleft", "alignright", or "aligncenter" to position your avatar.
112
+ 4. Shows nothing if no WP User Avatar image is set.
113
+ 5. Shows the default avatar only if "Show Avatars" is enabled in your Discussion settings.
114
+
115
+ = get_avatar =
116
+
117
+ 1. Requires you to enable "Show Avatars" in your Discussion settings to show any avatars.
118
+ 2. Accepts only numeric values for your avatar size.
119
+ 3. Always adds a fixed width and height to your image. This may cause problems if you use responsive CSS in your theme.
120
+ 4. Shows the default avatar if no WP User Avatar image is set. (Choosing "Blank" still generates a transparent image file.)
121
+ 5. Requires no changes to your theme files if you are currently using <code>get_avatar</code>.
122
+
123
+ = Can I insert WP User Avatar directly into a post? =
124
+
125
+ You can use the shortcode <code>[avatar]</code> in your posts. The shortcode will work with any theme, whether it has avatar support or not. It will detect the author of the post or you can specify an author by username. You can specify a size or alignment, but they are optional.
126
+
127
+ `[avatar user="admin" size="medium" align="left"]`
128
+
129
+ = Will WP User Avatar work with comment author avatars? =
130
+
131
+ Yes, for registered users. Non-registered comment authors will show their gravatar.com avatars.
132
+
133
+ = How can I see which users have an avatar? =
134
+
135
+ For administrators, WP User Avatar adds a column with avatar thumbnails to your Users admin table. If "Show Avatars" is enabled in your Discussion settings, you will see avatars to the left of each username instead of in a new column.
136
+
137
+ = What CSS can I use with WP User Avatar? =
138
+
139
+ WP User Avatar will add the CSS classes "wp-user-avatar" and "wp-user-avatar-{size}" to your image. If you add an alignment, the corresponding alignment class will be added:
140
+
141
+ `<?php echo get_wp_user_avatar($user_id, 96, 'left'); ?>`
142
+
143
+ Outputs:
144
+
145
+ `<img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />`
146
+
147
+ If you use the values "original", "large", "medium", or "thumbnail", no width or height will be added to the image. This will give you more flexibility to resize the image with CSS:
148
+
149
+ `<?php echo get_wp_user_avatar($user_id, 'medium'); ?>`
150
+
151
+ Outputs:
152
+
153
+ `<img src="{imageURL}" class="wp-user-avatar wp-user-avatar-medium" />`
154
+
155
+ **Note:** WordPress adds more CSS classes to the avater not listed here.
156
+
157
+ == Screenshots ==
158
+
159
+ 1. WP User Avatar adds a field to your profile in edit mode.
160
+
161
+ == Changelog ==
162
+
163
+ = 1.1.6 =
164
+ * Bug Fix: Image not showing in user profile edit
165
+
166
+ = 1.1.5a =
167
+ * Update: readme.txt
168
+
169
+ = 1.1.5 =
170
+ * Bug Fix: Remove stray curly bracket
171
+
172
+ = 1.1.4 =
173
+ * Bug Fix: change get_usermeta to get_user_meta
174
+ * Bug Fix: Non-object warning when retrieving user ID
175
+
176
+ = 1.1.3 =
177
+ * Bug Fix: Comment author with no e-mail address
178
+
179
+ = 1.1.2 =
180
+ * Remove: Unused variables
181
+
182
+ = 1.1.1 =
183
+ * Bug Fix: Capabilities error in comment avatar
184
+
185
+ = 1.1 =
186
+ * Add: Add filter for get_avatar
187
+ * Add: CSS alignment classes
188
+ * Add: Replace comment author avatar
189
+ * Add: Shortcode
190
+ * Update: readme.txt
191
+
192
+ = 1.0.2 =
193
+ * Update: FAQ
194
+ * Remove: CSS that hides "Insert into Post"
195
+
196
+ = 1.0.1 =
197
+ * Add: CSS classes to image output
198
+
199
+ = 1.0 =
200
+ * Initial release
201
+
202
+ == Upgrade Notice ==
203
+
204
+ = 1.1 =
205
+ * New Features: [avatar] shortcode, direct replacement of get_avatar() and comment author avatar, more CSS classes
wp-user-avatar.php ADDED
@@ -0,0 +1,320 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.1.6
5
+ */
6
+ /*
7
+ Plugin Name: WP User Avatar
8
+ Plugin URI: http://wordpress.org/extend/plugins/wp-user-avatar/
9
+ Description: Use any image from your WordPress Media Library as a custom user avatar.
10
+ Version: 1.1.6
11
+ Author: Bangbay Siboliban
12
+ Author URI: http://siboliban.org/
13
+ */
14
+
15
+ // Define paths and variables
16
+ define('WP_USER_AVATAR_FOLDER', basename(dirname(__FILE__)));
17
+ define('WP_USER_AVATAR_ABSPATH', trailingslashit(str_replace('\\','/', WP_PLUGIN_DIR.'/'.WP_USER_AVATAR_FOLDER)));
18
+ define('WP_USER_AVATAR_URLPATH', trailingslashit(plugins_url(WP_USER_AVATAR_FOLDER)));
19
+
20
+ // Remove user metadata on plugin delete
21
+ register_uninstall_hook(__FILE__, 'wp_user_avatar_delete_setup');
22
+
23
+ // Remove user metadata
24
+ function wp_user_avatar_delete_setup(){
25
+ $users = get_users();
26
+ foreach($users as $user){
27
+ delete_user_meta($user->ID, 'wp_user_avatar');
28
+ }
29
+ }
30
+
31
+ // WP User Avatar
32
+ if(!class_exists('wp_user_avatar')){
33
+ class wp_user_avatar{
34
+ function wp_user_avatar(){
35
+ add_action('show_user_profile', array('wp_user_avatar','action_show_user_profile'));
36
+ add_action('edit_user_profile', array($this,'action_show_user_profile'));
37
+ add_action('personal_options_update', array($this,'action_process_option_update'));
38
+ add_action('edit_user_profile_update', array($this,'action_process_option_update'));
39
+ if(!function_exists('wp_enqueue_media')){
40
+ add_filter('attachment_fields_to_edit', array($this, 'add_wp_user_avatar_attachment_field_to_edit'), 10, 2);
41
+ }
42
+ if(get_option('show_avatars') != '1'){
43
+ add_filter('manage_users_columns', array($this, 'add_wp_user_avatar_column'), 10, 1);
44
+ add_filter('manage_users_custom_column', array($this, 'show_wp_user_avatar_column'), 10, 3);
45
+ }
46
+ add_action('admin_enqueue_scripts', array($this, 'media_upload_scripts'));
47
+ }
48
+
49
+ // Add to user profile edit
50
+ function action_show_user_profile($user){
51
+ $wp_user_avatar = get_user_meta($user->ID, 'wp_user_avatar', true);
52
+ $hide = !has_wp_user_avatar($user->ID) ? ' style="display:none;"' : '';
53
+ ?>
54
+ <h3><?php _e('WP User Avatar') ?></h3>
55
+ <table class="form-table">
56
+ <tbody>
57
+ <tr>
58
+ <th><label for="wp_user_avatar"><?php _e('WP User Avatar'); ?></label></th>
59
+ <td>
60
+ <input type="hidden" name="wp-user-avatar" id="wp-user-avatar" value="<?php echo $wp_user_avatar; ?>" />
61
+ <p><button type="button" class="button" id="add-wp-user-avatar"><?php _e('Edit WP User Avatar'); ?></button></p>
62
+ <div id="wp-user-avatar-preview">
63
+ <p>
64
+ <?php
65
+ if(has_wp_user_avatar($user->ID)){
66
+ echo get_wp_user_avatar($user->ID, 'medium');
67
+ } else {
68
+ if(get_option('show_avatars') == '1'){
69
+ echo get_avatar($user->ID, 96);
70
+ } else {
71
+ echo '<img src="'.includes_url().'images/blank.gif" alt="" />';
72
+ }
73
+ }
74
+ ?>
75
+ </p>
76
+ </div>
77
+ <p><button type="button" class="button" id="remove-wp-user-avatar"<?php echo $hide; ?>><?php _e('Remove'); ?></button></p>
78
+ <p id="wp-user-avatar-message"><?php _e('Press "Update Profile" to save your changes.'); ?></p>
79
+ </td>
80
+ </tr>
81
+ </tbody>
82
+ </table>
83
+ <script type="text/javascript">
84
+ jQuery(function($){
85
+ <?php if(function_exists('wp_enqueue_media')) : // Use Backbone uploader for WP 3.5+ ?>
86
+ wp.media.wpUserAvatar = {
87
+ get: function() {
88
+ return wp.media.view.settings.post.wpUserAvatarId;
89
+ },
90
+ set: function(id) {
91
+ var settings = wp.media.view.settings;
92
+ settings.post.wpUserAvatarId = id;
93
+ settings.post.wpUserAvatarSrc = $('div.attachment-info').find('img').attr('src');
94
+ if(settings.post.wpUserAvatarId)
95
+ setWPUserAvatar(settings.post.wpUserAvatarId, settings.post.wpUserAvatarSrc);
96
+ },
97
+ frame: function(){
98
+ if(this._frame)
99
+ return this._frame;
100
+ this._frame = wp.media({
101
+ state: 'library',
102
+ states: [ new wp.media.controller.Library({ title: "Edit WP User Avatar: <?php echo $user->display_name; ?>" }) ]
103
+ });
104
+ this._frame.on('open', function(){
105
+ var selection = this.state().get('selection');
106
+ id = jQuery('#wp-user-avatar').val();
107
+ attachment = wp.media.attachment(id);
108
+ attachment.fetch();
109
+ selection.add(attachment ? [ attachment ] : []);
110
+ }, this._frame);
111
+ this._frame.on('toolbar:create:select', function(toolbar){
112
+ this.createSelectToolbar(toolbar, {
113
+ text: 'Set WP User Avatar'
114
+ });
115
+ }, this._frame);
116
+ this._frame.state('library').on('select', this.select);
117
+ return this._frame;
118
+ },
119
+ select: function(id) {
120
+ var settings = wp.media.view.settings,
121
+ selection = this.get('selection').single();
122
+ wp.media.wpUserAvatar.set(selection ? selection.id : -1);
123
+ },
124
+ init: function() {
125
+ $('body').on('click', '#add-wp-user-avatar', function(e){
126
+ e.preventDefault();
127
+ e.stopPropagation();
128
+ wp.media.wpUserAvatar.frame().open();
129
+ });
130
+ }
131
+ };
132
+ $(wp.media.wpUserAvatar.init);
133
+ <?php else : // Fall back to Thickbox uploader ?>
134
+ $('#add-wp-user-avatar').click(function(e){
135
+ e.preventDefault();
136
+ tb_show('Edit WP User Avatar: <?php echo $user->display_name; ?>', 'media-upload.php?type=image&post_type=user&tab=library&TB_iframe=1');
137
+ });
138
+ <?php endif; ?>
139
+ });
140
+ jQuery(function($){
141
+ $('#remove-wp-user-avatar').click(function(e){
142
+ var gravatar = '<img src="<?php echo includes_url().'images/blank.gif'; ?>" alt="" />';
143
+ e.preventDefault();
144
+ $(this).hide();
145
+ $('#wp-user-avatar-preview').find('img').replaceWith(gravatar);
146
+ $('#wp-user-avatar').val('');
147
+ $('#wp-user-avatar-message').show();
148
+ });
149
+ });
150
+ </script>
151
+ <?php
152
+ }
153
+ // Update user meta
154
+ function action_process_option_update($user_id){
155
+ update_user_meta($user_id, 'wp_user_avatar', (isset($_POST['wp-user-avatar']) ? $_POST['wp-user-avatar'] : ''));
156
+ }
157
+
158
+ // Add button to attach image
159
+ function add_wp_user_avatar_attachment_field_to_edit($fields, $post){
160
+ $image = wp_get_attachment_image_src($post->ID, "medium");
161
+ $button = '<button type="button" class="button" id="set-wp-user-avatar-image" onclick="setWPUserAvatar(\''.$post->ID.'\', \''.$image[0].'\')">Set WP User Avatar</button>';
162
+ $fields['wp-user-avatar'] = array(
163
+ 'label' => __('WP User Avatar'),
164
+ 'input' => 'html',
165
+ 'html' => $button
166
+ );
167
+ return $fields;
168
+ }
169
+
170
+ // Add column to Users page
171
+ function add_wp_user_avatar_column($columns){
172
+ return $columns + array('wp-user-avatar' => __('WP User Avatar'));;
173
+ }
174
+
175
+ // Show thumbnail of wp_user_avatar
176
+ function show_wp_user_avatar_column($value, $column_name, $user_id){
177
+ $wp_user_avatar = get_user_meta($user_id, 'wp_user_avatar', true);
178
+ $wp_user_avatar_image = wp_get_attachment_image($wp_user_avatar, array(32,32));
179
+ if($column_name == 'wp-user-avatar'){
180
+ return $wp_user_avatar_image;
181
+ }
182
+ }
183
+
184
+ // Media uploader
185
+ function media_upload_scripts(){
186
+ wp_enqueue_script('media-upload');
187
+ wp_enqueue_script('thickbox');
188
+ if(function_exists('wp_enqueue_media')){
189
+ wp_enqueue_media();
190
+ }
191
+ wp_enqueue_script('wp-user-avatar', WP_USER_AVATAR_URLPATH.'js/wp-user-avatar.js');
192
+ wp_enqueue_style('thickbox');
193
+ wp_enqueue_style('wp-user-avatar', WP_USER_AVATAR_URLPATH.'css/wp-user-avatar.css');
194
+ }
195
+ }
196
+ // Initialize wp_user_avatar
197
+ global $wp_user_avatar_instance;
198
+ $wp_user_avatar_instance = new wp_user_avatar();
199
+ }
200
+
201
+ // Returns true if user has wp_user_avatar
202
+ function has_wp_user_avatar($user_id = ''){
203
+ global $post;
204
+ if(empty($user_id)){
205
+ $author_name = get_query_var('author_name');
206
+ $user = is_author() ? get_user_by('slug', $author_name) : get_the_author_meta('ID');
207
+ $user_id = $user->ID;
208
+ }
209
+ $wp_user_avatar = get_user_meta($user_id, 'wp_user_avatar', true);
210
+ if(!empty($wp_user_avatar)){
211
+ return true;
212
+ }
213
+ }
214
+
215
+ // Find wp_user_avatar, show get_avatar if empty
216
+ function get_wp_user_avatar($id_or_email = '', $size = '96', $align = ''){
217
+ global $post, $comment;
218
+ // Find user ID on comment, author page, or post
219
+ if(is_object($id_or_email)){
220
+ $id_or_email = $comment->user_id != '0' ? $comment->user_id : $comment->comment_author_email;
221
+ $alt = $comment->comment_author;
222
+ } else {
223
+ if(!empty($id_or_email)){
224
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
225
+ } else {
226
+ $author_name = get_query_var('author_name');
227
+ if(is_author()){
228
+ $user = get_user_by('slug', $author_name);
229
+ } else {
230
+ $user_id = get_the_author_meta('ID');
231
+ $user = get_user_by('id', $user_id);
232
+ }
233
+ }
234
+ $id_or_email = !empty($user) ? $user->ID: '';
235
+ $alt = $user->display_name;
236
+ }
237
+ $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta('wp_user_avatar', $id_or_email) : '';
238
+ $alignclass = !empty($align) ? ' align'.$align : '';
239
+ if(!empty($wp_user_avatar_meta)){
240
+ $get_size = is_numeric($size) ? array($size,$size) : $size;
241
+ $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, $get_size);
242
+ $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : '';
243
+ $wp_user_avatar = '<img src="'.$wp_user_avatar_image[0].'"'.$dimensions.' alt="'.$alt.'" class="wp-user-avatar wp-user-avatar-'.$size.$alignclass.' avatar avatar-'.$size.' photo" />';
244
+ } else {
245
+ $default = '';
246
+ $alt = '';
247
+ if($size == 'original' || $size == 'large'){
248
+ $get_size = get_option('large_size_w');
249
+ } elseif($size == 'medium'){
250
+ $get_size = get_option('medium_size_w');
251
+ } elseif($size == 'thumbnail'){
252
+ $get_size = get_option('thumbnail_size_w');
253
+ } else {
254
+ $get_size = $size;
255
+ }
256
+ $avatar = get_avatar($id_or_email, $get_size, $default, $alt);
257
+ $gravatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$get_size.$alignclass." ", $avatar);
258
+ $wp_user_avatar = $gravatar;
259
+ }
260
+ return $wp_user_avatar;
261
+ }
262
+
263
+ // Return just the image src
264
+ function get_wp_user_avatar_src($id_or_email, $size = '', $align = ''){
265
+ $wp_user_avatar_image = get_wp_user_avatar($id_or_email, $size, $align);
266
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wp_user_avatar_image, $matches, PREG_SET_ORDER);
267
+ $wp_user_avatar_image_src = $matches [0] [1];
268
+ return $wp_user_avatar_image_src;
269
+ }
270
+
271
+ // Replace get_avatar()
272
+ function get_wp_user_avatar_alt($avatar, $id_or_email, $size = '', $default = '', $alt = false){
273
+ global $post, $pagenow, $comment;
274
+ // Find user ID on comment, author page, or post
275
+ if(is_object($id_or_email)){
276
+ $id_or_email = $comment->user_id != '0' ? $comment->user_id : $comment->comment_author_email;
277
+ $alt = $comment->comment_author;
278
+ } else {
279
+ if(!empty($id_or_email)){
280
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
281
+ } else {
282
+ $author_name = get_query_var('author_name');
283
+ if(is_author()){
284
+ $user = get_user_by('slug', $author_name);
285
+ } else {
286
+ $user_id = get_the_author_meta('ID');
287
+ $user = get_user_by('id', $user_id);
288
+ }
289
+ }
290
+ $id_or_email = $user->ID;
291
+ $alt = $user->display_name;
292
+ }
293
+ $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta('wp_user_avatar', $id_or_email) : '';
294
+ if(!empty($wp_user_avatar_meta) && $pagenow != 'options-discussion.php'){
295
+ $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, array($size,$size));
296
+ $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : '';
297
+ $wp_user_avatar = '<img src="'.$wp_user_avatar_image[0].'"'.$dimensions.' alt="'.$alt.'" class="wp-user-avatar wp-user-avatar-'.$size.' avatar avatar-'.$size.' photo" />';
298
+ } else {
299
+ $gravatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$size." ", $avatar);
300
+ $wp_user_avatar = $gravatar;
301
+ }
302
+ return $wp_user_avatar;
303
+ }
304
+ add_filter('get_avatar', 'get_wp_user_avatar_alt', 10, 6);
305
+
306
+ // Shortcode
307
+ function wp_user_avatar_shortcode($atts, $content){
308
+ // EXAMPLE USAGE:
309
+ // [avatar size="medium"]
310
+
311
+ // Set shortcode attributes
312
+ extract(shortcode_atts(array('user' => '', 'size' => '96', 'align' => ''), $atts));
313
+ $get_user = get_user_by('slug', $user);
314
+ $id_or_email = !empty($get_user) ? $get_user->ID : '';
315
+ $wp_user_avatar = get_wp_user_avatar($id_or_email, $size, $align);
316
+ return $wp_user_avatar;
317
+ }
318
+ add_shortcode('avatar','wp_user_avatar_shortcode');
319
+
320
+ ?>