WP User Avatar - Version 1.3.6

Version Description

  • Add: Target for link in shortcode
  • Update: Clean up code and add more comments
Download this release

Release Info

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

Version 1.3.6

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.3.6
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:285,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.3.6"}}});tinymce.PluginManager.add('wpUserAvatar',tinymce.plugins.wpUserAvatar)})();
includes/tinymce/window.php ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.3.6
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/jquery/jquery.js"></script>
17
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/tinymce/tiny_mce_popup.js"></script>
18
+ <script type="text/javascript" src="<?php echo site_url(); ?>/wp-includes/js/tinymce/utils/form_utils.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
+ var target = document.getElementById('wp_user_avatar_target').value;
30
+
31
+ // Add tag to shortcode only if not blank
32
+ var user_tag = (user != '') ? ' user="' + user + '"' : '';
33
+ var size_tag = (size != '' && size_number == '') ? ' size="' + size + '"' : '';
34
+ size_tag = (size_number != '') ? ' size="' + size_number + '"' : size_tag;
35
+ var align_tag = (align != '') ? ' align="' + align + '"' : '';
36
+ var link_tag = (link != '' && link_external == '') ? ' link="' + link + '"' : '';
37
+ link_tag = (link_external != '') ? ' link="' + link_external + '"' : link_tag;
38
+ var target_tag = document.getElementById('wp_user_avatar_target').checked && (link_tag != '') ? ' target="' + target + '"' : '';
39
+
40
+ shortcode = "<p>[avatar" + user_tag + size_tag + align_tag + link_tag + target_tag + "]</p>";
41
+
42
+ if(window.tinyMCE) {
43
+ window.tinyMCE.execInstanceCommand(window.tinyMCE.activeEditor.id, 'mceInsertContent', false, shortcode);
44
+ tinyMCEPopup.editor.execCommand('mceRepaint');
45
+ tinyMCEPopup.close();
46
+ }
47
+ return;
48
+ }
49
+ </script>
50
+ <style type="text/css">
51
+ form { background: #fff; border: 1px solid #eee; }
52
+ p, h4 { margin: 0; padding: 12px 0 0; }
53
+ h4.center { text-align: center; }
54
+ label { width: 90px; display: inline-block; text-align: right; }
55
+ .mceActionPanel { padding: 7px 0 12px; text-align: center; }
56
+ .mceActionPanel #insert { float: none; width: 180px; margin: 0 auto; }
57
+ </style>
58
+ </head>
59
+ <body id="link" class="wp-core-ui" onload="document.body.style.display='';" style="display:none;">
60
+ <form name="wpUserAvatar" action="#">
61
+ <p><label for="<?php esc_attr_e('wp_user_avatar_user'); ?>"><strong><?php _e("User:"); ?></strong></label>
62
+ <select id="<?php esc_attr_e('wp_user_avatar_user'); ?>" name="<?php esc_attr_e('wp_user_avatar_user'); ?>">
63
+ <option value=""></option>
64
+ <?php $users = get_users(); foreach($users as $user) : ?>
65
+ <option value="<?php echo $user->user_login; ?>"><?php echo $user->display_name; ?></option>
66
+ <?php endforeach; ?>
67
+ </select></p>
68
+
69
+ <h4 class="center">Choose a preset size or enter a number value.</h4>
70
+
71
+ <p>
72
+ <label for="<?php esc_attr_e('wp_user_avatar_size'); ?>"><strong><?php _e("Size:"); ?></strong></label>
73
+ <select id="<?php esc_attr_e('wp_user_avatar_size'); ?>" name="<?php esc_attr_e('wp_user_avatar_size'); ?>">
74
+ <option value=""></option>
75
+ <option value="original"><?php _e("Original"); ?></option>
76
+ <option value="large"><?php _e("Large"); ?></option>
77
+ <option value="medium"><?php _e("Medium"); ?></option>
78
+ <option value="thumbnail"><?php _e("Thumbnail"); ?></option>
79
+ </select>
80
+ or
81
+ <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="" />
82
+ </p>
83
+
84
+ <p><label for="<?php esc_attr_e('wp_user_avatar_align'); ?>"><strong><?php _e("Alignment:"); ?></strong></label>
85
+ <select id="<?php esc_attr_e('wp_user_avatar_align'); ?>" name="<?php esc_attr_e('wp_user_avatar_align'); ?>">
86
+ <option value=""></option>
87
+ <option value="center"><?php _e("Center"); ?></option>
88
+ <option value="left"><?php _e("Left"); ?></option>
89
+ <option value="right"><?php _e("Right"); ?></option>
90
+ </select></p>
91
+
92
+ <h4 class="center">Link to image file, attachment page, or custom URL.</h4>
93
+
94
+ <p>
95
+ <label for="<?php esc_attr_e('wp_user_avatar_link'); ?>"><strong><?php _e("Link to:"); ?></strong></label>
96
+ <select id="<?php esc_attr_e('wp_user_avatar_link'); ?>" name="<?php esc_attr_e('wp_user_avatar_link'); ?>">
97
+ <option value=""></option>
98
+ <option value="file"><?php _e("Image File"); ?></option>
99
+ <option value="attachment"><?php _e("Attachment Page"); ?></option>
100
+ </select>
101
+ </p>
102
+
103
+ <p>
104
+ <label for="<?php esc_attr_e('wp_user_avatar_link_external'); ?>">or</label>
105
+ <input type="text" size="36" id="<?php esc_attr_e('wp_user_avatar_link_external'); ?>" name="<?php esc_attr_e('wp_user_avatar_link_external'); ?>" value="" />
106
+ </p>
107
+
108
+ <p>
109
+ <label for="<?php esc_attr_e('wp_user_avatar_target'); ?>"></label>
110
+ <input type="checkbox" id="<?php esc_attr_e('wp_user_avatar_target'); ?>" name="<?php esc_attr_e('wp_user_avatar_target'); ?>" value="_blank" /> <strong>Open link in a new window</strong>
111
+ </p>
112
+
113
+ <div class="mceActionPanel">
114
+ <input type="submit" id="insert" class="button-primary" name="insert" value="<?php _e("Insert WP User Avatar"); ?>" onclick="insert_wp_user_avatar();" />
115
+ </div>
116
+ </form>
117
+ </body>
118
+ </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(c){wp.media.wpUserAvatar={get:function(){return wp.media.view.settings.post.wpUserAvatarId},set:function(a){var b=wp.media.view.settings;b.post.wpUserAvatarId=a;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: "+c})]});this._frame.on('open',function(){var a=this.state().get('selection');id=jQuery('#wp-user-avatar').val();attachment=wp.media.attachment(id);attachment.fetch();a.add(attachment?[attachment]:[])},this._frame);this._frame.on('toolbar:create:select',function(a){this.createSelectToolbar(a,{text:'Set WP User Avatar'})},this._frame);this._frame.state('library').on('select',this.select);return this._frame},select:function(a){var b=wp.media.view.settings,selection=this.get('selection').single();wp.media.wpUserAvatar.set(selection?selection.id:-1)},init:function(){jQuery('body').on('click','#add-wp-user-avatar',function(e){e.preventDefault();e.stopPropagation();wp.media.wpUserAvatar.frame().open()})}};jQuery(wp.media.wpUserAvatar.init)}function openThickboxUploader(a,b){jQuery('body').on('click','#add-wp-user-avatar',function(e){e.preventDefault();tb_show('Edit WP User Avatar: '+a,b)})}function setWPUserAvatar(a,b){jQuery('#wp-user-avatar',window.parent.document).val(a);jQuery('#wp-user-avatar-preview',window.parent.document).find('img').attr('src',b).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){jQuery('body').on('click','#remove-wp-user-avatar',function(e){e.preventDefault();jQuery(this).hide();jQuery('#wp-user-avatar-preview').find('img').attr('src',a).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,342 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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.3.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. 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. It is also compatible with WordPress Multisite.
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
+ = Posts =
39
+
40
+ Within [The Loop](http://codex.wordpress.org/The_Loop), you may be using:
41
+
42
+ `<?php echo get_avatar(get_the_author_meta('ID'), 96); ?>`
43
+
44
+ Replace this function with:
45
+
46
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96); ?>`
47
+
48
+ You can also use the values "original", "large", "medium", or "thumbnail" for your avatar size:
49
+
50
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 'medium'); ?>`
51
+
52
+ You can also add an alignment of "left", "right", or "center":
53
+
54
+ `<?php echo get_wp_user_avatar(get_the_author_meta('ID'), 96, 'left'); ?>`
55
+
56
+ = Author Page =
57
+
58
+ On an author page outside of [The Loop](http://codex.wordpress.org/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](http://codex.wordpress.org/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> 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.
77
+
78
+ = Comments =
79
+
80
+ For comments, you might have in your template:
81
+
82
+ `<?php echo get_avatar($comment, 32); ?>`
83
+
84
+ Replace this function with:
85
+
86
+ `<?php echo get_wp_user_avatar($comment, 32); ?>`
87
+
88
+ For comments, you must specify the $comment variable.
89
+
90
+ **Other Available Functions**
91
+
92
+ = [avatar] shortcode =
93
+
94
+ 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.
95
+
96
+ `[avatar user="admin" size="medium" align="left" link="file"]`
97
+
98
+ = get_wp_user_avatar_src =
99
+
100
+ 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:
101
+
102
+ `<a href="<?php echo get_wp_user_avatar_src($user_id, 'large'); ?>">
103
+ <?php echo get_wp_user_avatar($user_id, 'thumbnail'); ?>
104
+ </a>`
105
+
106
+ = has_wp_user_avatar =
107
+
108
+ Returns true if the user has a WP User Avatar image. You must specify the user ID:
109
+
110
+ `<?php
111
+ if ( has_wp_user_avatar($user_id) ) {
112
+ echo get_wp_user_avatar($user_id, 96);
113
+ } else {
114
+ echo '<img src="my-alternate-image.jpg" />';
115
+ }
116
+ ?>`
117
+
118
+ == Frequently Asked Questions ==
119
+
120
+ = How do I use WP User Avatar? =
121
+
122
+ 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:
123
+
124
+ = get_wp_user_avatar =
125
+
126
+ 1. Allows you to use the values "original", "large", "medium", or "thumbnail" for your avatar size.
127
+ 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.
128
+ 3. Optionally adds CSS classes "alignleft", "alignright", or "aligncenter" to position your avatar.
129
+ 4. Shows nothing if the user has no WP User Avatar image.
130
+ 5. Shows the user's [gravatar.com](http://gravatar.com/) avatar or Default Avatar only if "Show Avatars" is enabled in your Discussion settings.
131
+
132
+ = get_avatar =
133
+
134
+ 1. Requires you to enable "Show Avatars" in your Discussion settings to show any avatars.
135
+ 2. Accepts only numeric values for your avatar size.
136
+ 3. Always adds a fixed width and height to your image. This may cause problems if you use responsive CSS in your theme.
137
+ 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.)
138
+ 5. Requires no changes to your theme files if you are currently using <code>get_avatar</code>.
139
+
140
+ [Read more about get_avatar in the WordPress Function Reference](http://codex.wordpress.org/Function_Reference/get_avatar).
141
+
142
+ = Can I create a custom Default Avatar? =
143
+ In your Discussion settings, you can upload your own Default Avatar.
144
+
145
+ = Can I insert WP User Avatar directly into a post? =
146
+
147
+ 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.
148
+
149
+ `[avatar user="admin" size="96" align="left" link="file"]`
150
+
151
+ Outputs:
152
+
153
+ `<a href="{fileURL}" class="wp-user-avatar-link wp-user-avatar-file">
154
+ <img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />
155
+ </a>`
156
+
157
+ = Can Contributors or Subscribers choose their own WP User Avatar image? =
158
+
159
+ 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.
160
+
161
+ [Read more about Roles and Capabilities here](http://codex.wordpress.org/Roles_and_Capabilities).
162
+
163
+ = Will WP User Avatar work with comment author avatars? =
164
+
165
+ Yes, for registered users. Non-registered comment authors will show their [gravatar.com](http://gravatar.com/) avatars or Default Avatar.
166
+
167
+ = Will WP User Avatar work with bbPress? =
168
+
169
+ Yes, but only users that have <code>upload_files</code> capability can choose their own WP User Avatar image.
170
+
171
+ = Will WP User Avatar work with WordPress Multisite? =
172
+
173
+ Yes, however, each site has its own avatar settings. If you set a WP User Avatar image on one site, you have to set it again for different sites in your network.
174
+
175
+ = How can I see which users have an avatar? =
176
+
177
+ 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.
178
+
179
+ = What CSS can I use with WP User Avatar? =
180
+
181
+ 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:
182
+
183
+ `<?php echo get_wp_user_avatar($user_id, 96, 'left'); ?>`
184
+
185
+ Outputs:
186
+
187
+ `<img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />`
188
+
189
+ **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.
190
+
191
+ 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:
192
+
193
+ `<?php echo get_wp_user_avatar($user_id, 'medium'); ?>`
194
+
195
+ Outputs:
196
+
197
+ `<img src="{imageURL}" class="wp-user-avatar wp-user-avatar-medium" />`
198
+
199
+ **Note:** WordPress adds more CSS classes to the avatar not listed here.
200
+
201
+ 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.
202
+
203
+ * Image File: wp-user-avatar-file
204
+ * Attachment: wp-user-avatar-attachment
205
+ * Custom URL: wp-user-avatar-custom
206
+
207
+ `[avatar user="admin" size="96" align="left" link="attachment"]`
208
+
209
+ Outputs:
210
+
211
+ `<a href="{attachmentURL}" class="wp-user-avatar-link wp-user-avatar-attachment">
212
+ <img src="{imageURL}" width="96" height="96" class="wp-user-avatar wp-user-avatar-96 alignleft" />
213
+ </a>`
214
+
215
+ = What other functions are available for WP User Avatar? =
216
+ * <code>get_wp_user_avatar_src</code>: retrieves just the image URL
217
+ * <code>has_wp_user_avatar</code>: checks if the user has a WP User Avatar image
218
+ * [See example usage here](http://wordpress.org/extend/plugins/wp-user-avatar/installation/)
219
+
220
+ == Screenshots ==
221
+
222
+ 1. WP User Avatar lets you upload your own Default Avatar.
223
+ 2. WP User Avatar adds a field to your edit profile page.
224
+ 3. After you've chosen a WP User Avatar image, you will see the option to remove it.
225
+ 4. WP User Avatar adds a button to insert the [avatar] shortcode in the Visual Editor.
226
+ 5. Options for the [avatar] shortcode.
227
+
228
+ == Changelog ==
229
+
230
+ = 1.3.6 =
231
+ * Add: Target for link in shortcode
232
+ * Update: Clean up code and add more comments
233
+
234
+ = 1.3.5 =
235
+ * Bug Fix: Swap TinyMCE file locations
236
+
237
+ = 1.3.4 =
238
+ * Update: Change support only to WP 3.3+ because of jQuery 1.7.1 support
239
+
240
+ = 1.3.3 =
241
+ * Update: Shortcode checks for user ID, login, slug, or e-mail address
242
+ * Update: Move jquery to register_script for < WP 3.5
243
+
244
+ = 1.3.2 =
245
+ * Bug Fix: Check for user before setting name in alt tag
246
+ * Update: readme.txt
247
+
248
+ = 1.3.1 =
249
+ * Bug Fix: Rename usermeta only if found
250
+
251
+ = 1.3 =
252
+ * Add: Multisite support
253
+ * Bug Fix: Warnings if no user found
254
+ * Update: Enable action_show_user_profile for any class using show_user_profile hook
255
+
256
+ = 1.2.6 =
257
+ * Bug Fix: options-discussion.php page doesn't show default avatars
258
+
259
+ = 1.2.5 =
260
+ * Bug Fix: Comment author showing wrong avatar
261
+ * Bug Fix: Avatar adds fixed dimensions when non-numeric size is used
262
+ * Update: Use local image for default avatar instead of calling image from Gravatar
263
+
264
+ = 1.2.4 =
265
+ * Bug Fix: Show default avatar when user removes custom avatar
266
+ * Bug Fix: Default Avatar save setting
267
+
268
+ = 1.2.3 =
269
+ * Bug Fix: Show default avatar when user removes custom avatar
270
+ * Bug Fix: Default Avatar save setting
271
+
272
+ = 1.2.2 =
273
+ * Add: Ability for bbPress users to edit avatar on front profile page
274
+ * Add: Link options for shortcode
275
+ * Bug Fix: Show WP User Avatar only to users with upload_files capability
276
+
277
+ = 1.2.1 =
278
+ * Add: TinyMCE button
279
+ * Update: Clean up redundant code
280
+ * Update: Compatibility only back to WordPress 3.3
281
+
282
+ = 1.2 =
283
+ * Add: Default Avatar setting
284
+
285
+ = 1.1.7 =
286
+ * Bug Fix: Change update_usermeta to update_user_meta
287
+
288
+ = 1.1.6 =
289
+ * Bug Fix: Image not showing in user profile edit
290
+
291
+ = 1.1.5a =
292
+ * Update: readme.txt
293
+
294
+ = 1.1.5 =
295
+ * Bug Fix: Remove stray curly bracket
296
+
297
+ = 1.1.4 =
298
+ * Bug Fix: Change get_usermeta to get_user_meta
299
+ * Bug Fix: Non-object warning when retrieving user ID
300
+
301
+ = 1.1.3 =
302
+ * Bug Fix: Comment author with no e-mail address
303
+
304
+ = 1.1.2 =
305
+ * Remove: Unused variables
306
+
307
+ = 1.1.1 =
308
+ * Bug Fix: Capabilities error in comment avatar
309
+
310
+ = 1.1 =
311
+ * Add: Add filter for get_avatar
312
+ * Add: CSS alignment classes
313
+ * Add: Replace comment author avatar
314
+ * Add: Shortcode
315
+ * Update: readme.txt
316
+
317
+ = 1.0.2 =
318
+ * Update: FAQ
319
+ * Remove: CSS that hides "Insert into Post"
320
+
321
+ = 1.0.1 =
322
+ * Add: CSS classes to image output
323
+
324
+ = 1.0 =
325
+ * Initial release
326
+
327
+ == Upgrade Notice ==
328
+
329
+ = 1.3 =
330
+ * New Feature: Multisite support
331
+
332
+ = 1.2.2 =
333
+ * New Features: Link options for shortcode, bbPress integration
334
+
335
+ = 1.2.1 =
336
+ * New Feature: Shortcode insertion button for Visual Editor
337
+
338
+ = 1.2 =
339
+ * New Feature: Default Avatar customization
340
+
341
+ = 1.1 =
342
+ * New Features: [avatar] shortcode, direct replacement of get_avatar() and comment author avatar, more CSS classes
wp-user-avatar.php ADDED
@@ -0,0 +1,552 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package WP User Avatar
4
+ * @version 1.3.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. Add your own Default Avatar.
10
+ Version: 1.3.6
11
+ Author: Bangbay Siboliban
12
+ Author URI: http://siboliban.org/
13
+ */
14
+
15
+ // Define paths and variables
16
+ define('WP_USER_AVATAR_VERSION', '1.3.6');
17
+ define('WP_USER_AVATAR_FOLDER', basename(dirname(__FILE__)));
18
+ define('WP_USER_AVATAR_ABSPATH', trailingslashit(str_replace('\\','/', WP_PLUGIN_DIR.'/'.WP_USER_AVATAR_FOLDER)));
19
+ define('WP_USER_AVATAR_URLPATH', trailingslashit(plugins_url(WP_USER_AVATAR_FOLDER)));
20
+
21
+ // Define global variables
22
+ $avatar_default = get_option('avatar_default');
23
+ $avatar_default_wp_user_avatar = get_option('avatar_default_wp_user_avatar');
24
+ $show_avatars = get_option('show_avatars');
25
+ $mustache_original = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar.png';
26
+ $mustache_medium = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-300x300.png';
27
+ $mustache_thumbnail = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-150x150.png';
28
+ $mustache_avatar = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-96x96.png';
29
+ $mustache_admin = WP_USER_AVATAR_URLPATH.'images/wp-user-avatar-32x32.png';
30
+ $ssl = is_ssl() ? 's' : '';
31
+
32
+ // Load add-ons
33
+ include_once(WP_USER_AVATAR_ABSPATH.'includes/tinymce.php');
34
+
35
+ // Initialize default settings
36
+ register_activation_hook(__FILE__, 'wp_user_avatar_options');
37
+
38
+ // Remove user metadata and options on plugin delete
39
+ register_uninstall_hook(__FILE__, 'wp_user_avatar_delete_setup');
40
+
41
+ // Settings saved to wp_options
42
+ function wp_user_avatar_options(){
43
+ add_option('avatar_default_wp_user_avatar','');
44
+ }
45
+ add_action('init', 'wp_user_avatar_options');
46
+
47
+ // Update default avatar to new format
48
+ function wp_user_avatar_default_avatar(){
49
+ global $avatar_default, $avatar_default_wp_user_avatar, $mustache_original;
50
+ // If default avatar is the old mustache URL, update it
51
+ if($avatar_default == $mustache_original){
52
+ update_option('avatar_default', 'wp_user_avatar');
53
+ }
54
+ // If user had an image URL as the default avatar, replace with ID instead
55
+ if(!empty($avatar_default_wp_user_avatar)){
56
+ $avatar_default_wp_user_avatar_image = wp_get_attachment_image_src($avatar_default_wp_user_avatar, 'medium');
57
+ if($avatar_default == $avatar_default_wp_user_avatar_image[0]){
58
+ update_option('avatar_default', 'wp_user_avatar');
59
+ }
60
+ }
61
+ }
62
+ add_action('init', 'wp_user_avatar_default_avatar');
63
+
64
+ // Rename user meta to match database settings
65
+ function wp_user_avatar_user_meta(){
66
+ global $wpdb, $blog_id;
67
+ $wp_user_avatar_metakey = $wpdb->get_blog_prefix($blog_id).'user_avatar';
68
+ // If database tables start with something other than wp_
69
+ if($wp_user_avatar_metakey != 'wp_user_avatar'){
70
+ $users = get_users();
71
+ // Move current user metakeys to new metakeys
72
+ foreach($users as $user){
73
+ $wp_user_avatar = get_user_meta($user->ID, 'wp_user_avatar', true);
74
+ if(!empty($wp_user_avatar)){
75
+ update_user_meta($user->ID, $wpdb->get_blog_prefix($blog_id).'user_avatar', $wp_user_avatar);
76
+ delete_user_meta($user->ID, 'wp_user_avatar');
77
+ }
78
+ }
79
+ }
80
+ }
81
+ add_action('init', 'wp_user_avatar_user_meta');
82
+
83
+ // Remove user metadata and options on plugin delete
84
+ function wp_user_avatar_delete_setup(){
85
+ global $wpdb, $blog_id, $switched;
86
+ $users = get_users();
87
+ // Remove settings for all sites in multisite
88
+ if(is_multisite()){
89
+ $blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_blogs"));
90
+ foreach($users as $user){
91
+ foreach($blogs as $blog){
92
+ delete_user_meta($user->ID, $wpdb->get_blog_prefix($blog->blog_id).'user_avatar');
93
+ }
94
+ }
95
+ foreach($blogs as $blog){
96
+ switch_to_blog($blog->blog_id);
97
+ delete_option('avatar_default_wp_user_avatar');
98
+ }
99
+ } else {
100
+ foreach($users as $user){
101
+ delete_user_meta($user->ID, $wpdb->get_blog_prefix($blog_id).'user_avatar');
102
+ }
103
+ delete_option('avatar_default_wp_user_avatar');
104
+ }
105
+ // Reset all default avatars to Mystery Man
106
+ update_option('avatar_default', 'mystery');
107
+ }
108
+
109
+ // WP User Avatar
110
+ if(!class_exists('wp_user_avatar')){
111
+ class wp_user_avatar{
112
+ function wp_user_avatar(){
113
+ global $current_user, $show_avatars;
114
+ // Only works if user can upload files
115
+ if(current_user_can('upload_files')){
116
+ // Adds WPUA to profile
117
+ add_action('show_user_profile', array('wp_user_avatar','action_show_user_profile'));
118
+ add_action('edit_user_profile', array($this,'action_show_user_profile'));
119
+ add_action('personal_options_update', array($this,'action_process_option_update'));
120
+ add_action('edit_user_profile_update', array($this,'action_process_option_update'));
121
+ // Adds WPUA to Discussion settings
122
+ add_action('discussion_update', array($this,'action_process_option_update'));
123
+ // Adds scripts to admin
124
+ add_action('admin_enqueue_scripts', array($this, 'media_upload_scripts'));
125
+ // Adds scripts to front pages
126
+ add_action('wp_enqueue_scripts', array($this, 'media_upload_scripts'));
127
+ }
128
+ // Only add attachment field for WP 3.4 and older
129
+ if(!function_exists('wp_enqueue_media')){
130
+ add_filter('attachment_fields_to_edit', array($this, 'add_wp_user_avatar_attachment_field_to_edit'), 10, 2);
131
+ }
132
+ // Hide column in Users table if default avatars are enabled
133
+ if($show_avatars != '1'){
134
+ add_filter('manage_users_columns', array($this, 'add_wp_user_avatar_column'), 10, 1);
135
+ add_filter('manage_users_custom_column', array($this, 'show_wp_user_avatar_column'), 10, 3);
136
+ }
137
+ }
138
+
139
+ // Add to edit user profile
140
+ function action_show_user_profile($user){
141
+ global $wpdb, $blog_id, $current_user, $show_avatars;
142
+ // Get WPUA attachment ID
143
+ $wp_user_avatar = get_user_meta($user->ID, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
144
+ // Show remove button if WPUA is set
145
+ $hide_notice = has_wp_user_avatar($user->ID) ? ' class="hide-me"' : '';
146
+ $hide_remove = !has_wp_user_avatar($user->ID) ? ' hide-me' : '';
147
+ // If avatars are enabled, get original avatar image or show blank
148
+ $avatar_medium_src = ($show_avatars == '1') ? get_avatar_original($user->user_email, 96) : includes_url().'images/blank.gif';
149
+ // Check if user has wp_user_avatar, if not show image from above
150
+ $avatar_medium = has_wp_user_avatar($user->ID) ? get_wp_user_avatar_src($user->ID, 'medium') : $avatar_medium_src;
151
+ // Change text on message based on current user
152
+ $profile = ($current_user->ID == $user->ID) ? 'Profile' : 'User';
153
+ ?>
154
+ <?php if(class_exists('bbPress') && !is_admin()) : // Add to bbPress profile with same style ?>
155
+ <h2 class="entry-title"><?php _e('WP User Avatar'); ?></h2>
156
+ <fieldset class="bbp-form">
157
+ <legend><?php _e('WP User Avatar'); ?></legend>
158
+ <?php else : // Add to profile with admin style ?>
159
+ <h3><?php _e('WP User Avatar') ?></h3>
160
+ <table class="form-table">
161
+ <tr>
162
+ <th><label for="wp_user_avatar"><?php _e('WP User Avatar'); ?></label></th>
163
+ <td>
164
+ <?php endif; ?>
165
+ <input type="hidden" name="wp-user-avatar" id="wp-user-avatar" value="<?php echo $wp_user_avatar; ?>" />
166
+ <p><button type="button" class="button" id="add-wp-user-avatar"><?php _e('Edit WP User Avatar'); ?></button></p>
167
+ <p id="wp-user-avatar-preview"><?php echo '<img src="'.$avatar_medium.'" alt="" />'; ?></p>
168
+ <?php if($show_avatars == '1') : ?>
169
+ <p id="wp-user-avatar-notice"<?php echo $hide_notice; ?>><?php _e('This is your default avatar.'); ?></p>
170
+ <?php endif; ?>
171
+ <p><button type="button" class="button<?php echo $hide_remove; ?>" id="remove-wp-user-avatar"><?php _e('Remove'); ?></button></p>
172
+ <p id="wp-user-avatar-message"><?php _e('Press "Update '.$profile.'" to save your changes.'); ?></p>
173
+ <?php if(class_exists('bbPress') && !is_admin()) : // Add to bbPress profile with same style ?>
174
+ </fieldset>
175
+ <?php else : // Add to profile with admin style ?>
176
+ </td>
177
+ </tr>
178
+ </table>
179
+ <?php endif; ?>
180
+ <?php
181
+ // Add JS
182
+ echo edit_default_wp_user_avatar($user->display_name, $avatar_medium_src);
183
+ }
184
+
185
+ // Update user meta
186
+ function action_process_option_update($user_id){
187
+ global $wpdb, $blog_id;
188
+ update_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', (isset($_POST['wp-user-avatar']) ? intval($_POST['wp-user-avatar']) : ''));
189
+ }
190
+
191
+ // Add button to attach image for WP 3.4 and older
192
+ function add_wp_user_avatar_attachment_field_to_edit($fields, $post){
193
+ $image = wp_get_attachment_image_src($post->ID, "medium");
194
+ $button = '<button type="button" class="button" id="set-wp-user-avatar-image" onclick="setWPUserAvatar(\''.$post->ID.'\', \''.$image[0].'\')">Set WP User Avatar</button>';
195
+ $fields['wp-user-avatar'] = array(
196
+ 'label' => __('WP User Avatar'),
197
+ 'input' => 'html',
198
+ 'html' => $button
199
+ );
200
+ return $fields;
201
+ }
202
+
203
+ // Add column to Users table
204
+ function add_wp_user_avatar_column($columns){
205
+ return $columns + array('wp-user-avatar' => __('WP User Avatar'));
206
+ }
207
+
208
+ // Show thumbnail in Users table
209
+ function show_wp_user_avatar_column($value, $column_name, $user_id){
210
+ global $wpdb, $blog_id;
211
+ $wp_user_avatar = get_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
212
+ $wp_user_avatar_image = wp_get_attachment_image($wp_user_avatar, array(32,32));
213
+ if($column_name == 'wp-user-avatar'){
214
+ return $wp_user_avatar_image;
215
+ }
216
+ }
217
+
218
+ // Media uploader
219
+ function media_upload_scripts(){
220
+ if(function_exists('wp_enqueue_media')){
221
+ wp_enqueue_media();
222
+ } else {
223
+ wp_enqueue_script('media-upload');
224
+ wp_enqueue_script('thickbox');
225
+ wp_enqueue_style('thickbox');
226
+ }
227
+ wp_enqueue_script('wp-user-avatar', WP_USER_AVATAR_URLPATH.'js/wp-user-avatar.js', '', WP_USER_AVATAR_VERSION);
228
+ wp_enqueue_style('wp-user-avatar', WP_USER_AVATAR_URLPATH.'css/wp-user-avatar.css', '', WP_USER_AVATAR_VERSION);
229
+ }
230
+ }
231
+
232
+ // Uploader scripts
233
+ function edit_default_wp_user_avatar($section, $avatar_thumb){ ?>
234
+ <script type="text/javascript">
235
+ jQuery(function(){
236
+ <?php if(function_exists('wp_enqueue_media')) : // Backbone uploader for WP 3.5+ ?>
237
+ openMediaUploader("<?php echo $section; ?>");
238
+ <?php else : // Fall back to Thickbox uploader ?>
239
+ openThickboxUploader("<?php echo $section; ?>", "<?php echo get_admin_url(); ?>media-upload.php?post_id=0&type=image&tab=library&TB_iframe=1");
240
+ <?php endif; ?>
241
+ removeWPUserAvatar("<?php echo htmlspecialchars_decode($avatar_thumb); ?>");
242
+ });
243
+ </script>
244
+ <?php
245
+ }
246
+
247
+ // Returns true if user has Gravatar-hosted image
248
+ function has_gravatar($id_or_email, $has_gravatar=false, $user='', $email=''){
249
+ global $ssl;
250
+ if(!is_object($id_or_email) && !empty($id_or_email)){
251
+ // Find user by ID or e-mail address
252
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
253
+ // Get registered user e-mail address from profile, otherwise e-mail address should be value
254
+ $email = !empty($user) ? $user->user_email : '';
255
+ }
256
+ // Check if Gravatar image returns 200 (OK) or 404 (Not Found)
257
+ if(!empty($email)){
258
+ $hash = md5(strtolower(trim($email)));
259
+ $gravatar = 'http'.$ssl.'://www.gravatar.com/avatar/'.$hash.'?d=404';
260
+ $headers = @get_headers($gravatar);
261
+ $has_gravatar = !preg_match("|200|", $headers[0]) ? false : true;
262
+ }
263
+ return $has_gravatar;
264
+ }
265
+
266
+ // Returns true if user has wp_user_avatar
267
+ function has_wp_user_avatar($id_or_email='', $has_wp_user_avatar=false, $user='', $user_id=''){
268
+ global $wpdb, $blog_id;
269
+ if(!is_object($id_or_email) && !empty($id_or_email)){
270
+ // Find user by ID or e-mail address
271
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
272
+ // Get registered user e-mail address from profile, otherwise e-mail address should be value
273
+ $user_id = !empty($user) ? $user->ID : '';
274
+ }
275
+ $wp_user_avatar = get_user_meta($user_id, $wpdb->get_blog_prefix($blog_id).'user_avatar', true);
276
+ $has_wp_user_avatar = !empty($wp_user_avatar) ? true : false;
277
+ return $has_wp_user_avatar;
278
+ }
279
+
280
+ // Replace get_avatar only in get_wp_user_avatar
281
+ function get_avatar_filter($avatar, $id_or_email, $size='', $default='', $alt=''){
282
+ global $post, $comment, $avatar_default, $avatar_default_wp_user_avatar, $mustache_original, $mustache_medium, $mustache_thumbnail, $mustache_avatar, $mustache_admin;
283
+ // User has WPUA
284
+ if(is_object($id_or_email)){
285
+ if(!empty($comment->comment_author_email)){
286
+ $avatar = get_wp_user_avatar($comment->comment_author_email, $size, $default, $alt);
287
+ } else {
288
+ $avatar = get_wp_user_avatar('unknown@gravatar.com', $size, $default, $alt);
289
+ }
290
+ } else {
291
+ if(has_wp_user_avatar($id_or_email)){
292
+ $avatar = get_wp_user_avatar($id_or_email, $size, $default, $alt);
293
+ // User has Gravatar
294
+ } elseif(has_gravatar($id_or_email)){
295
+ $avatar = $avatar;
296
+ // User doesn't have WPUA or Gravatar and Default Avatar is wp_user_avatar, show custom Default Avatar
297
+ } elseif($avatar_default == 'wp_user_avatar'){
298
+ // Show custom Default Avatar
299
+ if(!empty($avatar_default_wp_user_avatar)){
300
+ // Get image
301
+ $avatar_default_wp_user_avatar_image = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array($size,$size));
302
+ // Image src
303
+ $default = $avatar_default_wp_user_avatar_image[0];
304
+ // Add dimensions if numeric size
305
+ $dimensions = ' width="'.$avatar_default_wp_user_avatar_image[1].'" height="'.$avatar_default_wp_user_avatar_image[2].'"';
306
+ } else {
307
+ // Get mustache image based on numeric size comparison
308
+ if($size > get_option('medium_size_w')){
309
+ $default = $mustache_original;
310
+ } elseif($size <= get_option('medium_size_w') && $size > get_option('thumbnail_size_w')){
311
+ $default = $mustache_medium;
312
+ } elseif($size <= get_option('thumbnail_size_w') && $size > 96){
313
+ $default = $mustache_thumbnail;
314
+ } elseif($size <= 96 && $size > 32){
315
+ $default = $mustache_avatar;
316
+ } elseif($size <= 32){
317
+ $default = $mustache_admin;
318
+ }
319
+ // Add dimensions if numeric size
320
+ $dimensions = ' width="'.$size.'" height="'.$size.'"';
321
+ $defaultcss = ' avatar-default';
322
+ }
323
+ // Construct the img tag
324
+ $avatar = "<img src='".$default."'".$dimensions." alt='".$alt."' class='wp-user-avatar wp-user-avatar-".$size." avatar avatar-".$size." photo'".$defaultcss." />";
325
+ }
326
+ }
327
+ return $avatar;
328
+ }
329
+ add_filter('get_avatar', 'get_avatar_filter', 10, 6);
330
+
331
+ // Get original avatar, for when user removes wp_user_avatar
332
+ function get_avatar_original($id_or_email, $size='', $default='', $alt=''){
333
+ global $avatar_default, $avatar_default_wp_user_avatar, $mustache_avatar;
334
+ // Remove get_avatar filter
335
+ remove_filter('get_avatar', 'get_avatar_filter');
336
+ // User doesn't Gravatar and Default Avatar is wp_user_avatar, show custom Default Avatar
337
+ if(!has_gravatar($id_or_email) && $avatar_default == 'wp_user_avatar'){
338
+ // Show custom Default Avatar
339
+ if(!empty($avatar_default_wp_user_avatar)){
340
+ $avatar_default_wp_user_avatar_image = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array($size,$size));
341
+ $default = $avatar_default_wp_user_avatar_image[0];
342
+ } else {
343
+ $default = $mustache_avatar;
344
+ }
345
+ } else {
346
+ // Get image from Gravatar, whether it's the user's image or default image
347
+ $wp_user_avatar_image = get_avatar($id_or_email);
348
+ // Takes the img tag, extracts the src
349
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wp_user_avatar_image, $matches, PREG_SET_ORDER);
350
+ $default = $matches [0] [1];
351
+ }
352
+ return $default;
353
+ }
354
+
355
+ // Find WPUA, show get_avatar if empty
356
+ function get_wp_user_avatar($id_or_email='', $size='96', $align='', $alt=''){
357
+ global $post, $comment, $avatar_default, $wpdb, $blog_id;
358
+ // Checks if comment
359
+ if(is_object($id_or_email)){
360
+ // Checks if comment author is registered user by user ID
361
+ if($comment->user_id != '0'){
362
+ $id_or_email = $comment->user_id;
363
+ $user = get_user_by('id', $id_or_email);
364
+ // Checks that comment author isn't anonymous
365
+ } elseif(!empty($comment->comment_author_email)){
366
+ // Checks if comment author is registered user by e-mail address
367
+ $user = get_user_by('email', $comment->comment_author_email);
368
+ // Get registered user info from profile, otherwise e-mail address should be value
369
+ $id_or_email = !empty($user) ? $user->ID : $comment->comment_author_email;
370
+ }
371
+ $alt = $comment->comment_author;
372
+ } else {
373
+ if(!empty($id_or_email)){
374
+ // Find user by ID or e-mail address
375
+ $user = is_numeric($id_or_email) ? get_user_by('id', $id_or_email) : get_user_by('email', $id_or_email);
376
+ } else {
377
+ // Find author's name if id_or_email is empty
378
+ $author_name = get_query_var('author_name');
379
+ if(is_author()){
380
+ // On author page, get user by page slug
381
+ $user = get_user_by('slug', $author_name);
382
+ } else {
383
+ // On post, get user by author meta
384
+ $user_id = get_the_author_meta('ID');
385
+ $user = get_user_by('id', $user_id);
386
+ }
387
+ }
388
+ // Set user's ID and name
389
+ if(!empty($user)){
390
+ $id_or_email = $user->ID;
391
+ $alt = $user->display_name;
392
+ }
393
+ }
394
+ // Checks if user has WPUA
395
+ $wp_user_avatar_meta = !empty($id_or_email) ? get_the_author_meta($wpdb->get_blog_prefix($blog_id).'user_avatar', $id_or_email) : '';
396
+ // Add alignment class
397
+ $alignclass = !empty($align) ? ' align'.$align : '';
398
+ // User has WPUA, bypass get_avatar
399
+ if(!empty($wp_user_avatar_meta)){
400
+ // Numeric size use size array
401
+ $get_size = is_numeric($size) ? array($size,$size) : $size;
402
+ // Get image src
403
+ $wp_user_avatar_image = wp_get_attachment_image_src($wp_user_avatar_meta, $get_size);
404
+ // Add dimensions to img only if numeric size was specified
405
+ $dimensions = is_numeric($size) ? ' width="'.$wp_user_avatar_image[1].'" height="'.$wp_user_avatar_image[2].'"' : '';
406
+ // Construct the img tag
407
+ $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" />';
408
+ } else {
409
+ // Get numeric sizes for non-numeric sizes based on media options
410
+ if($size == 'original' || $size == 'large' || $size == 'medium' || $size == 'thumbnail'){
411
+ $get_size = ($size == 'original') ? get_option('large_size_w') : get_option($size.'_size_w');
412
+ } else {
413
+ // Numeric sizes leave as-is
414
+ $get_size = $size;
415
+ }
416
+ // User with no WPUA uses get_avatar
417
+ $avatar = get_avatar($id_or_email, $get_size, $default='', $alt='');
418
+ // Remove width and height for non-numeric sizes
419
+ if(!is_numeric($size)){
420
+ $avatar = preg_replace("/(width|height)=\'\d*\'\s/", '', $avatar);
421
+ $avatar = preg_replace('/(width|height)=\"\d*\"\s/', '', $avatar);
422
+ $avatar = str_replace('wp-user-avatar wp-user-avatar-'.$get_size.' ', '', $avatar);
423
+ $avatar = str_replace("class='", "class='wp-user-avatar wp-user-avatar-".$size.$alignclass." ", $avatar);
424
+ }
425
+ }
426
+ return $avatar;
427
+ }
428
+
429
+ // Return just the image src
430
+ function get_wp_user_avatar_src($id_or_email, $size='', $align=''){
431
+ $wp_user_avatar_image_src = '';
432
+ // Gets the avatar img tag
433
+ $wp_user_avatar_image = get_wp_user_avatar($id_or_email, $size, $align);
434
+ // Takes the img tag, extracts the src
435
+ if(!empty($wp_user_avatar_image)){
436
+ $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $wp_user_avatar_image, $matches, PREG_SET_ORDER);
437
+ $wp_user_avatar_image_src = $matches [0] [1];
438
+ }
439
+ return $wp_user_avatar_image_src;
440
+ }
441
+
442
+ // Shortcode
443
+ function wp_user_avatar_shortcode($atts, $content){
444
+ global $wpdb, $blog_id;
445
+ // Set shortcode attributes
446
+ extract(shortcode_atts(array('user' => '', 'size' => '96', 'align' => '', 'link' => '', 'target' => ''), $atts));
447
+ // Find user by ID, login, slug, or e-mail address
448
+ if(!empty($user)){
449
+ $user = is_numeric($user) ? get_user_by('id', $user) : get_user_by('login', $user);
450
+ $user = empty($user) ? get_user_by('slug', $user) : $user;
451
+ $user = empty($user) ? get_user_by('email', $user) : $user;
452
+ }
453
+ // Get user ID
454
+ $id_or_email = !empty($user) ? $user->ID : '';
455
+ // Check if link is set
456
+ if(!empty($link)){
457
+ // CSS class is same as link type, except for URL
458
+ $link_class = $link;
459
+ // Open in new window
460
+ $target_link = !empty($target) ? ' target="'.$target.'"' : '';
461
+ if($link == 'file'){
462
+ // Get image src
463
+ $image_link = get_wp_user_avatar_src($id_or_email, 'original', $align);
464
+ } elseif($link == 'attachment'){
465
+ // Get attachmennt URL
466
+ $image_link = get_attachment_link(get_the_author_meta($wpdb->get_blog_prefix($blog_id).'user_avatar', $id_or_email));
467
+ } else {
468
+ // URL
469
+ $image_link = $link;
470
+ $link_class = 'custom';
471
+ }
472
+ // Wrap the avatar inside the link
473
+ $avatar = '<a href="'.$image_link.'" class="wp-user-avatar-link wp-user-avatar-'.$link_class.'"'.$target_link.'>'.get_wp_user_avatar($id_or_email, $size, $align).'</a>';
474
+ } else {
475
+ // Get WPUA as normal
476
+ $avatar = get_wp_user_avatar($id_or_email, $size, $align);
477
+ }
478
+ return $avatar;
479
+ }
480
+ add_shortcode('avatar','wp_user_avatar_shortcode');
481
+
482
+ // Add default avatar
483
+ function add_default_wp_user_avatar($avatar_list){
484
+ global $avatar_default, $avatar_default_wp_user_avatar, $mustache_medium, $mustache_admin;
485
+ // Remove get_avatar filter
486
+ remove_filter('get_avatar', 'get_avatar_filter');
487
+ // Set avatar_list variable
488
+ $avatar_list = '';
489
+ // Set avatar defaults
490
+ $avatar_defaults = array(
491
+ 'mystery' => __('Mystery Man'),
492
+ 'blank' => __('Blank'),
493
+ 'gravatar_default' => __('Gravatar Logo'),
494
+ 'identicon' => __('Identicon (Generated)'),
495
+ 'wavatar' => __('Wavatar (Generated)'),
496
+ 'monsterid' => __('MonsterID (Generated)'),
497
+ 'retro' => __('Retro (Generated)')
498
+ );
499
+ // No Default Avatar, set to Mystery Man
500
+ if(empty($avatar_default)){
501
+ $avatar_default = 'mystery';
502
+ }
503
+ // Take avatar_defaults and get examples for unknown@gravatar.com
504
+ foreach($avatar_defaults as $default_key => $default_name){
505
+ $avatar = get_avatar('unknown@gravatar.com', 32, $default_key);
506
+ $selected = ($avatar_default == $default_key) ? 'checked="checked" ' : '';
507
+ $avatar_list .= "\n\t<label><input type='radio' name='avatar_default' id='avatar_{$default_key}' value='".esc_attr($default_key)."' {$selected}/> ";
508
+ $avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1&amp;forcedefault=1'", $avatar);
509
+ $avatar_list .= ' '.$default_name.'</label>';
510
+ $avatar_list .= '<br />';
511
+ }
512
+ // Show remove link if custom Default Avatar is set
513
+ if(!empty($avatar_default_wp_user_avatar)){
514
+ $avatar_thumb_src = wp_get_attachment_image_src($avatar_default_wp_user_avatar, array(32,32));
515
+ $avatar_thumb = $avatar_thumb_src[0];
516
+ $hide_remove = '';
517
+ } else {
518
+ $avatar_thumb = $mustache_admin;
519
+ $hide_remove = ' class="hide-me"';
520
+ }
521
+ // Default Avatar is wp_user_avatar, check the radio button next to it
522
+ $selected_avatar = ($avatar_default == 'wp_user_avatar') ? ' checked="checked" ' : '';
523
+ // Wrap WPUA in div
524
+ $avatar_thumb_img = '<div id="wp-user-avatar-preview"><img src="'.$avatar_thumb.'" width="32" /></div>';
525
+ // Add WPUA to list
526
+ $wp_user_avatar_list = "\n\t<label><input type='radio' name='avatar_default' id='wp_user_avatar_radio' value='wp_user_avatar'$selected_avatar /> ";
527
+ $wp_user_avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1'", $avatar_thumb_img);
528
+ $wp_user_avatar_list .= ' '.__('WP User Avatar').'</label>';
529
+ $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>';
530
+ $wp_user_avatar_list .= '<a href="#" id="remove-wp-user-avatar"'.$hide_remove.'>'.__('Remove').'</a></p>';
531
+ $wp_user_avatar_list .= '<input type="hidden" id="wp-user-avatar" name="avatar_default_wp_user_avatar" value="'.$avatar_default_wp_user_avatar.'">';
532
+ $wp_user_avatar_list .= '<p id="wp-user-avatar-message">'.__('Press "Save Changes" to save your changes.').'</p>';
533
+ $wp_user_avatar_list .= edit_default_wp_user_avatar('Default Avatar', $mustache_admin);
534
+ return $wp_user_avatar_list.$avatar_list;
535
+ }
536
+ add_filter('default_avatar_select', 'add_default_wp_user_avatar', 10);
537
+
538
+ // Add default avatar_default to whitelist
539
+ function wp_user_avatar_whitelist_options($whitelist_options){
540
+ $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');
541
+ return $whitelist_options;
542
+ }
543
+ add_filter('whitelist_options', 'wp_user_avatar_whitelist_options', 10);
544
+
545
+ // Initialize WPUA after other plugins are loaded
546
+ function wp_user_avatar_load(){
547
+ global $wp_user_avatar_instance;
548
+ $wp_user_avatar_instance = new wp_user_avatar();
549
+ }
550
+ add_action('plugins_loaded','wp_user_avatar_load');
551
+ }
552
+ ?>