<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>digitalnature &#187; avatars</title>
	<atom:link href="https://digitalnature.eu/blog/tag/avatars/feed/" rel="self" type="application/rss+xml" />
	<link>https://digitalnature.eu</link>
	<description>Rise Above the Ordinary</description>
	<lastBuildDate>Fri, 11 Sep 2015 15:02:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>Custom avatar images in WordPress</title>
		<link>https://digitalnature.eu/blog/2011/09/18/custom-avatar-images-in-wordpress/</link>
		<comments>https://digitalnature.eu/blog/2011/09/18/custom-avatar-images-in-wordpress/#comments</comments>
		<pubDate>Sun, 18 Sep 2011 23:28:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Useless Stuff]]></category>
		<category><![CDATA[avatars]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://digitalnature.eu/?p=1870</guid>
		<description><![CDATA[This is a short walktrough on how to let your website users upload their own avatars, without using a plugin.
First, define the maximum image sizes (note that we will only downsize, smaller images will get browser-resized):

define('MAX_AVATAR_WIDTH', 96);
define('MAX_AVATAR_HEIGHT', 96);

This  [...]]]></description>
			<content:encoded><![CDATA[<p>This is a short walktrough on how to let your website users upload their own avatars, without using a plugin.</p>
<p>First, define the maximum image sizes (note that we will only downsize, smaller images will get browser-resized):</p>
<pre class="php">
define('MAX_AVATAR_WIDTH', 96);
define('MAX_AVATAR_HEIGHT', 96);
</pre>
<p>This goes in the child theme&#8217;s functions-user.php file.</p>
<p>Next, create the form which lets users upload the image. I chose the author.php template here, but you can easily add it inside the dashboard trough some hooks.</p>
<pre class="php">
&lt;?php if(is_user_logged_in() &amp;&amp; get_current_user_id() === (int)get_query_var('author')): ?&gt;
&lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot; action=&quot;&quot;&gt;
  &lt;input type=&quot;file&quot; name=&quot;user_avatar&quot; /&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;wp_handle_upload&quot; /&gt;
  &lt;input type=&quot;submit&quot; value=&quot;Upload new Avatar&quot; /&gt;
&lt;/form&gt;
&lt;?php endif; ?&gt;
</pre>
<p>The actual upload processing. You can have this in the same author template before get_header(), or inside the child theme&#8217;s functions:</p>
<pre class="php">
if(is_user_logged_in() &amp;&amp; isset($_FILES['user_avatar'])){

  // we need this for the wp_handle_upload function
  require_once ABSPATH.'wp-admin/includes/file.php';

  // ID of the current user
  $current_user_id = get_current_user_id();

  // just be aware that GIFs are annoying as fuck
  $allowed_image_types = array(
    'jpg|jpeg|jpe' =&gt; 'image/jpeg',
    'png'          =&gt; 'image/png',
    'gif'          =&gt; 'image/gif',
  );

  // let wp do the upload checks, file moving etc.
  $status = wp_handle_upload($_FILES['user_avatar'], array('mimes' =&gt; $allowed_image_types));

  // no errors? Get the uploaded file path and resize it
  if(empty($status['error'])){

    // resize it
    $resized = image_resize($status['file'], MAX_AVATAR_WIDTH, MAX_AVATAR_HEIGHT, $crop = true);

    // resize failed, display the reason
    if(is_wp_error($resized))
      wp_die($resized-&gt;get_error_message());

    // determine the resized file URL
    $uploads = wp_upload_dir();
    $resized_url = $uploads['url'].'/'.basename($resized);

    // insert the file URL into the current user meta
    update_user_meta($current_user_id, 'custom_avatar', $resized_url);

  // error, show it
  }else{
    wp_die(sprintf(__('Upload Error: %s'), $status['error']));

  }

}
</pre>
<p>Finally, hook into the avatar display function and get the avatar user meta field:</p>
<pre class="php">
add_filter('get_avatar', 'custom_avatars', 10, 3);

function custom_avatars($avatar, $id_or_email, $size){
  if(is_user_logged_in()){
    $current_user = wp_get_current_user();
    $image_url = get_user_meta($current_user-&gt;ID, 'custom_avatar', true);
    if($user_avatar !== false)
      return '&lt;img src=&quot;'.$image_url.'&quot; class=&quot;avatar photo&quot; width=&quot;'.$size.'&quot; height=&quot;'.$size.'&quot; alt=&quot;'.$current_user-&gt;display_name .'&quot; /&gt;';
  }

  return $avatar;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>https://digitalnature.eu/blog/2011/09/18/custom-avatar-images-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>83</slash:comments>
		</item>
	</channel>
</rss>
