WP User Avatar - Version 1.2.5

Version Description

  • Bug Fix: Comment author showing wrong avatar
  • Bug Fix: Avatar adds fixed dimensions when non-numeric size is used
  • Update: Use local image for default avatar instead of calling image from Gravatar
Download this release

Release Info

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

Version 1.2.5

css/wp-user-avatar.css ADDED
@@ -0,0 +1,7 @@
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; }
5
+ #edit-wp-user-avatar { padding-left: 15px; }
6
+ #edit-wp-user-avatar #remove-wp-user-avatar { margin-left: 10px; }
7
+ .hide-me, .wp-core-ui .hide-me { display: none; }
images/wp-user-avatar-150x150.png ADDED
Binary file
images/wp-user-avatar-300x300.png ADDED
Binary file
images/wp-user-avatar-32x32.png ADDED
Binary file
images/wp-user-avatar-96x96.png ADDED
Binary file
images/wp-user-avatar.png ADDED
Binary file
includes/tinymce.php ADDED
@@ -0,0 +1,42 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.2.5
5
+ */
6
+
7
+ function myplugin_addbuttons() {
8
+ // Don't bother doing this stuff if the current user lacks permissions
9
+ if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
10
+ return;
11
+
12
+ // Add only in Rich Editor mode
13
+ if(get_user_option('rich_editing') == 'true'){
14
+ add_filter('mce_external_plugins', 'add_myplugin_tinymce_plugin');
15
+ add_filter('mce_buttons', 'register_myplugin_button');
16
+ }
17
+ }
18
+
19
+ function register_myplugin_button($buttons) {
20
+ array_push($buttons, 'separator', 'wpUserAvatar');
21
+ return $buttons;
22
+ }
23
+
24
+ // Load the TinyMCE plugin : editor_plugin.js (wp2.5)
25
+ function add_myplugin_tinymce_plugin($plugin_array) {
26
+ $plugin_array['wpUserAvatar'] = WP_USER_AVATAR_URLPATH.'includes/tinymce/editor_plugin.js';
27
+ return $plugin_array;
28
+ }
29
+
30
+ // init process for button control
31
+ add_action('init', 'myplugin_addbuttons');
32
+
33
+ // Call TinyMCE window content via admin-ajax
34
+ function wp_user_avatar_ajax_tinymce(){
35
+ if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
36
+ die('You are not allowed to call this page directly.');
37
+ include_once(WP_USER_AVATAR_ABSPATH.'includes/tinymce/window.php');
38
+ die();
39
+ }
40
+ add_action('wp_ajax_wp_user_avatar_tinymce', 'wp_user_avatar_ajax_tinymce');
41
+
42
+ ?>
includes/tinymce/editor_plugin.js ADDED
@@ -0,0 +1 @@
1
+ (function(){tinymce.PluginManager.requireLangPack('wpUserAvatar');tinymce.create('tinymce.plugins.wpUserAvatar',{init:function(ed,url){ed.addCommand('mceWpUserAvatar',function(){ed.windowManager.open({file:ajaxurl+'?action=wp_user_avatar_tinymce',width:400,height:260,inline:1},{plugin_url:url})});ed.addButton('wpUserAvatar',{title:'Insert WP User Avatar',cmd:'mceWpUserAvatar',image:url+'/../../images/wp-user-avatar-32x32.png'});ed.onNodeChange.add(function(ed,cm,n){cm.setActive('wpUserAvatar',n.nodeName=='IMG')})},createControl:function(n,cm){return null},getInfo:function(){return{longname:'WP User Avatar',author:'Bangbay Siboliban',authorurl:'http://siboliban.org/',infourl:'http://wordpress.org/extend/plugins/wp-user-avatar/',version:"1.2.5"}}});tinymce.PluginManager.add('wpUserAvatar',tinymce.plugins.wpUserAvatar)})();
includes/tinymce/window.php ADDED
@@ -0,0 +1,111 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.2.5
5
+ */
6
+
7
+ if ( !defined('ABSPATH') )
8
+ die('You are not allowed to call this page directly.');
9
+ @header('Content-Type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset'));
10
+ ?>
11
+ <html xmlns="http://www.w3.org/1999/xhtml">
12
+ <head>
13
+ <title>WP User Avatar</title>
14
+ <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
15
+ <base target="_self" />
16
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/tinymce/tiny_mce_popup.js"></script>
17
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/tinymce/utils/form_utils.js"></script>
18
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/jquery/jquery.js"></script>
19
+ <script type="text/javascript">
20
+ function insert_wp_user_avatar(){
21
+ // Custom shortcode values
22
+ var shortcode;
23
+ var user = document.getElementById('wp_user_avatar_user').value;
24
+ var size = document.getElementById('wp_user_avatar_size').value;
25
+ var size_number = document.getElementById('wp_user_avatar_size_number').value;
26
+ var align = document.getElementById('wp_user_avatar_align').value;
27
+ var link = document.getElementById('wp_user_avatar_link').value;
28
+ var link_external = document.getElementById('wp_user_avatar_link_external').value;
29
+
30
+ // Add tag to shortcode only if not blank
31
+ var user_tag = (user != '') ? ' user="' + user + '"' : '';
32
+ var size_tag = (size != '' && size_number == '') ? ' size="' + size + '"' : '';
33
+ size_tag = (size_number != '') ? ' size="' + size_number + '"' : size_tag;
34
+ var align_tag = (align != '') ? ' align="' + align + '"' : '';
35
+ var link_tag = (link != '' && link_external == '') ? ' link="' + link + '"' : '';
36
+ link_tag = (link_external != '') ? ' link="' + link_external + '"' : link_tag;
37
+
38
+ shortcode = "<p>[avatar" + user_tag + size_tag + align_tag + link_tag + "]</p>";
39
+
40
+ if(window.tinyMCE) {
41
+ window.tinyMCE.execInstanceCommand(window.tinyMCE.activeEditor.id, 'mceInsertContent', false, shortcode);
42
+ tinyMCEPopup.editor.execCommand('mceRepaint');
43
+ tinyMCEPopup.close();
44
+ }
45
+ return;
46
+ }
47
+ </script>
48
+ <style type="text/css">
49
+ form { background: #fff; border: 1px solid #eee; }
50
+ p, h4 { margin: 0; padding: 12px 0 0; }
51
+ h4.center { text-align: center; }
52
+ label { width: 90px; display: inline-block; text-align: right; }
53
+ .mceActionPanel { padding: 7px 0 12px; text-align: center; }
54
+ .mceActionPanel #insert { float: none; width: 180px; margin: 0 auto; }
55
+ </style>
56
+ </head>
57
+ <body id="link" class="wp-core-ui" onload="document.body.style.display='';" style="display:none;">
58
+ <form name="wpUserAvatar" action="#">
59
+ <p><label for="<?php esc_attr_e('wp_user_avatar_user'); ?>"><strong><?php _e("User:"); ?></strong></label>
60
+ <select id="<?php esc_attr_e('wp_user_avatar_user'); ?>" name="<?php esc_attr_e('wp_user_avatar_user'); ?>">
61
+ <option value=""></option>
62
+ <?php $users = get_users(); foreach($users as $user) : ?>
63
+ <option value="<?php echo $user->user_login; ?>"><?php echo $user->display_name; ?></option>
64
+ <?php endforeach; ?>
65
+ </select></p>
66
+
67
+ <h4 class="center">Choose a preset size or enter a number value.</h4>
68
+
69
+ <p>
70
+ <label for="<?php esc_attr_e('wp_user_avatar_size'); ?>"><strong><?php _e("Size:"); ?></strong></label>
71
+ <select id="<?php esc_attr_e('wp_user_avatar_size'); ?>" name="<?php esc_attr_e('wp_user_avatar_size'); ?>">
72
+ <option value=""></option>
73
+ <option value="original"><?php _e("Original"); ?></option>
74
+ <option value="large"><?php _e("Large"); ?></option>
75
+ <option value="medium"><?php _e("Medium"); ?></option>
76
+ <option value="thumbnail"><?php _e("Thumbnail"); ?></option>
77
+ </select>
78
+ or
79
+ <input type="text" size="8" id="<?php esc_attr_e('wp_user_avatar_size_number'); ?>" name="<?php esc_attr_e('wp_user_avatar_size'); ?>" value="" />
80
+ </p>
81
+
82
+ <p><label for="<?php esc_attr_e('wp_user_avatar_align'); ?>"><strong><?php _e("Alignment:"); ?></strong></label>
83
+ <select id="<?php esc_attr_e('wp_user_avatar_align'); ?>" name="<?php esc_attr_e('wp_user_avatar_align'); ?>">
84
+ <option value=""></option>
85
+ <option value="center"><?php _e("Center"); ?></option>
86
+ <option value="left"><?php _e("Left"); ?></option>
87
+ <option value="right"><?php _e("Right"); ?></option>
88
+ </select></p>
89
+
90
+ <h4 class="center">Link to image file, attachment page, or custom URL.</h4>
91
+
92
+ <p>
93
+ <label for="<?php esc_attr_e('wp_user_avatar_link'); ?>"><strong><?php _e("Link to:"); ?></strong></label>
94
+ <select id="<?php esc_attr_e('wp_user_avatar_link'); ?>" name="<?php esc_attr_e('wp_user_avatar_link'); ?>">
95
+ <option value=""></option>
96
+ <option value="file"><?php _e("Image File"); ?></option>
97
+ <option value="attachment"><?php _e("Attachment Page"); ?></option>
98
+ </select>
99
+ </p>
100
+
101
+ <p>
102
+ <label for="<?php esc_attr_e('wp_user_avatar_link_external'); ?>">or</label>
103
+ <input type="text" size="36" id="<?php esc_attr_e('wp_user_avatar_link_external'); ?>" name="<?php esc_attr_e('wp_user_avatar_link'); ?>" value="" />
104
+ </p>
105
+
106
+ <div class="mceActionPanel">
107
+ <input type="submit" id="insert" class="button-primary" name="insert" value="<?php _e("Insert WP User Avatar"); ?>" onclick="insert_wp_user_avatar();" />
108
+ </div>
109
+ </form>
110
+ </body>
111
+ </html>
index.html ADDED
@@ -0,0 +1 @@
1
+ <!-- Thanks for using WP User Avatar! -->
js/wp-user-avatar.js ADDED
@@ -0,0 +1 @@
1
+ function openMediaUploader(a){wp.media.wpUserAvatar={get:function(){return wp.media.view.settings.post.wpUserAvatarId},set:function(c){var b=wp.media.view.settings;b.post.wpUserAvatarId=c;b.post.wpUserAvatarSrc=jQuery("div.attachment-info").find("img").attr("src");if(b.post.wpUserAvatarId){setWPUserAvatar(b.post.wpUserAvatarId,b.post.wpUserAvatarSrc);jQuery("#wp_user_avatar_radio").trigger("click")}},frame:function(){if(this._frame){return this._frame}this._frame=wp.media({state:"library",states:[new wp.media.controller.Library({title:"Edit WP User Avatar: "+a})]});this._frame.on("open",function(){var b=this.state().get("selection");id=jQuery("#wp-user-avatar").val();attachment=wp.media.attachment(id);attachment.fetch();b.add(attachment?[attachment]:[])},this._frame);this._frame.on("toolbar:create:select",function(b){this.createSelectToolbar(b,{text:"Set WP User Avatar"})},this._frame);this._frame.state("library").on("select",this.select);return this._frame},select:function(d){var c=wp.media.view.settings,b=this.get("selection").single();wp.media.wpUserAvatar.set(b?b.id:-1)},init:function(){jQuery("body").on("click","#add-wp-user-avatar",function(b){b.preventDefault();b.stopPropagation();wp.media.wpUserAvatar.frame().open()})}};jQuery(wp.media.wpUserAvatar.init)}function openThickboxUploader(b,a){jQuery("body").on("click","#add-wp-user-avatar",function(c){c.preventDefault();tb_show("Edit WP User Avatar: "+b,a)})}function setWPUserAvatar(b,a){jQuery("#wp-user-avatar",window.parent.document).val(b);jQuery("#wp-user-avatar-preview",window.parent.document).find("img").attr("src",a).removeAttr("width","").removeAttr("height","");jQuery("#wp-user-avatar-message",window.parent.document).show();jQuery("#remove-wp-user-avatar",window.parent.document).show();jQuery("#wp-user-avatar-notice",window.parent.document).hide();jQuery("#wp_user_avatar_radio",window.parent.document).trigger("click");if(typeof(wp)!="undefined"){wp.media.wpUserAvatar.frame().close()}else{window.parent.tb_remove()}}function removeWPUserAvatar(a,b){jQuery("body").on("click","#remove-wp-user-avatar",function(c){c.preventDefault();jQuery(this).hide();jQuery("#wp-user-avatar-preview").find("img").attr("src",b).removeAttr("width","").removeAttr("height","");jQuery("#wp-user-avatar").val("");jQuery("#wp-user-avatar-message, #wp-user-avatar-notice").show();jQuery("#wp_user_avatar_radio").trigger("click")})};
readme.txt ADDED
@@ -0,0 +1,290 @@
1
+ === WP User Avatar ===
2
+
3
+ Contributors: bangbay
4
+ Donate link: http://siboliban.org/donate
5
+ Tags: author image, author photo, author avatar, avatar, bbPress, profile avatar, profile image, user avatar, user image, user photo
6
+ Requires at least: 3.3
7
+ Tested up to: 3.5.1
8
+ Stable tag: 1.2.5
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
+ WP User Avatar also lets you:
19
+
20
+ * Upload your own Default Avatar in your Discussion settings.
21
+ * Show the user's [gravatar.com](http://gravatar.com/) avatar or Default Avatar if the user doesn't have a WP User Avatar image.
22
+ * Use the <code>[avatar]</code> shortcode in your posts. The shortcode will work with any theme, whether it has avatar support or not.
23
+
24
+ This plugin uses the 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. Upload your own Default Avatar in your Discussion settings (optional).
33
+ 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/).
34
+ 7. You can also use the <code>[avatar]</code> shortcode in your posts. The shortcode will work with any theme, whether it has avatar support or not.
35
+
36
+ **Example Usage**
37
+
38
+ Within [The Loop](http://codex.wordpress.org/The_Loop), you may be using:
39
+
40
+ `<?php echo get_avatar(get_the_author_meta('ID'), 96); ?>`
41
+
42
+ Replace this function with:
43
+
44
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96); ?>`
45
+
46
+ You can also use the values "original", "large", "medium", or "thumbnail" for your avatar size:
47
+
48
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 'medium'); ?>`
49
+
50
+ You can also add an alignment of "left", "right", or "center":
51
+
52
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96, 'left'); ?>`
53
+
54
+ On an author page outside of [The Loop](http://codex.wordpress.org/The_Loop), you may be using:
55
+
56
+ `<?php
57
+ $user = get_user_by('slug', $author_name);
58
+ echo get_avatar($user->ID, 96);
59
+ ?>`
60
+
61
+ Replace this function with:
62
+
63
+ `<?php
64
+ $user = get_user_by('slug', $author_name);
65
+ echo get_wp_user_avatar($user->ID, 96);
66
+ ?>`
67
+
68
+ If you leave the options blank, WP User Avatar will detect whether you're inside [The Loop](http://codex.wordpress.org/The_Loop) or on an author page and return the correct avatar in the default 96x96 size:
69
+
70
+ `<?php echo get_wp_user_avatar(); ?>`
71
+
72
+ The function <code>get_wp_user_avatar</code> can also fall back to <code>get_avatar</code> if there is no WP User Avatar image. 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](http://gravatar.com/) avatar or Default Avatar.
73
+
74
+ **Other Available Functions**
75
+
76
+ = [avatar] shortcode =
77
+
78
+ You can use the <code>[avatar]</code> shortcode in your posts. It will detect the author of the post or you can specify an author by username. You can specify a size, alignment, and link, but they are optional. For links, you can link to the original image file, attachment page, or a custom URL.
79
+
80
+ `[avatar user="admin" size="medium" align="left" link="file"]`
81
+
82
+ = get_wp_user_avatar_src =
83
+
84
+ 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:
85
+
86
+ `<a href="<?php echo get_wp_user_avatar_src($user_id, 'large'); ?>">
87
+ <?php echo get_wp_user_avatar($user_id, 'thumbnail'); ?>
88
+ </a>`
89
+
90
+ = has_wp_user_avatar =
91
+
92
+ 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](http://codex.wordpress.org/The_Loop) or author page:
93
+
94
+ `<?php
95
+ if ( has_wp_user_avatar($user_id) ) {
96
+ echo get_wp_user_avatar($user_id, 96);
97
+ } else {
98
+ echo '<img src="my-alternate-image.jpg" />';
99
+ }
100
+ ?>`
101
+
102
+ == Frequently Asked Questions ==
103
+
104
+ = How do I use WP User Avatar? =
105
+
106
+ 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:
107
+
108
+ = get_wp_user_avatar =
109
+
110
+ 1. Allows you to use the values "original", "large", "medium", or "thumbnail" for your avatar size.
111
+ 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.
112
+ 3. Optionally adds CSS classes "alignleft", "alignright", or "aligncenter" to position your avatar.
113
+ 4. Shows nothing if the user has no WP User Avatar image.
114
+ 5. Shows the user's [gravatar.com](http://gravatar.com/) avatar or Default Avatar only if "Show Avatars" is enabled in your Discussion settings.
115
+
116
+ = get_avatar =
117
+
118
+ 1. Requires you to enable "Show Avatars" in your Discussion settings to show any avatars.
119
+ 2. Accepts only numeric values for your avatar size.
120
+ 3. Always adds a fixed width and height to your image. This may cause problems if you use responsive CSS in your theme.
121
+ 4. Shows the user's [gravatar.com](http://gravatar.com/) avatar or 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.)
122
+ 5. Requires no changes to your theme files if you are currently using <code>get_avatar</code>.
123
+
124
+ [Read more about get_avatar in the WordPress Function Reference](http://codex.wordpress.org/Function_Reference/get_avatar).
125
+
126
+ = Can I create a custom Default Avatar? =
127
+ In your Discussion settings, you can upload your own Default Avatar.
128
+
129
+ = Can I insert WP User Avatar directly into a post? =
130
+
131
+ You can use the <code>[avatar]</code> shortcode in your posts. It will detect the author of the post or you can specify an author by username. You can specify a size, alignment, and link, but they are optional. For links, you can link to the original image file, attachment page, or a custom URL.
132
+
133
+ `[avatar user="admin" size="96" align="left" link="file"]`
134
+
135
+ Outputs:
136
+
137
+ `<a href="{fileURL}" class="wp-user-avatar-link wp-user-avatar-file">
138
+ <img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />
139
+ </a>`
140
+
141
+ = Can Contributors or Subscribers choose their own WP User Avatar image? =
142
+
143
+ Users need <code>upload_files</code> capability to choose their own WP User Avatar image. This means that only Administrators, Editors, and Authors can choose their own WP User Avatar image. Contributors and Subscribers cannot upload images. Administators can choose WP User Avatar images for Contributors and Subscribers.
144
+
145
+ [Read more about Roles and Capabilities here](http://codex.wordpress.org/Roles_and_Capabilities).
146
+
147
+ = Will WP User Avatar work with comment author avatars? =
148
+
149
+ Yes, for registered users. Non-registered comment authors will show their [gravatar.com](http://gravatar.com/) avatars or Default Avatar.
150
+
151
+ = Will WP User Avatar work with bbPress? =
152
+
153
+ Yes, but only users that have <code>upload_files</code> capability can choose their own WP User Avatar image.
154
+
155
+ = How can I see which users have an avatar? =
156
+
157
+ For Administrators, WP User Avatar adds a column with avatar thumbnails to your Users list 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.
158
+
159
+ = What CSS can I use with WP User Avatar? =
160
+
161
+ 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:
162
+
163
+ `<?php echo get_wp_user_avatar($user_id, 96, 'left'); ?>`
164
+
165
+ Outputs:
166
+
167
+ `<img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />`
168
+
169
+ **Note:** "alignleft", "alignright", and aligncenter" are common WordPress CSS classes, but not every theme supports them. Contact the theme author to add those CSS classes.
170
+
171
+ 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:
172
+
173
+ `<?php echo get_wp_user_avatar($user_id, 'medium'); ?>`
174
+
175
+ Outputs:
176
+
177
+ `<img src="{imageURL}" class="wp-user-avatar wp-user-avatar-medium" />`
178
+
179
+ **Note:** WordPress adds more CSS classes to the avatar not listed here.
180
+
181
+ If you use the <code>[avatar]</code> shortcode, WP User Avatar will add the CSS class "wp-user-avatar-link" to the link. It will also add CSS classes based on link type.
182
+
183
+ * Image File: wp-user-avatar-file
184
+ * Attachment: wp-user-avatar-attachment
185
+ * Custom URL: wp-user-avatar-custom
186
+
187
+ `[avatar user="admin" size="96" align="left" link="attachment"]`
188
+
189
+ Outputs:
190
+
191
+ `<a href="{attachmentURL}" class="wp-user-avatar-link wp-user-avatar-attachment">
192
+ <img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />
193
+ </a>`
194
+
195
+ = What other functions are available for WP User Avatar? =
196
+ * <code>get_wp_user_avatar_src</code>: retrieves just the image URL
197
+ * <code>has_wp_user_avatar</code>: checks if the user has a WP User Avatar image
198
+ * [See example usage here](http://wordpress.org/extend/plugins/wp-user-avatar/installation/)
199
+
200
+ == Screenshots ==
201
+
202
+ 1. WP User Avatar lets you upload your own Default Avatar.
203
+ 2. WP User Avatar adds a field to your edit profile page.
204
+ 3. After you've chosen a WP User Avatar image, you will see the option to remove it.
205
+ 4. WP User Avatar adds a button to insert the [avatar] shortcode in the Visual Editor.
206
+ 5. Options for the [avatar] shortcode.
207
+
208
+ == Changelog ==
209
+
210
+ = 1.2.5 =
211
+ * Bug Fix: Comment author showing wrong avatar
212
+ * Bug Fix: Avatar adds fixed dimensions when non-numeric size is used
213
+ * Update: Use local image for default avatar instead of calling image from Gravatar
214
+
215
+ = 1.2.4 =
216
+ * Bug Fix: Show default avatar when user removes custom avatar
217
+ * Bug Fix: Default Avatar save setting
218
+
219
+ = 1.2.3 =
220
+ * Bug Fix: Show default avatar when user removes custom avatar
221
+ * Bug Fix: Default Avatar save setting
222
+
223
+ = 1.2.2 =
224
+ * Add: Ability for bbPress users to edit avatar on front profile page
225
+ * Add: Link options for shortcode
226
+ * Bug Fix: Show WP User Avatar only to users with upload_files capability
227
+
228
+ = 1.2.1 =
229
+ * Add: TinyMCE button
230
+ * Update: Clean up redundant code
231
+ * Update: Compatibility only back to WordPress 3.3
232
+
233
+ = 1.2 =
234
+ * Add: Default Avatar setting
235
+
236
+ = 1.1.7 =
237
+ * Bug Fix: Change update_usermeta to update_user_meta
238
+
239
+ = 1.1.6 =
240
+ * Bug Fix: Image not showing in user profile edit
241
+
242
+ = 1.1.5a =
243
+ * Update: readme.txt
244
+
245
+ = 1.1.5 =
246
+ * Bug Fix: Remove stray curly bracket
247
+
248
+ = 1.1.4 =
249
+ * Bug Fix: Change get_usermeta to get_user_meta
250
+ * Bug Fix: Non-object warning when retrieving user ID
251
+
252
+ = 1.1.3 =
253
+ * Bug Fix: Comment author with no e-mail address
254
+
255
+ = 1.1.2 =
256
+ * Remove: Unused variables
257
+
258
+ = 1.1.1 =
259
+ * Bug Fix: Capabilities error in comment avatar
260
+
261
+ = 1.1 =
262
+ * Add: Add filter for get_avatar
263
+ * Add: CSS alignment classes
264
+ * Add: Replace comment author avatar
265
+ * Add: Shortcode
266
+ * Update: readme.txt
267
+
268
+ = 1.0.2 =
269
+ * Update: FAQ
270
+ * Remove: CSS that hides "Insert into Post"
271
+
272
+ = 1.0.1 =
273
+ * Add: CSS classes to image output
274
+
275
+ = 1.0 =
276
+ * Initial release
277
+
278
+ == Upgrade Notice ==
279
+
280
+ = 1.2.2 =
281
+ * New Features: Link options for shortcode, bbPress integration
282
+
283
+ = 1.2.1 =
284
+ * New Feature: Shortcode insertion button for Visual Editor
285
+
286
+ = 1.2 =
287
+ * New Feature: Default Avatar customization
288
+
289
+ = 1.1 =
290
+ * New Features: [avatar] shortcode, direct replacement of get_avatar() and comment author avatar, more CSS classes
wp-user-avatar.php ADDED
@@ -0,0 +1,472 @@
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.2.5
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. Add your own Default Avatar.
10
+ Version: 1.2.5
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
+ // Define global variables
21
+ $avatar_default = get_option('avatar_default');
22
+ $avatar_default_wp_user_avatar = get_option('avatar_default_wp_user_avatar');
23
+ $mustache_original = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar.png';
24
+ $mustache_medium = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-300x300.png';
25
+ $mustache_thumbnail = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-150x150.png';
26
+ $mustache_avatar = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-96x96.png';
27
+ $mustache_admin = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-32x32.png';
28
+ $ssl = is_ssl() ? 's' : '';
29
+
30
+ // Load add-ons
31
+ include_once(WP_USER_AVATAR_ABSPATH.'includes/tinymce.php');
32
+
33
+ // Initialize default settings
34
+ register_activation_hook(__FILE__, 'wp_user_avatar_options');
35
+
36
+ // Remove user metadata and options on plugin delete
37
+ register_uninstall_hook(__FILE__, 'wp_user_avatar_delete_setup');
38
+
39
+ // Settings saved to wp_options
40
+ function wp_user_avatar_options(){
41
+ add_option('avatar_default_wp_user_avatar','');
42
+ }
43
+ add_action('init', 'wp_user_avatar_options');
44
+
45
+ // Update default avatar to new format
46
+ function wp_user_avatar_default_avatar(){
47
+ global $avatar_default, $avatar_default_wp_user_avatar, $mustache_original;
48
+ if($avatar_default == $mustache_original){
49
+ update_option('avatar_default', 'wp_user_avatar');
50
+ }
51
+ if(!empty($avatar_default_wp_user_avatar)){
52
+ $avatar_default_wp_user_avatar_image = wp_get_attachment_image_src($avatar_default_wp_user_avatar, 'medium');
53
+ if($avatar_default == $avatar_default_wp_user_avatar_image[0]){
54
+ update_option('avatar_default', 'wp_user_avatar');
55
+ }
56
+ }
57
+ }
58
+ add_action('init', 'wp_user_avatar_default_avatar');
59
+
60
+ // Remove user metadata and options on plugin delete
61
+ function wp_user_avatar_delete_setup(){
62
+ $users = get_users();
63
+ foreach($users as $user){
64
+ delete_user_meta($user->ID, 'wp_user_avatar');
65
+ }
66
+ delete_option('avatar_default_wp_user_avatar');
67
+ update_option('avatar_default', 'mystery');
68
+ }
69
+
70
+ // WP User Avatar
71
+ if(!class_exists('wp_user_avatar')){
72
+ class wp_user_avatar{
73
+ function wp_user_avatar(){
74
+ // Only works if user can upload files
75
+ if(current_user_can('upload_files')){
76
+ add_action('show_user_profile', array('wp_user_avatar','action_show_user_profile'));
77
+ add_action('edit_user_profile', array($this,'action_show_user_profile'));
78
+ add_action('personal_options_update', array($this,'action_process_option_update'));
79
+ add_action('edit_user_profile_update', array($this,'action_process_option_update'));
80
+ add_action('discussion_update', array($this,'action_process_option_update'));
81
+ add_action('admin_enqueue_scripts', array($this, 'media_upload_scripts'));
82
+ }
83
+ // Only add attachment field for WP 3.4 and older
84
+ if(!function_exists('wp_enqueue_media')){
85
+ add_filter('attachment_fields_to_edit', array($this, 'add_wp_user_avatar_attachment_field_to_edit'), 10, 2);
86
+ }
87
+ // Hide column in Users table if avatars are shown
88
+ if(get_option('show_avatars') != '1'){
89
+ add_filter('manage_users_columns', array($this, 'add_wp_user_avatar_column'), 10, 1);
90
+ add_filter('manage_users_custom_column', array($this, 'show_wp_user_avatar_column'), 10, 3);
91
+ }
92
+ // Load scripts in front pages for logged in bbPress users
93
+ if(class_exists('bbPress') && is_user_logged_in() && current_user_can('upload_files')){
94
+ add_action('wp_enqueue_scripts', array($this, 'media_upload_scripts'));
95
+ }
96
+ }
97
+
98
+ // Add to edit user profile
99
+ function action_show_user_profile($user){
100
+ global $current_user;
101
+ $wp_user_avatar = get_user_meta($user->ID, 'wp_user_avatar', true);
102
+ $hide_notice = has_wp_user_avatar($user->ID) ? ' class="hide-me"' : '';
103
+ $hide_remove = !has_wp_user_avatar($user->ID) ? ' hide-me' : '';
104
+ $avatar_medium_src = (get_option('show_avatars') == '1') ? get_avatar_original($user->user_email, 96) : includes_url().'images/blank.gif';
105
+ $avatar_medium = has_wp_user_avatar($user->ID) ? get_wp_user_avatar_src($user->ID, 'medium') : $avatar_medium_src;
106
+ $profile = ($current_user->ID == $user->ID) ? 'Profile' : 'User';
107
+ ?>
108
+ <?php if(is_admin()) : ?>
109
+ <h3><?php _e('WP User Avatar') ?></h3>
110
+ <table class="form-table">
111
+ <tr>
112
+ <th><label for="wp_user_avatar"><?php _e('WP User Avatar'); ?></label></th>
113
+ <td>
114
+ <input type="hidden" name="wp-user-avatar" id="wp-user-avatar" value="<?php echo $wp_user_avatar; ?>" />
115
+ <p><button type="button" class="button" id="add-wp-user-avatar"><?php _e('Edit WP User Avatar'); ?></button></p>
116
+ <p id="wp-user-avatar-preview"><?php echo '<img src="'.$avatar_medium.'" alt="" />'; ?></p>
117
+ <?php if(get_option('show_avatars') == '1') : ?>
118
+ <p id="wp-user-avatar-notice"<?php echo $hide_notice; ?>><?php _e('This is your default avatar.'); ?></p>
119
+ <?php endif; ?>
120
+ <p><button type="button" class="button<?php echo $hide_remove; ?>" id="remove-wp-user-avatar"><?php _e('Remove'); ?></button></p>
121
+ <p id="wp-user-avatar-message"><?php _e('Press "Update '.$profile.'" to save your changes.'); ?></p>
122
+ </td>
123
+ </tr>
124
+ </table>
125
+ <?php elseif(class_exists('bbPress')) : ?>
126
+ <h2 class="entry-title"><?php _e('WP User Avatar'); ?></h2>
127
+ <fieldset class="bbp-form">
128
+ <legend><?php _e('WP User Avatar'); ?></legend>
129
+ <input type="hidden" name="wp-user-avatar" id="wp-user-avatar" value="<?php echo $wp_user_avatar; ?>" />
130
+ <p><button type="button" class="button" id="add-wp-user-avatar"><?php _e('Edit WP User Avatar'); ?></button></p>
131
+ <p id="wp-user-avatar-preview"><?php echo '<img src="'.$avatar_medium.'" alt="" />'; ?></p>
132
+ <?php if(get_option('show_avatars') == '1') : ?>
133
+ <p id="wp-user-avatar-notice"<?php echo $hide_notice; ?>><?php _e('This is your default avatar.'); ?></p>
134
+ <?php endif; ?>
135
+ <p><button type="button" class="button" id="remove-wp-user-avatar"<?php echo $hide_remove; ?>><?php _e('Remove'); ?></button></p>
136
+ <p id="wp-user-avatar-message"><?php _e('Press "Update '.$profile.'" to save your changes.'); ?></p>
137
+ </fieldset>
138
+ <?php endif; ?>
139
+ <?php
140
+ echo edit_default_wp_user_avatar($user->display_name, $avatar_medium_src, $avatar_medium_src);
141
+ }
142
+
143
+ // Update user meta
144
+ function action_process_option_update($user_id){
145
+ update_user_meta($user_id, 'wp_user_avatar', (isset($_POST['wp-user-avatar']) ? intval($_POST['wp-user-avatar']) : ''));
146
+ }
147
+
148
+ // Add button to attach image
149
+ function add_wp_user_avatar_attachment_field_to_edit($fields, $post){
150
+ $image = wp_get_attachment_image_src($post->ID, "medium");
151
+ $button = '<button type="button" class="button" id="set-wp-user-avatar-image" onclick="setWPUserAvatar(\''.$post->ID.'\', \''.$image[0].'\')">Set WP User Avatar</button>';
152
+ $fields['wp-user-avatar'] = array(
153
+ 'label' => __('WP User Avatar'),
154
+ 'input' => 'html',
155
+ 'html' => $button
156
+ );
157
+ return $fields;
158
+ }
159
+
160
+ // Add column to Users page
161
+ function add_wp_user_avatar_column($columns){
162
+ return $columns + array('wp-user-avatar' => __('WP User Avatar'));;
163
+ }
164
+
165
+ // Show thumbnail of wp_user_avatar
166
+ function show_wp_user_avatar_column($value, $column_name, $user_id){
167
+ $wp_user_avatar = get_user_meta($user_id, 'wp_user_avatar', true);
168
+ $wp_user_avatar_image = wp_get_attachment_image($wp_user_avatar, array(32,32));
169
+ if($column_name == 'wp-user-avatar'){
170
+ return $wp_user_avatar_image;
171
+ }
172
+ }
173
+
174
+ // Media uploader
175
+ function media_upload_scripts(){
176
+ global $ssl;
177
+ if(function_exists('wp_enqueue_media')){
178
+ wp_enqueue_media();
179
+ } else {
180
+ wp_enqueue_script('media-upload');
181
+ wp_enqueue_script('thickbox');
182
+ wp_enqueue_style('thickbox');
183
+ }
184
+ wp_enqueue_script('wp-user-avatar', WP_USER_AVATAR_URLPATH.'js/wp-user-avatar.js', '', '1.2.5');
185
+ wp_enqueue_style('wp-user-avatar', WP_USER_AVATAR_URLPATH.'css/wp-user-avatar.css', '', '1.2.5');
186
+ }
187
+ }
188
+
189
+ // Uploader scripts
190
+ function edit_default_wp_user_avatar($section, $avatar_medium, $avatar_thumb){ ?>
191
+ <script type="text/javascript">
192
+ jQuery(function(){
193
+ <?php if(function_exists('wp_enqueue_media')) : // Backbone uploader for WP 3.5+ ?>
194
+ openMediaUploader("<?php echo $section; ?>");
195
+ <?php else : // Fall back to Thickbox uploader ?>
196
+ openThickboxUploader("<?php echo $section; ?>", "<?php echo get_admin_url(); ?>media-upload.php?post_id=0&type=image&tab=library&TB_iframe=1");
197
+ <?php endif; ?>
198
+ removeWPUserAvatar("<?php echo htmlspecialchars_decode($avatar_medium); ?>", "<?php echo htmlspecialchars_decode($avatar_thumb); ?>");
199
+ });
200
+ </script>
201
+ <?php
202
+ }
203
+
204
+ // Returns true if user has Gravatar-hosted image
205
+ function has_gravatar($id_or_email, $has_gravatar=false){
206
+ global $ssl;
207
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
208
+ $email = !empty($user) ? $user->user_email : $id_or_email;
209
+ if(!empty($email)){
210
+ $hash = md5(strtolower(trim($email)));
211
+ $gravatar = 'http'.$ssl.'://www.gravatar.com/avatar/'.$hash.'?d=404';
212
+ $headers = @get_headers($gravatar);
213
+ $has_gravatar = !preg_match("|200|", $headers[0]) ? false : true;
214
+ }
215
+ return $has_gravatar;
216
+ }
217
+
218
+ // Returns true if user has wp_user_avatar
219
+ function has_wp_user_avatar($user_id=''){
220
+ global $post;
221
+ if(empty($user_id)){
222
+ $author_name = get_query_var('author_name');
223
+ $user = is_author() ? get_user_by('slug', $author_name) : get_the_author_meta('ID');
224
+ $user_id = $user->ID;
225
+ }
226
+ $wp_user_avatar = get_user_meta($user_id, 'wp_user_avatar', true);
227
+ if(!empty($wp_user_avatar)){
228
+ return true;
229
+ }
230
+ }
231
+
232
+ // Find wp_user_avatar, show get_avatar if empty
233
+ function get_wp_user_avatar($id_or_email='', $size='96', $align=''){
234
+ global $post, $comment, $avatar_default;
235
+ // Find user ID on comment, author page, or post
236
+ if(is_object($id_or_email)){
237
+ if($comment->user_id != '0'){
238
+ $id_or_email = $comment->user_id;
239
+ $user = get_user_by('id', $id_or_email);
240
+ $email = $user->user_email;
241
+ } elseif(!empty($comment->comment_author_email)){
242
+ $user = get_user_by('email', $comment->comment_author_email);
243
+ $id_or_email = !empty($user) ? $user->ID : $comment->comment_author_email;
244
+ $email = !empty($user) ? $user->user_email : $id_or_email;
245
+ }
246
+ $alt = $comment->comment_author;
247
+ } else {
248
+ if(!empty($id_or_email)){
249
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
250
+ } else {
251
+ $author_name = get_query_var('author_name');
252
+ if(is_author()){
253
+ $user = get_user_by('slug', $author_name);
254
+ } else {
255
+ $user_id = get_the_author_meta('ID');
256
+ $user = get_user_by('id', $user_id);
257
+ }
258
+ }
259
+ $id_or_email = !empty($user) ? $user->ID : '';
260
+ $alt = $user->display_name;
261
+ }
262
+ $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta('wp_user_avatar', $id_or_email) : '';
263
+ $alignclass = !empty($align) ? ' align'.$align : '';
264
+ if(!empty($wp_user_avatar_meta)){
265
+ $get_size = is_numeric($size) ? array($size,$size) : $size;
266
+ $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, $get_size);
267
+ $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : '';
268
+ $wp_user_avatar = '<img src="'.$wp_user_avatar_image[0].'"'.$dimensions.' alt="'.$alt.'" class="wp-user-avatar wp-user-avatar-'.$size.$alignclass.' avatar avatar avatar-'.$size.' photo" />';
269
+ } else {
270
+ if($size == 'original' || $size == 'large' || $size == 'medium' || $size == 'thumbnail'){
271
+ $get_size = ($size == 'original') ? get_option('large_size_w') : get_option($size.'_size_w');
272
+ } else {
273
+ $get_size = $size;
274
+ }
275
+ $avatar = get_avatar($id_or_email, $get_size, $default='', $alt='');
276
+ if(!is_numeric($size)){
277
+ $avatar = preg_replace("/(width|height)=\'\d*\'\s/", '', $avatar);
278
+ $avatar = preg_replace('/(width|height)=\"\d*\"\s/', '', $avatar);
279
+ $avatar = str_replace('wp-user-avatar wp-user-avatar-'.$get_size.' ', '', $avatar);
280
+ $avatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$size.$alignclass." ", $avatar);
281
+ }
282
+ $wp_user_avatar = $avatar;
283
+ }
284
+ return $wp_user_avatar;
285
+ }
286
+
287
+ // Return just the image src
288
+ function get_wp_user_avatar_src($id_or_email, $size='', $align=''){
289
+ $wp_user_avatar_image = get_wp_user_avatar($id_or_email, $size, $align);
290
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wp_user_avatar_image, $matches, PREG_SET_ORDER);
291
+ $wp_user_avatar_image_src = $matches [0] [1];
292
+ return $wp_user_avatar_image_src;
293
+ }
294
+
295
+ // Replace get_avatar
296
+ function get_wp_user_avatar_alt($avatar, $id_or_email, $size='', $default='', $alt=''){
297
+ global $post, $pagenow, $comment, $avatar_default, $avatar_default_wp_user_avatar, $mustache_original, $mustache_medium, $mustache_thumbnail, $mustache_avatar, $mustache_admin;
298
+ // Find user ID on comment, author page, or post
299
+ $email = '';
300
+ if(is_object($id_or_email)){
301
+ if($comment->user_id != '0'){
302
+ $id_or_email = $comment->user_id;
303
+ $user = get_user_by('id', $id_or_email);
304
+ $email = $user->user_email;
305
+ } elseif(!empty($comment->comment_author_email)){
306
+ $user = get_user_by('email', $comment->comment_author_email);
307
+ $id_or_email = !empty($user) ? $user->ID : $comment->comment_author_email;
308
+ $email = !empty($user) ? $user->user_email : $id_or_email;
309
+ }
310
+ $alt = $comment->comment_author;
311
+ } else {
312
+ if(!empty($id_or_email)){
313
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
314
+ } else {
315
+ $author_name = get_query_var('author_name');
316
+ if(is_author()){
317
+ $user = get_user_by('slug', $author_name);
318
+ } else {
319
+ $user_id = get_the_author_meta('ID');
320
+ $user = get_user_by('id', $user_id);
321
+ }
322
+ }
323
+ $id_or_email = $user->ID;
324
+ $email = $user->user_email;
325
+ $alt = $user->display_name;
326
+ }
327
+ $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta('wp_user_avatar', $id_or_email) : '';
328
+ if(!empty($wp_user_avatar_meta)){
329
+ $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, array($size,$size));
330
+ $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : '';
331
+ $default = $wp_user_avatar_image[0];
332
+ $avatar = '<img src="'.$default.'"'.$dimensions.' alt="'.$alt.'" class="wp-user-avatar wp-user-avatar-'.$size.' avatar avatar-'.$size.' photo" />';
333
+ } else {
334
+ if(!has_gravatar($email) && $avatar_default == 'wp_user_avatar'){
335
+ if(!empty($avatar_default_wp_user_avatar)){
336
+ $avatar_default_wp_user_avatar_image = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array($size,$size));
337
+ $default = $avatar_default_wp_user_avatar_image[0];
338
+ $dimensions = is_numeric($size) ? ' width="'.$avatar_default_wp_user_avatar_image[1].'" height="'.$avatar_default_wp_user_avatar_image[2].'"' : '';
339
+ } else {
340
+ if($size > get_option('medium_size_w')){
341
+ $default = $mustache_original;
342
+ } elseif($size <= get_option('medium_size_w') && $size > get_option('thumbnail_size_w')){
343
+ $default = $mustache_medium;
344
+ } elseif($size <= get_option('thumbnail_size_w') && $size > 96){
345
+ $default = $mustache_thumbnail;
346
+ } elseif($size <= 96 && $size > 32){
347
+ $default = $mustache_avatar;
348
+ } elseif($size <= 32){
349
+ $default = $mustache_admin;
350
+ }
351
+ $dimensions = is_numeric($size) ? ' width="'.$size.'" height="'.$size.'"' : '';
352
+ }
353
+ $avatar = "<img src='".$default."'".$dimensions." alt='".$alt."' class='wp-user-avatar wp-user-avatar-".$size." avatar avatar-".$size." photo avatar-default' />";
354
+ }
355
+ }
356
+ $wp_user_avatar = $avatar;
357
+ return $wp_user_avatar;
358
+ }
359
+ add_filter('get_avatar', 'get_wp_user_avatar_alt', 10, 6);
360
+
361
+ // Get original avatar
362
+ function get_avatar_original($id_or_email, $size='', $default='', $alt=''){
363
+ global $avatar_default, $avatar_default_wp_user_avatar, $mustache_avatar;
364
+ remove_filter('get_avatar', 'get_wp_user_avatar_alt');
365
+ if(!has_gravatar($id_or_email) && $avatar_default == 'wp_user_avatar'){
366
+ if(!empty($avatar_default_wp_user_avatar)){
367
+ $avatar_default_wp_user_avatar_image = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array($size,$size));
368
+ $default = $avatar_default_wp_user_avatar_image[0];
369
+ } else {
370
+ $default = $mustache_avatar;
371
+ }
372
+ } else {
373
+ $wp_user_avatar_image = get_avatar($id_or_email);
374
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wp_user_avatar_image, $matches, PREG_SET_ORDER);
375
+ $default = $matches [0] [1];
376
+ }
377
+ return $default;
378
+ }
379
+
380
+ // Shortcode
381
+ function wp_user_avatar_shortcode($atts, $content){
382
+ // EXAMPLE USAGE:
383
+ // [avatar size="medium"]
384
+
385
+ // Set shortcode attributes
386
+ extract(shortcode_atts(array('user' => '', 'size' => '96', 'align' => '', 'link' => ''), $atts));
387
+ $get_user = get_user_by('slug', $user);
388
+ $id_or_email = !empty($get_user) ? $get_user->ID : '';
389
+ if(!empty($link)){
390
+ $link_class = $link;
391
+ if($link == 'file'){
392
+ $image_link = get_wp_user_avatar_src($id_or_email, 'original', $align);
393
+ } elseif($link == 'attachment'){
394
+ $image_link = get_attachment_link(get_the_author_meta('wp_user_avatar', $id_or_email));
395
+ } else {
396
+ $image_link = $link;
397
+ $link_class = 'custom';
398
+ }
399
+ $wp_user_avatar = '<a href="'.$image_link.'" class="wp-user-avatar-link wp-user-avatar-'.$link_class.'">'.get_wp_user_avatar($id_or_email, $size, $align).'</a>';
400
+ } else {
401
+ $wp_user_avatar = get_wp_user_avatar($id_or_email, $size, $align);
402
+ }
403
+ return $wp_user_avatar;
404
+ }
405
+ add_shortcode('avatar','wp_user_avatar_shortcode');
406
+
407
+ // Add default avatar
408
+ function add_default_wp_user_avatar($avatar_list){
409
+ global $avatar_default, $avatar_default_wp_user_avatar, $mustache_medium, $mustache_admin;
410
+ remove_filter('get_avatar', 'get_wp_user_avatar_alt');
411
+ $avatar_defaults = array(
412
+ 'mystery' => __('Mystery Man'),
413
+ 'blank' => __('Blank'),
414
+ 'gravatar_default' => __('Gravatar Logo'),
415
+ 'identicon' => __('Identicon (Generated)'),
416
+ 'wavatar' => __('Wavatar (Generated)'),
417
+ 'monsterid' => __('MonsterID (Generated)'),
418
+ 'retro' => __('Retro (Generated)')
419
+ );
420
+ if(empty($avatar_default)){
421
+ $avatar_default = 'mystery';
422
+ }
423
+ $size = 32;
424
+ $avatar_list = '';
425
+ foreach($avatar_defaults as $default_key => $default_name){
426
+ $selected = ($avatar_default == $default_key) ? 'checked="checked" ' : '';
427
+ $avatar_list .= "\n\t<label><input type='radio' name='avatar_default' id='avatar_{$default_key}' value='".esc_attr($default_key)."' {$selected}/> ";
428
+ $avatar = get_avatar('unknown@gravatar.com', $size, $default_key);
429
+ $avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1&amp;forcedefault=1'", $avatar);
430
+ $avatar_list .= ' '.$default_name.'</label>';
431
+ $avatar_list .= '<br />';
432
+ }
433
+ if(!empty($avatar_default_wp_user_avatar)){
434
+ $avatar_medium_src = wp_get_attachment_image_src($avatar_default_wp_user_avatar, 'medium');
435
+ $avatar_thumb_src = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array(32,32));
436
+ $avatar_medium = $avatar_medium_src[0];
437
+ $avatar_thumb = $avatar_thumb_src[0];
438
+ $hide_remove = '';
439
+ } else {
440
+ $avatar_medium = $mustache_medium;
441
+ $avatar_thumb = $mustache_admin;
442
+ $hide_remove = ' class="hide-me"';
443
+ }
444
+ $selected_avatar = ($avatar_default == 'wp_user_avatar') ? ' checked="checked" ' : '';
445
+ $avatar_thumb_img = '<div id="wp-user-avatar-preview"><img src="'.$avatar_thumb.'" width="32" /></div>';
446
+ $wp_user_avatar_list = "\n\t<label><input type='radio' name='avatar_default' id='wp_user_avatar_radio' value='wp_user_avatar'$selected_avatar /> ";
447
+ $wp_user_avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1'", $avatar_thumb_img);
448
+ $wp_user_avatar_list .= ' '.__('WP User Avatar').'</label>';
449
+ $wp_user_avatar_list .= '<p id="edit-wp-user-avatar"><button type="button" class="button" id="add-wp-user-avatar">'.__('Edit WP User Avatar').'</button>';
450
+ $wp_user_avatar_list .= '<a href="#" id="remove-wp-user-avatar"'.$hide_remove.'>'.__('Remove').'</a></p>';
451
+ $wp_user_avatar_list .= '<input type="hidden" id="wp-user-avatar" name="avatar_default_wp_user_avatar" value="'.$avatar_default_wp_user_avatar.'">';
452
+ $wp_user_avatar_list .= '<p id="wp-user-avatar-message">'.__('Press "Save Changes" to save your changes.').'</p>';
453
+ $wp_user_avatar_list .= edit_default_wp_user_avatar('Default Avatar', $mustache_medium, $mustache_admin);
454
+ return $wp_user_avatar_list.$avatar_list;
455
+ }
456
+ add_filter('default_avatar_select', 'add_default_wp_user_avatar');
457
+
458
+ // Add default avatar field to whitelist
459
+ function wp_user_avatar_whitelist_options($whitelist_options){
460
+ $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');
461
+ return $whitelist_options;
462
+ }
463
+ add_filter('whitelist_options', 'wp_user_avatar_whitelist_options');
464
+
465
+ // Initialize wp_user_avatar
466
+ function wp_user_avatar_load(){
467
+ global $wp_user_avatar_instance;
468
+ $wp_user_avatar_instance = new wp_user_avatar();
469
+ }
470
+ add_action('plugins_loaded','wp_user_avatar_load');
471
+ }
472
+ ?>