WP User Avatar - Version 1.2

Version Description

  • Add: Default Avatar setting
Download this release

Release Info

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

Version 1.2

css/wp-user-avatar.css ADDED
@@ -0,0 +1,4 @@
1
+ #wp-user-avatar-message { color: #c00; display: none; }
2
+ #wp-user-avatar-preview img { max-width: 96px; }
3
+ .defaultavatarpicker #wp-user-avatar-preview { width: 32px; height: 32px; display: inline-block; overflow: hidden; vertical-align: middle; }
4
+ .defaultavatarpicker #wp-user-avatar-preview img { width: 32px; }
images/wp-user-avatar-32x32.png ADDED
Binary file
images/wp-user-avatar.png ADDED
Binary file
index.html ADDED
@@ -0,0 +1 @@
1
+ <!-- Thanks for using WP User Avatar! -->
js/wp-user-avatar.js ADDED
@@ -0,0 +1,13 @@
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).removeAttr('width', '').removeAttr('height', '');
4
+ jQuery('#wp-user-avatar-message', window.parent.document).show();
5
+ jQuery('#remove-wp-user-avatar', window.parent.document).show();
6
+ jQuery('#gravatar-notice', window.parent.document).hide();
7
+ jQuery('#wp_user_avatar_radio', window.parent.document).val(imageURL).trigger('click');
8
+ if(typeof(wp) != 'undefined'){
9
+ wp.media.wpUserAvatar.frame().close()
10
+ } else {
11
+ window.parent.tb_remove();
12
+ }
13
+ }
readme.txt ADDED
@@ -0,0 +1,230 @@
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.1
8
+ Stable tag: 1.2
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. Add your own Default Avatar.
13
+
14
+ == Description ==
15
+
16
+ WordPress currently only allows you to use custom avatars that are uploaded through [gravatar.com](http://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
+ WP User Avatar also lets you:
21
+
22
+ * Upload your own Default Avatar in your Discussion settings.
23
+ * Show the user's gravatar.com avatar or Default Avatar if the user doesn't have a WP User Avatar image.
24
+ * Use the shortcode <code>[avatar]</code> in your posts. The shortcode will work with any theme, whether it has avatar support or not.
25
+
26
+ [Read more about get_avatar in the WordPress Function Reference](http://codex.wordpress.org/Function_Reference/get_avatar).
27
+
28
+ This plugin uses the Media uploader introduced in WordPress 3.5, but is also backwards-compatible to WordPress 3.3.
29
+
30
+ == Installation ==
31
+
32
+ 1. Download, install, and activate the WP User Avatar plugin.
33
+ 2. On your edit profile page, click "Edit WP User Avatar".
34
+ 3. Choose an image, then click "Set WP User Avatar".
35
+ 4. Click "Update Profile".
36
+ 5. Upload your own Default Avatar in your Discussion settings (optional).
37
+ 6. 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/).
38
+ 7. 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.
39
+
40
+ **Example Usage**
41
+
42
+ Within The Loop, you may be using:
43
+
44
+ `<?php echo get_avatar(get_the_author_meta('ID'), 96); ?>`
45
+
46
+ Replace this function with:
47
+
48
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96); ?>`
49
+
50
+ You can also use the values "original", "large", "medium", or "thumbnail" for your avatar size:
51
+
52
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 'medium'); ?>`
53
+
54
+ You can also add an alignment of "left", "right", or "center":
55
+
56
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96, 'left'); ?>`
57
+
58
+ On an author page outside of The Loop, you may be using:
59
+
60
+ `<?php
61
+ $user = get_user_by('slug', $author_name);
62
+ echo get_avatar($user->ID, 96);
63
+ ?>`
64
+
65
+ Replace this function with:
66
+
67
+ `<?php
68
+ $user = get_user_by('slug', $author_name);
69
+ echo get_wp_user_avatar($user->ID, 96);
70
+ ?>`
71
+
72
+ 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:
73
+
74
+ `<?php echo get_wp_user_avatar(); ?>`
75
+
76
+ 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. When this setting is enabled, you will see the user's gravatar.com avatar or Default Avatar.
77
+
78
+ **Other Available Functions**
79
+
80
+ = [avatar] shortcode =
81
+
82
+ 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.
83
+
84
+ `[avatar user="admin" size="medium" align="left"]`
85
+
86
+ = get_wp_user_avatar_src =
87
+
88
+ 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:
89
+
90
+ `<a href="<?php echo get_wp_user_avatar_src($user_id, 'large'); ?>">
91
+ <?php echo get_wp_user_avatar($user_id, 'thumbnail'); ?>
92
+ </a>`
93
+
94
+ = has_wp_user_avatar =
95
+
96
+ 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:
97
+
98
+ `<?php
99
+ if ( has_wp_user_avatar($user_id) ) {
100
+ echo get_wp_user_avatar($user_id, 96);
101
+ } else {
102
+ echo '<img src="my-alternate-image.jpg" />';
103
+ }
104
+ ?>`
105
+
106
+ == Frequently Asked Questions ==
107
+
108
+ = How do I use WP User Avatar? =
109
+
110
+ 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:
111
+
112
+ = get_wp_user_avatar =
113
+
114
+ 1. Allows you to use the values "original", "large", "medium", or "thumbnail" for your avatar size.
115
+ 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.
116
+ 3. Optionally adds CSS classes "alignleft", "alignright", or "aligncenter" to position your avatar.
117
+ 4. Shows nothing if the user has no WP User Avatar image.
118
+ 5. Shows the user's gravatar.com avatar or the Default Avatar only if "Show Avatars" is enabled in your Discussion settings.
119
+
120
+ = get_avatar =
121
+
122
+ 1. Requires you to enable "Show Avatars" in your Discussion settings to show any avatars.
123
+ 2. Accepts only numeric values for your avatar size.
124
+ 3. Always adds a fixed width and height to your image. This may cause problems if you use responsive CSS in your theme.
125
+ 4. Show the user's gravatar.com avatar or the Default Avatar if the user doesn't have a WP User Avatar image. (Choosing "Blank" as your Default Avatar still generates a transparent image file.)
126
+ 5. Requires no changes to your theme files if you are currently using <code>get_avatar</code>.
127
+
128
+ = Can I create a custom Default Avatar? =
129
+ In your Discussion settings, you can upload your own Default Avatar.
130
+
131
+ = Can I insert WP User Avatar directly into a post? =
132
+
133
+ 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.
134
+
135
+ `[avatar user="admin" size="medium" align="left"]`
136
+
137
+ = Will WP User Avatar work with comment author avatars? =
138
+
139
+ Yes, for registered users. Non-registered comment authors will show their gravatar.com avatars.
140
+
141
+ = How can I see which users have an avatar? =
142
+
143
+ 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.
144
+
145
+ = What CSS can I use with WP User Avatar? =
146
+
147
+ 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:
148
+
149
+ `<?php echo get_wp_user_avatar($user_id, 96, 'left'); ?>`
150
+
151
+ Outputs:
152
+
153
+ `<img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />`
154
+
155
+ 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:
156
+
157
+ `<?php echo get_wp_user_avatar($user_id, 'medium'); ?>`
158
+
159
+ Outputs:
160
+
161
+ `<img src="{imageURL}" class="wp-user-avatar wp-user-avatar-medium" />`
162
+
163
+ **Note:** WordPress adds more CSS classes to the avatar not listed here.
164
+
165
+ = What other functions are available for WP User Avatar? =
166
+ * <code>get_wp_user_avatar_src</code> - retrieve just the image URL
167
+ * <code>has_wp_user_avatar</code> - check if the user has a WP User Avatar image
168
+
169
+ [See example usage here](http://wordpress.org/extend/plugins/wp-user-avatar/installation/)
170
+
171
+ == Screenshots ==
172
+
173
+ 1. WP User Avatar lets you upload your own Default Avatar.
174
+ 2. WP User Avatar adds a field to your profile in edit mode.
175
+ 3. After you've set a WP User Avatar image, you will see the option to remove it.
176
+
177
+ == Changelog ==
178
+
179
+ = 1.2 =
180
+ * Add: Default Avatar setting
181
+
182
+ = 1.1.7 =
183
+ * Bug Fix: Change update_usermeta to update_user_meta
184
+
185
+ = 1.1.6 =
186
+ * Bug Fix: Image not showing in user profile edit
187
+
188
+ = 1.1.5a =
189
+ * Update: readme.txt
190
+
191
+ = 1.1.5 =
192
+ * Bug Fix: Remove stray curly bracket
193
+
194
+ = 1.1.4 =
195
+ * Bug Fix: Change get_usermeta to get_user_meta
196
+ * Bug Fix: Non-object warning when retrieving user ID
197
+
198
+ = 1.1.3 =
199
+ * Bug Fix: Comment author with no e-mail address
200
+
201
+ = 1.1.2 =
202
+ * Remove: Unused variables
203
+
204
+ = 1.1.1 =
205
+ * Bug Fix: Capabilities error in comment avatar
206
+
207
+ = 1.1 =
208
+ * Add: Add filter for get_avatar
209
+ * Add: CSS alignment classes
210
+ * Add: Replace comment author avatar
211
+ * Add: Shortcode
212
+ * Update: readme.txt
213
+
214
+ = 1.0.2 =
215
+ * Update: FAQ
216
+ * Remove: CSS that hides "Insert into Post"
217
+
218
+ = 1.0.1 =
219
+ * Add: CSS classes to image output
220
+
221
+ = 1.0 =
222
+ * Initial release
223
+
224
+ == Upgrade Notice ==
225
+
226
+ = 1.2=
227
+ * New Feature: Default Avatar customization
228
+
229
+ = 1.1 =
230
+ * New Features: [avatar] shortcode, direct replacement of get_avatar() and comment author avatar, more CSS classes
wp-user-avatar.php ADDED
@@ -0,0 +1,470 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.2
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.2
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
+ // Initialize default settings
21
+ register_activation_hook(__FILE__, 'wp_user_avatar_options');
22
+
23
+ // Remove user metadata on plugin delete
24
+ register_uninstall_hook(__FILE__, 'wp_user_avatar_delete_setup');
25
+
26
+ // Settings saved to wp_options
27
+ function wp_user_avatar_options(){
28
+ add_option('avatar_default_wp_user_avatar','');
29
+ }
30
+ add_action('init', 'wp_user_avatar_options');
31
+
32
+ // Remove user metadata
33
+ function wp_user_avatar_delete_setup(){
34
+ $users = get_users();
35
+ foreach($users as $user){
36
+ delete_user_meta($user->ID, 'wp_user_avatar');
37
+ }
38
+ delete_option('avatar_default_wp_user_avatar');
39
+ update_option('avatar_default', 'mystery');
40
+ }
41
+
42
+ // WP User Avatar
43
+ if(!class_exists('wp_user_avatar')){
44
+ class wp_user_avatar{
45
+ function wp_user_avatar(){
46
+ add_action('show_user_profile', array('wp_user_avatar','action_show_user_profile'));
47
+ add_action('edit_user_profile', array($this,'action_show_user_profile'));
48
+ add_action('personal_options_update', array($this,'action_process_option_update'));
49
+ add_action('edit_user_profile_update', array($this,'action_process_option_update'));
50
+ if(!function_exists('wp_enqueue_media')){
51
+ add_filter('attachment_fields_to_edit', array($this,'add_wp_user_avatar_attachment_field_to_edit'), 10, 2);
52
+ }
53
+ if(get_option('show_avatars') != '1'){
54
+ add_filter('manage_users_columns', array($this,'add_wp_user_avatar_column'), 10, 1);
55
+ add_filter('manage_users_custom_column', array($this,'show_wp_user_avatar_column'), 10, 3);
56
+ }
57
+ add_action('admin_enqueue_scripts', array($this,'media_upload_scripts'));
58
+ }
59
+
60
+ // Add to user profile edit
61
+ function action_show_user_profile($user){
62
+ $wp_user_avatar = get_user_meta($user->ID, 'wp_user_avatar', true);
63
+ $hide_notice = has_wp_user_avatar($user->ID) ? ' style="display:none;"' : '';
64
+ $hide_remove = !has_wp_user_avatar($user->ID) ? ' style="display:none;"' : '';
65
+ ?>
66
+ <h3><?php _e('WP User Avatar') ?></h3>
67
+ <table class="form-table">
68
+ <tbody>
69
+ <tr>
70
+ <th><label for="wp_user_avatar"><?php _e('WP User Avatar'); ?></label></th>
71
+ <td>
72
+ <input type="hidden" name="wp-user-avatar" id="wp-user-avatar" value="<?php echo $wp_user_avatar; ?>" />
73
+ <p><button type="button" class="button" id="add-wp-user-avatar"><?php _e('Edit WP User Avatar'); ?></button></p>
74
+ <div id="wp-user-avatar-preview">
75
+ <p>
76
+ <?php
77
+ if(has_wp_user_avatar($user->ID)){
78
+ echo get_wp_user_avatar($user->ID, 'medium');
79
+ } else {
80
+ if(get_option('show_avatars') == '1'){
81
+ echo get_avatar($user->ID, 96);
82
+ } else {
83
+ echo '<img src="'.includes_url().'images/blank.gif" alt="" />';
84
+ }
85
+ }
86
+ ?>
87
+ </p>
88
+ </div>
89
+ <?php if(get_option('show_avatars') == '1') : ?>
90
+ <p id="gravatar-notice"<?php echo $hide_notice; ?>>This is your <a href="http://gravatar.com/" target="_blank">gravatar.com</a> avatar.</p>
91
+ <?php endif; ?>
92
+ <p><button type="button" class="button" id="remove-wp-user-avatar"<?php echo $hide_remove; ?>><?php _e('Remove'); ?></button></p>
93
+ <p id="wp-user-avatar-message"><?php _e('Press "Update Profile" to save your changes.'); ?></p>
94
+ </td>
95
+ </tr>
96
+ </tbody>
97
+ </table>
98
+ <script type="text/javascript">
99
+ jQuery(function($){
100
+ <?php if(function_exists('wp_enqueue_media')) : // Use Backbone uploader for WP 3.5+ ?>
101
+ wp.media.wpUserAvatar = {
102
+ get: function() {
103
+ return wp.media.view.settings.post.wpUserAvatarId;
104
+ },
105
+ set: function(id) {
106
+ var settings = wp.media.view.settings;
107
+ settings.post.wpUserAvatarId = id;
108
+ settings.post.wpUserAvatarSrc = $('div.attachment-info').find('img').attr('src');
109
+ if(settings.post.wpUserAvatarId)
110
+ setWPUserAvatar(settings.post.wpUserAvatarId, settings.post.wpUserAvatarSrc);
111
+ },
112
+ frame: function(){
113
+ if(this._frame)
114
+ return this._frame;
115
+ this._frame = wp.media({
116
+ state: 'library',
117
+ states: [ new wp.media.controller.Library({ title: "Edit WP User Avatar: <?php echo $user->display_name; ?>" }) ]
118
+ });
119
+ this._frame.on('open', function(){
120
+ var selection = this.state().get('selection');
121
+ id = jQuery('#wp-user-avatar').val();
122
+ attachment = wp.media.attachment(id);
123
+ attachment.fetch();
124
+ selection.add(attachment ? [ attachment ] : []);
125
+ }, this._frame);
126
+ this._frame.on('toolbar:create:select', function(toolbar){
127
+ this.createSelectToolbar(toolbar, {
128
+ text: 'Set WP User Avatar'
129
+ });
130
+ }, this._frame);
131
+ this._frame.state('library').on('select', this.select);
132
+ return this._frame;
133
+ },
134
+ select: function(id) {
135
+ var settings = wp.media.view.settings,
136
+ selection = this.get('selection').single();
137
+ wp.media.wpUserAvatar.set(selection ? selection.id : -1);
138
+ },
139
+ init: function() {
140
+ $('body').on('click', '#add-wp-user-avatar', function(e){
141
+ e.preventDefault();
142
+ e.stopPropagation();
143
+ wp.media.wpUserAvatar.frame().open();
144
+ });
145
+ }
146
+ };
147
+ $(wp.media.wpUserAvatar.init);
148
+ <?php else : // Fall back to Thickbox uploader ?>
149
+ $('#add-wp-user-avatar').click(function(e){
150
+ e.preventDefault();
151
+ tb_show('Edit WP User Avatar: <?php echo $user->display_name; ?>', 'media-upload.php?type=image&post_type=user&post_id=0&tab=library&TB_iframe=1');
152
+ });
153
+ <?php endif; ?>
154
+ });
155
+ jQuery(function($){
156
+ $('#remove-wp-user-avatar').click(function(e){
157
+ <?php if(get_option('show_avatars') == '1') : ?>
158
+ var avatar_thumb = '<?php echo addslashes(get_avatar_original($user->ID, 96)); ?>';
159
+ $('#gravatar-notice').show();
160
+ <?php else : ?>
161
+ var avatar_thumb = '<img src="<?php echo includes_url(); ?>images/blank.gif" alt="" />';
162
+ <?php endif; ?>
163
+ e.preventDefault();
164
+ $(this).hide();
165
+ $('#wp-user-avatar-preview').find('img').replaceWith(avatar_thumb);
166
+ $('#wp-user-avatar').val('');
167
+ $('#wp-user-avatar-message').show();
168
+ });
169
+ });
170
+ </script>
171
+ <?php
172
+ }
173
+ // Update user meta
174
+ function action_process_option_update($user_id){
175
+ update_user_meta($user_id, 'wp_user_avatar', (isset($_POST['wp-user-avatar']) ? $_POST['wp-user-avatar'] : ''));
176
+ }
177
+
178
+ // Add button to attach image
179
+ function add_wp_user_avatar_attachment_field_to_edit($fields, $post){
180
+ $image = wp_get_attachment_image_src($post->ID, "medium");
181
+ $button = '<button type="button" class="button" id="set-wp-user-avatar-image" onclick="setWPUserAvatar(\''.$post->ID.'\', \''.$image[0].'\')">Set WP User Avatar</button>';
182
+ $fields['wp-user-avatar'] = array(
183
+ 'label' => __('WP User Avatar'),
184
+ 'input' => 'html',
185
+ 'html' => $button
186
+ );
187
+ return $fields;
188
+ }
189
+
190
+ // Add column to Users page
191
+ function add_wp_user_avatar_column($columns){
192
+ return $columns + array('wp-user-avatar' => __('WP User Avatar'));;
193
+ }
194
+
195
+ // Show thumbnail of wp_user_avatar
196
+ function show_wp_user_avatar_column($value, $column_name, $user_id){
197
+ $wp_user_avatar = get_user_meta($user_id, 'wp_user_avatar', true);
198
+ $wp_user_avatar_image = wp_get_attachment_image($wp_user_avatar, array(32,32));
199
+ if($column_name == 'wp-user-avatar'){
200
+ return $wp_user_avatar_image;
201
+ }
202
+ }
203
+
204
+ // Media uploader
205
+ function media_upload_scripts(){
206
+ global $pagenow;
207
+ wp_enqueue_script('media-upload');
208
+ wp_enqueue_script('thickbox');
209
+ if(function_exists('wp_enqueue_media')){
210
+ wp_enqueue_media();
211
+ }
212
+ wp_enqueue_script('wp-user-avatar', WP_USER_AVATAR_URLPATH.'js/wp-user-avatar.js');
213
+ wp_enqueue_style('thickbox');
214
+ wp_enqueue_style('wp-user-avatar', WP_USER_AVATAR_URLPATH.'css/wp-user-avatar.css');
215
+ }
216
+ }
217
+ // Initialize wp_user_avatar
218
+ global $wp_user_avatar_instance;
219
+ $wp_user_avatar_instance = new wp_user_avatar();
220
+ }
221
+
222
+ // Returns true if user has wp_user_avatar
223
+ function has_wp_user_avatar($user_id = ''){
224
+ global $post;
225
+ if(empty($user_id)){
226
+ $author_name = get_query_var('author_name');
227
+ $user = is_author() ? get_user_by('slug', $author_name) : get_the_author_meta('ID');
228
+ $user_id = $user->ID;
229
+ }
230
+ $wp_user_avatar = get_user_meta($user_id, 'wp_user_avatar', true);
231
+ if(!empty($wp_user_avatar)){
232
+ return true;
233
+ }
234
+ }
235
+
236
+ // Find wp_user_avatar, show get_avatar if empty
237
+ function get_wp_user_avatar($id_or_email = '', $size = '96', $align = ''){
238
+ global $post, $comment;
239
+ // Find user ID on comment, author page, or post
240
+ if(is_object($id_or_email)){
241
+ $id_or_email = $comment->user_id != '0' ? $comment->user_id : $comment->comment_author_email;
242
+ $alt = $comment->comment_author;
243
+ } else {
244
+ if(!empty($id_or_email)){
245
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
246
+ } else {
247
+ $author_name = get_query_var('author_name');
248
+ if(is_author()){
249
+ $user = get_user_by('slug', $author_name);
250
+ } else {
251
+ $user_id = get_the_author_meta('ID');
252
+ $user = get_user_by('id', $user_id);
253
+ }
254
+ }
255
+ $id_or_email = !empty($user) ? $user->ID: '';
256
+ $alt = $user->display_name;
257
+ }
258
+ $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta('wp_user_avatar', $id_or_email) : '';
259
+ $alignclass = !empty($align) ? ' align'.$align : '';
260
+ if(!empty($wp_user_avatar_meta)){
261
+ $get_size = is_numeric($size) ? array($size,$size) : $size;
262
+ $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, $get_size);
263
+ $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : '';
264
+ $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" />';
265
+ } else {
266
+ $default = '';
267
+ $alt = '';
268
+ if($size == 'original' || $size == 'large'){
269
+ $get_size = get_option('large_size_w');
270
+ } elseif($size == 'medium'){
271
+ $get_size = get_option('medium_size_w');
272
+ } elseif($size == 'thumbnail'){
273
+ $get_size = get_option('thumbnail_size_w');
274
+ } else {
275
+ $get_size = $size;
276
+ }
277
+ $avatar = get_avatar($id_or_email, $get_size, $default, $alt);
278
+ $gravatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$get_size.$alignclass." ", $avatar);
279
+ $wp_user_avatar = $gravatar;
280
+ }
281
+ return $wp_user_avatar;
282
+ }
283
+
284
+ // Return just the image src
285
+ function get_wp_user_avatar_src($id_or_email, $size = '', $align = ''){
286
+ $wp_user_avatar_image = get_wp_user_avatar($id_or_email, $size, $align);
287
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wp_user_avatar_image, $matches, PREG_SET_ORDER);
288
+ $wp_user_avatar_image_src = $matches [0] [1];
289
+ return $wp_user_avatar_image_src;
290
+ }
291
+
292
+ // Return just the image src
293
+ function get_avatar_src($avatar, $id_or_email, $size = '', $default = '', $alt = false){
294
+ $avatar_image = get_avatar($id_or_email);
295
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $avatar_image, $matches, PREG_SET_ORDER);
296
+ $avatar_image_src = $matches [0] [1];
297
+ return $avatar_image_src;
298
+ }
299
+
300
+ // Replace get_avatar()
301
+ function get_wp_user_avatar_alt($avatar, $id_or_email, $size = '', $default = '', $alt = false){
302
+ global $post, $pagenow, $comment;
303
+ $default = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar.png';
304
+ // Find user ID on comment, author page, or post
305
+ if(is_object($id_or_email)){
306
+ $id_or_email = $comment->user_id != '0' ? $comment->user_id : $comment->comment_author_email;
307
+ $alt = $comment->comment_author;
308
+ } else {
309
+ if(!empty($id_or_email)){
310
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
311
+ } else {
312
+ $author_name = get_query_var('author_name');
313
+ if(is_author()){
314
+ $user = get_user_by('slug', $author_name);
315
+ } else {
316
+ $user_id = get_the_author_meta('ID');
317
+ $user = get_user_by('id', $user_id);
318
+ }
319
+ }
320
+ $id_or_email = $user->ID;
321
+ $alt = $user->display_name;
322
+ }
323
+ $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta('wp_user_avatar', $id_or_email) : '';
324
+ if(!empty($wp_user_avatar_meta) && $pagenow != 'options-discussion.php'){
325
+ $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, array($size,$size));
326
+ $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : '';
327
+ $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" />';
328
+ } else {
329
+ $gravatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$size." ", $avatar);
330
+ $wp_user_avatar = $gravatar;
331
+ }
332
+ return $wp_user_avatar;
333
+ }
334
+ add_filter('get_avatar', 'get_wp_user_avatar_alt', 10, 6);
335
+
336
+ // Shortcode
337
+ function wp_user_avatar_shortcode($atts, $content){
338
+ // EXAMPLE USAGE:
339
+ // [avatar size="medium"]
340
+
341
+ // Set shortcode attributes
342
+ extract(shortcode_atts(array('user' => '', 'size' => '96', 'align' => ''), $atts));
343
+ $get_user = get_user_by('slug', $user);
344
+ $id_or_email = !empty($get_user) ? $get_user->ID : '';
345
+ $wp_user_avatar = get_wp_user_avatar($id_or_email, $size, $align);
346
+ return $wp_user_avatar;
347
+ }
348
+ add_shortcode('avatar','wp_user_avatar_shortcode');
349
+
350
+ // Add default avatar
351
+ function add_default_wp_user_avatar($avatar_list){
352
+ $avatar_default = get_option('avatar_default');
353
+ $avatar_default_wp_user_avatar = get_option('avatar_default_wp_user_avatar');
354
+ if(!empty($avatar_default_wp_user_avatar)){
355
+ $avatar_full_src = wp_get_attachment_image_src($avatar_default_wp_user_avatar, 'medium');
356
+ $avatar_thumb_src = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array(32,32));
357
+ $avatar_full = $avatar_full_src[0];
358
+ $avatar_thumb = $avatar_thumb_src[0];
359
+ $hide_remove = '';
360
+ } else {
361
+ $avatar_full = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar.png';
362
+ $avatar_thumb = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-32x32.png';
363
+ $hide_remove = ' style="display:none;"';
364
+ }
365
+ $selected_avatar = ($avatar_default == $avatar_full) ? ' checked="checked" ' : '';
366
+ $avatar_thumb_img = '<div id="wp-user-avatar-preview"><img src="'.$avatar_thumb.'" width="32" /></div>';
367
+ $wp_user_avatar_list = '';
368
+ $wp_user_avatar_list .= "\n\t<label><input type='radio' name='avatar_default' id='wp_user_avatar_radio' value='$avatar_full'$selected_avatar /> ";
369
+ $wp_user_avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1&amp;forcedefault=1'", $avatar_thumb_img);
370
+ $wp_user_avatar_list .= ' WP User Avatar</label>';
371
+ $wp_user_avatar_list .= '<p style="padding-left:15px;"><button type="button" class="button" id="add-wp-user-avatar">Edit WP User Avatar</button>';
372
+ $wp_user_avatar_list .= '&nbsp;&nbsp;&nbsp;&nbsp;<a href="#" id="remove-wp-user-avatar"'.$hide_remove.'>Remove</a></p>';
373
+ $wp_user_avatar_list .= '<input type="hidden" id="wp-user-avatar" name="avatar_default_wp_user_avatar" value="'.$avatar_default_wp_user_avatar.'">';
374
+ $wp_user_avatar_list .= '<p id="wp-user-avatar-message">Press "Save Changes" to save your changes.</p>';
375
+ $wp_user_avatar_list .= edit_default_wp_user_avatar();
376
+ return $wp_user_avatar_list.$avatar_list;
377
+ }
378
+ add_filter('default_avatar_select', 'add_default_wp_user_avatar');
379
+
380
+ // Uploader for default avatar
381
+ function edit_default_wp_user_avatar(){ ?>
382
+ <script type="text/javascript">
383
+ jQuery(function($){
384
+ <?php if(function_exists('wp_enqueue_media')) : // Use Backbone uploader for WP 3.5+ ?>
385
+ wp.media.wpUserAvatar = {
386
+ get: function() {
387
+ return wp.media.view.settings.post.wpUserAvatarId;
388
+ },
389
+ set: function(id) {
390
+ var settings = wp.media.view.settings;
391
+ settings.post.wpUserAvatarId = id;
392
+ settings.post.wpUserAvatarSrc = $('div.attachment-info').find('img').attr('src');
393
+ if(settings.post.wpUserAvatarId)
394
+ setWPUserAvatar(settings.post.wpUserAvatarId, settings.post.wpUserAvatarSrc);
395
+ $('#wp_user_avatar_radio').val(settings.post.wpUserAvatarSrc).trigger('click');
396
+ },
397
+ frame: function(){
398
+ if(this._frame)
399
+ return this._frame;
400
+ this._frame = wp.media({
401
+ state: 'library',
402
+ states: [ new wp.media.controller.Library({ title: "Edit WP User Avatar Default Avatar" }) ]
403
+ });
404
+ this._frame.on('open', function(){
405
+ var selection = this.state().get('selection');
406
+ id = jQuery('#wp-user-avatar').val();
407
+ attachment = wp.media.attachment(id);
408
+ attachment.fetch();
409
+ selection.add(attachment ? [ attachment ] : []);
410
+ }, this._frame);
411
+ this._frame.on('toolbar:create:select', function(toolbar){
412
+ this.createSelectToolbar(toolbar, {
413
+ text: 'Set WP User Avatar'
414
+ });
415
+ }, this._frame);
416
+ this._frame.state('library').on('select', this.select);
417
+ return this._frame;
418
+ },
419
+ select: function(id) {
420
+ var settings = wp.media.view.settings,
421
+ selection = this.get('selection').single();
422
+ wp.media.wpUserAvatar.set(selection ? selection.id : -1);
423
+ },
424
+ init: function() {
425
+ $('body').on('click', '#add-wp-user-avatar', function(e){
426
+ e.preventDefault();
427
+ e.stopPropagation();
428
+ wp.media.wpUserAvatar.frame().open();
429
+ });
430
+ }
431
+ };
432
+ $(wp.media.wpUserAvatar.init);
433
+ <?php else : // Fall back to Thickbox uploader ?>
434
+ $('#add-wp-user-avatar').click(function(e){
435
+ e.preventDefault();
436
+ tb_show('Edit WP User Avatar Default Avatar', 'media-upload.php?type=image&post_type=user&post_id=0&tab=library&TB_iframe=1');
437
+ });
438
+ <?php endif; ?>
439
+ });
440
+ jQuery(function($){
441
+ $('#remove-wp-user-avatar').click(function(e){
442
+ var avatar_full = '<?php echo WP_USER_AVATAR_URLPATH; ?>images/wp-user-avatar.png';
443
+ var avatar_thumb = '<img src="<?php echo WP_USER_AVATAR_URLPATH; ?>images/wp-user-avatar-32x32.png" alt="" />';
444
+ e.preventDefault();
445
+ $(this).hide();
446
+ $('#wp-user-avatar-preview').find('img').replaceWith(avatar_thumb);
447
+ $('#wp-user-avatar').val('');
448
+ $('#wp-user-avatar-message').show();
449
+ $('#wp_user_avatar_radio').val(avatar_full).trigger('click');
450
+ });
451
+ });
452
+ </script>
453
+ <?php
454
+ }
455
+
456
+ // Add default avatar field to whitelist
457
+ function wp_user_avatar_whitelist_options($whitelist_options){
458
+ $whitelist_options['discussion'] = array( 'default_pingback_flag', 'default_ping_status', 'default_comment_status', 'comments_notify', 'moderation_notify', 'comment_moderation', 'require_name_email', 'comment_whitelist', 'comment_max_links', 'moderation_keys', 'blacklist_keys', 'show_avatars', 'avatar_rating', 'avatar_default', 'close_comments_for_old_posts', 'close_comments_days_old', 'thread_comments', 'thread_comments_depth', 'page_comments', 'comments_per_page', 'default_comments_page', 'comment_order', 'comment_registration', 'avatar_default_wp_user_avatar' );
459
+ return $whitelist_options;
460
+ }
461
+ add_filter('whitelist_options', 'wp_user_avatar_whitelist_options');
462
+
463
+ // Get orginal avatar
464
+ function get_avatar_original($avatar, $id_or_email, $size = '', $default = '', $alt = false){
465
+ remove_filter('get_avatar', 'get_wp_user_avatar_alt');
466
+ $gravatar = get_avatar($avatar);
467
+ return $gravatar;
468
+ }
469
+
470
+ ?>