Skip to content
Franz Josef Kaiser edited this page Oct 20, 2016 · 6 revisions

To edit the meta key that is used to save and retrieve the user ID, use the following filter. The default is user_avatar.

add_filter( 'wcm.avatar.meta_key', function( $key )
{
	return 'user_photo';
} );

To enable the additional custom column in the admin UI user list table, use the following filter. Use this filter if you want to see both the users Gravatar as well as the custom uploaded attachment that can be used instead.

add_filter( 'wcm.avatar.enable_custom_column', '__return_true' );

To disable custom avatars use the following filter. The output of the get_avatar() function will be overriden per default.

add_filter( 'wcm.avatar.enable_custom_avatar', '__return_false' );

To customize the output of the get_avatar() function, use the following filter. The filter has two additional arguments on top of the original arguments that can be retrieved via the get_avatar_data() function.

add_filter( 'wcm.avatar.args', [
	// Default get_avatar_data() args:
	'size'          => 96,
	'height'        => null,
	'width'         => null,
	'alt'           => '',
	'class'         => null,
	'extra_attr'    => '',
	// Additional arguments:
	'size_name'     => 'thumbnail',
	'size_name_2x'  => 'medium',
] );

The class argument gets appended to the following classes. Those are the default for WordPress avatars in the admin UI user list table. The $size argument is the size argument from above array and of the type int.

  • photo
  • avatar
  • avatar-{$size}

To set a different allowed maximum file size/ dimension than the default value of 1024, use the following filter:

add_filter( 'wcm.avatar.size.max', function( $max ) {
	return 2048;
} );

To set a different required minimum file size/ dimension than the default value of 32, use the following filter:

add_filter( 'wcm.avatar.size.min', function( $min ) {
	return 128;
} );

The implementation in your theme should be as following:

function upload_vatar( Array $user )
{
	foreach ( [ 'media', 'file', 'image' ] as $file ) {
		require_once ABSPATH."/wp-admin/includes/{$file}.php";
	}

	$att_id = media_handle_upload( 'user_avatar', $post_id = - 1 );
	if ( is_wp_error( $att_id ) ) {
		avatar_redirect( $att_id->get_error_code() );
	}

	$key = apply_filters( 'wcm.avatar.meta_key', 'user_avatar' );
	// Add attachment-post ID to user as meta single entry to allow querying for it
	$user_meta = update_user_meta( $user['ID'], $key, $att_id );
	if ( FALSE === $user_meta ) {
		avatar_redirect( 'avatar_upload_umeta' );
	}

	// Add user to attachment-post, again as single post meta entry to allow querying it
	$post_meta = add_post_meta( $att_id, 'user_id', $user['ID'], TRUE );
	if ( FALSE === $post_meta ) {
		avatar_redirect( 'avatar_upload_pmeta' );
	}
}

function avatar_redirect() 
{
	if ( headers_sent() ) {
		exit( $reason );
	}
	$key  = $reason === 'saved' ? 'success' : 'error';
	$link = get_permalink( get_queried_object() );
	$url  = $reason === 'login'
		? wp_login_url( $link )
		: add_query_arg( [ $key => $reason ], $link );
	wp_safe_redirect( $url );
	exit();
}
Clone this wiki locally