• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

how to make Avatar visible to registered members only?

erich37

Well-known member
#1
I run a private forum and would like to show Avatars (thread creator avatar) to members only.

So literally, visitors who are looking at a thread (thread-content is already made visible to registered members only) should not be able to see the custom avatar-image of the user. There should be just the default avatar visible for guests.

The real avatar-image of the user should be visible to registered members only.


Is there a way to achieve this ?


Appreciate your help!
 

Jake Bunce

XenForo moderator
Staff member
#2
Requires code edit:

library/XenForo/Template/Helper/Core.php

Add the red code:

Code:
	public static function helperAvatarHtml(array $user, $img, array $attributes = array(), $content = '')
	{
		if (!empty($attributes['size']))
		{
			$size = strtolower($attributes['size']);

			switch ($size)
			{
				case 'l':
				case 'm':
				case 's':
					break;

				default:
					$size = 'm';
			}
		}
		else
		{
			$size = 'm';
		}

		$forceType = (isset($attributes['forcetype']) ? $attributes['forcetype'] : null);

		$canonical = (isset($attributes['canonical']) && self::attributeTrue($attributes['canonical']));

		$src = call_user_func(self::$helperCallbacks['avatar'], $user, $size, $forceType, $canonical);

		$src = (XenForo_Visitor::getInstance()->user_id ? $src : call_user_func(self::$helperCallbacks['avatar'], $user, $size, 'default', $canonical));

		$href = self::getUserHref($user, $attributes);
		unset($attributes['href']);

		if ($img)
		{
			$username = htmlspecialchars($user['username']);
			$dimension = XenForo_Model_Avatar::getSizeFromCode($size);

			$image = "<img src=\"{$src}\" width=\"{$dimension}\" height=\"{$dimension}\" alt=\"{$username}\" />";
		}
		else
		{
			$text = (empty($attributes['text']) ? '' : htmlspecialchars($attributes['text']));

			$image = "<span class=\"img {$size}\" style=\"background-image: url('{$src}')\">{$text}</span>";
		}

		$class = (empty($attributes['class']) ? '' : ' ' . htmlspecialchars($attributes['class']));

		unset($attributes['user'], $attributes['size'], $attributes['img'], $attributes['text'], $attributes['class']);

		$attribs = self::getAttributes($attributes);

		if ($content !== '')
		{
			$content = " {$content}";
		}

		return "<a{$href} class=\"avatar Av{$user['user_id']}{$size}{$class}\"{$attribs} data-avatarHtml=\"true\">{$image}{$content}</a>";
	}
That will make it so guests only see default avatars for everyone.
 

erich37

Well-known member
#3
Requires code edit:

library/XenForo/Template/Helper/Core.php

Add the red code:

Code:
public static function helperAvatarHtml(array $user, $img, array $attributes = array(), $content = '')
{
if (!empty($attributes['size']))
{
$size = strtolower($attributes['size']);
 
switch ($size)
{
case 'l':
case 'm':
case 's':
break;
 
default:
$size = 'm';
}
}
else
{
$size = 'm';
}
 
$forceType = (isset($attributes['forcetype']) ? $attributes['forcetype'] : null);
 
$canonical = (isset($attributes['canonical']) && self::attributeTrue($attributes['canonical']));
 
$src = call_user_func(self::$helperCallbacks['avatar'], $user, $size, $forceType, $canonical);
 
$src = (XenForo_Visitor::getInstance()->user_id ? $src : call_user_func(self::$helperCallbacks['avatar'], $user, $size, 'default', $canonical));
 
$href = self::getUserHref($user, $attributes);
unset($attributes['href']);
 
if ($img)
{
$username = htmlspecialchars($user['username']);
$dimension = XenForo_Model_Avatar::getSizeFromCode($size);
 
$image = "<img src=\"{$src}\" width=\"{$dimension}\" height=\"{$dimension}\" alt=\"{$username}\" />";
}
else
{
$text = (empty($attributes['text']) ? '' : htmlspecialchars($attributes['text']));
 
$image = "<span class=\"img {$size}\" style=\"background-image: url('{$src}')\">{$text}</span>";
}
 
$class = (empty($attributes['class']) ? '' : ' ' . htmlspecialchars($attributes['class']));
 
unset($attributes['user'], $attributes['size'], $attributes['img'], $attributes['text'], $attributes['class']);
 
$attribs = self::getAttributes($attributes);
 
if ($content !== '')
{
$content = " {$content}";
}
 
return "<a{$href} class=\"avatar Av{$user['user_id']}{$size}{$class}\"{$attribs} data-avatarHtml=\"true\">{$image}{$content}</a>";
}
That will make it so guests only see default avatars for everyone.
Thanks a lot for this Jake!

I just applied this code, but unfortunately the personal User-Photo (uploaded Avatar) is still showing at the Membercard and also at the User-Profile-Page.

Is there a way to also hide the User-Photo and only show the default-avatar at the Membercard and at the User-Profile-Page (for guests) ?


Appreciate your help!
Many thanks!

:)
 

Jake Bunce

XenForo moderator
Staff member
#4
Those areas call on the helperAvatarUrl function in the same file. You need to make a similar change to that function.
 

erich37

Well-known member
#6
I would like to show the "Default Avatar" to guests at the "Membercard" and also at the "User-Profile Page".

Is there anyone who could help me with the code-edit in order to achieve this ?



Appreciate your help!
 

bart

Active member
#7
I have a question that's in the same realm.

I would not like guests to see the memberlist at all. Nor the profiles, nor the membercard. How do I do that?
 

James

Well-known member
#8
I have a question that's in the same realm.

I would not like guests to see the memberlist at all. Nor the profiles, nor the membercard. How do I do that?
You could do template conditionals to ensure a user is logged in, or alternatively you could make code edits or create an add-on to disallow access to guests.
 

bart

Active member
#10
Member list is explained here: http://xenforo.com/community/resources/make-the-member-list-inaccessible-to-guests.337/

Apply the same method to the profile pages and member card.
Works!

I use

Code:
<xen:if is="{$visitor.user_group_id}==2">
so only usergroup 2 (members) can view the memberlist and not member awaiting approval or being banned.

In the member_card-template you have to put the xen:if in between the main div.

I did:
member_list
member_view
member_card

I think that's all...
 

James

Well-known member
#11
Works!

I use

Code:
<xen:if is="{$visitor.user_group_id}==2">
so only usergroup 2 (members) can view the memberlist and not member awaiting approval or being banned.

In the member_card-template you have to put the xen:if in between the main div.

I did:
member_list
member_view
member_card

I think that's all...
Using that particular condition means that only people whose primary usergroup is members can see the memberlist. You should change it to use {xen:helper ismemberof, $visitor, 2}.

Registered should be the primary usergroup anyway (as per the "official" way of using permissions and inheritance), but this is just in case it isn't.
 

bart

Active member
#12
Using that particular condition means that only people whose primary usergroup is members can see the memberlist. You should change it to use {xen:helper ismemberof, $visitor, 2}.

Registered should be the primary usergroup anyway (as per the "official" way of using permissions and inheritance), but this is just in case it isn't.
Thanks, that's better!

One last question:

- how do i disable the membercard completely?




Sorry Erich37 for taking over your thread. But I think this might interest you too.
 

Jake Bunce

XenForo moderator
Staff member
#16
hmmm..... yeah, but how ? :D;)

I am a noob :cautious::cool:


Could you please help me out ?
Undo the previous edit and do this instead (add the red):

Code:
	public static function getAvatarUrl(array $user, $size, $forceType = '')
	{
		if (XenForo_Visitor::getInstance()->user_id && !empty($user['user_id']) && $forceType != 'default')
		{
			if ($user['gravatar'] && $forceType != 'custom')
			{
				return self::_getGravatarUrl($user, $size);
			}
			else if (!empty($user['avatar_date']))
			{
				return self::_getCustomAvatarUrl($user, $size);
			}
		}

		return self::_getDefaultAvatarUrl($user, $size);
	}
That will make it so guests only see default avatars for everyone.
 

dawg

Well-known member
#18
Undo the previous edit and do this instead (add the red):

Code:
    public static function getAvatarUrl(array $user, $size, $forceType = '')
    {
        if (XenForo_Visitor::getInstance()->user_id && !empty($user['user_id']) && $forceType != 'default')
        {
            if ($user['gravatar'] && $forceType != 'custom')
            {
                return self::_getGravatarUrl($user, $size);
            }
            else if (!empty($user['avatar_date']))
            {
                return self::_getCustomAvatarUrl($user, $size);
            }
        }

        return self::_getDefaultAvatarUrl($user, $size);
    }
That will make it so guests only see default avatars for everyone.
Hi Jake. Is this edit still valid? This is exactly what i need. I did the edit but got errors about a white syntax or something like that. Maybe i did it wrong, just checking since we are way beyond the version you suggested this for.
 

RoldanLT

Well-known member
#19
Undo the previous edit and do this instead (add the red):

Code:
    public static function getAvatarUrl(array $user, $size, $forceType = '')
    {
        if (XenForo_Visitor::getInstance()->user_id && !empty($user['user_id']) && $forceType != 'default')
        {
            if ($user['gravatar'] && $forceType != 'custom')
            {
                return self::_getGravatarUrl($user, $size);
            }
            else if (!empty($user['avatar_date']))
            {
                return self::_getCustomAvatarUrl($user, $size);
            }
        }

        return self::_getDefaultAvatarUrl($user, $size);
    }
That will make it so guests only see default avatars for everyone.
Working great for me, only thing it didn't work on notable pages :).

Edit: Never mind, it was just my cache, it works great hehe.
 
Last edited: