MyBB Community Forums

Full Version: Help with hardcoding a default "No Avatar"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In the file called: functions_post.php. Which can be found in the "inc" directory.

This code can be found starting around line: 303.

		if($post['avatar'] != "" && ($mybb->user['showavatars'] != 0 || !$mybb->user['uid']))
		{
			$post['avatar'] = htmlspecialchars_uni($post['avatar']);
			$avatar_dimensions = explode("|", $post['avatardimensions']);
			
			if($avatar_dimensions[0] && $avatar_dimensions[1])
			{
				list($max_width, $max_height) = explode("x", my_strtolower($mybb->settings['postmaxavatarsize']));
			 	if($avatar_dimensions[0] > $max_width || $avatar_dimensions[1] > $max_height)
				{
					require_once MYBB_ROOT."inc/functions_image.php";
					$scaled_dimensions = scale_image($avatar_dimensions[0], $avatar_dimensions[1], $max_width, $max_height);
					$avatar_width_height = "width=\"{$scaled_dimensions['width']}\" height=\"{$scaled_dimensions['height']}\"";
				}
				else
				{
					$avatar_width_height = "width=\"{$avatar_dimensions[0]}\" height=\"{$avatar_dimensions[1]}\"";	
				}
			}
			
			eval("\$post['useravatar'] = \"".$templates->get("postbit_avatar")."\";");
			$post['avatar_padding'] = "padding-right: 10px;";
		}
		else
		{
			$post['useravatar'] = "";
		}

In this section of that code above, show below:

			$post['useravatar'] = "";

I added a default avatar to be displayed in posts for members that have not chosen one like this below.

			$post['useravatar'] = "<a href=\"{$post['profilelink_plain']}\"><img src=\"{$mybb->settings['bburl']}/images/no_avatar.gif\" class=\"avatar_border_static\" alt=\"\" /></a>";

The problem I face with though. That default avatar gets displayed in both the "classic" and "horizontal" layouts. I need to be able to add a conditional so one version of that no_avatar gets displayed in the classic layout, but another one gets shown in the horizontal layout. How can that be done with the code above, looking at this section I would presume most below.

			eval("\$post['useravatar'] = \"".$templates->get("postbit_avatar")."\";");
			$post['avatar_padding'] = "padding-right: 10px;";
		}
		else
		{
			$post['useravatar'] = "<a href=\"{$post['profilelink_plain']}\"><img src=\"{$mybb->settings['bburl']}/images/no_avatar.gif\" class=\"avatar_border_static\" alt=\"\" /></a>";
		}

I can see it uses an (else) statement, so I'm thinking that can be expanded upon to list two different avatars I enter. One for the classic layout and one for horizontal layout.
Just to add the reason why I need to do this. I need to display a different default "No Avatar" in the Horizontal layout, because it requires that "margin-right: 10px;" be added to the css it uses. Done so it matches the other avatars people upload and use, creating a 10 px space between the avatar on the right-side and the rank images.

The margin space cannot be added if the same avatar is also displayed in the Classic Layout. It pushed the avatar 10px over to the left side, it doesn't align center in that classic layout.
What you could do, rather than altering the core files, you could use ZingaBurgas php in templates plugin. Then you can do this in the postbits:

<if $post['avatar'] then>
<href="member.php?action=profile&amp;uid=$post[uid]"><img src="{$post['avatar']}" width="45" height="45" alt="avatar" /></a>
<else>                                       
<img src="{$mybb->settings['bburl']}/images/avatars/noavatar.gif" width="45" height="45" alt="avatar" />
</if>


You can add a class to the avatar if you want, then you can style it.
I hope that helps

I've already edited two other core files to display a No Avatar in both the Members List and Members Profile for people who don't choose or upload one. And the method works great compared to most plug-ins I've tried first, that to be honest had issues one way or another.

With a few plugs I noticed they assign a No Avatar to new members, but if a member then uploads and later removed that avatar. He's left using none, he's not re-assigned back the No Avatar. That's one issue I spotted with a few plug-ins.

All the functions_post.php needs now to finish the job. Is just some permission code adding to display one version in classic layout, the other in horizontal layout. But I'm not good with PHP myself, I'm no coder so don't know what the required code would be to change it in the core file to work right.

I'm not bothered about editing the core files, because I won't be upgrading any further now past MyBB 1.6.1. I've made way too made changes with things since.

Thanks for the advice, but I'm really looking to edit the core file with the correct permission to do it that way.
Try this code (replace it with the current code):

		if($post['avatar'] != "" && ($mybb->user['showavatars'] != 0 || !$mybb->user['uid']))
		{
			$post['avatar'] = htmlspecialchars_uni($post['avatar']);
			$avatar_dimensions = explode("|", $post['avatardimensions']);
			
			if($avatar_dimensions[0] && $avatar_dimensions[1])
			{
				list($max_width, $max_height) = explode("x", my_strtolower($mybb->settings['postmaxavatarsize']));
			 	if($avatar_dimensions[0] > $max_width || $avatar_dimensions[1] > $max_height)
				{
					require_once MYBB_ROOT."inc/functions_image.php";
					$scaled_dimensions = scale_image($avatar_dimensions[0], $avatar_dimensions[1], $max_width, $max_height);
					$avatar_width_height = "width=\"{$scaled_dimensions['width']}\" height=\"{$scaled_dimensions['height']}\"";
				}
				else
				{
					$avatar_width_height = "width=\"{$avatar_dimensions[0]}\" height=\"{$avatar_dimensions[1]}\"";	
				}
			}
			
			eval("\$post['useravatar'] = \"".$templates->get("postbit_avatar")."\";");
			$post['avatar_padding'] = "padding-right: 10px;";
		}
		else
		{
			if($mybb->user['classicpostbit'] == 1)
			{
				// if user is using classic postbit
				$post['useravatar'] = "<a href=\"{$post['profilelink_plain']}\"><img src=\"{$mybb->settings['bburl']}/images/no_avatar.gif\" class=\"avatar_border_static\" alt=\"\" /></a>";
			}
			else
			{
				// if user is using horizontal postbit
				$post['useravatar'] = "<a href=\"{$post['profilelink_plain']}\"><img src=\"{$mybb->settings['bburl']}/images/no_avatar.gif\" class=\"avatar_border_static\" alt=\"\" /></a>";
			}
		}
Thank-you very much. Just added and tested it now, appears to work perfect.

        else
        {
            if($mybb->user['classicpostbit'] == 1)
            {
                // if user is using classic postbit
                $post['useravatar'] = "<a href=\"{$post['profilelink_plain']}\"><img src=\"{$mybb->settings['bburl']}/images/no_avatar.gif\" class=\"avatar_border_static\" alt=\"\" /></a>";
            }
            else
            {
                // if user is using horizontal postbit
                $post['useravatar'] = "<a href=\"{$post['profilelink_plain']}\"><img src=\"{$mybb->settings['bburl']}/images/no_avatar.gif\" class=\"avatar_border_static\" alt=\"\" /></a>";
            }
        }
(2010-12-30, 02:05 AM)Gary Bolton Wrote: [ -> ]I'm not bothered about editing the core files, because I won't be upgrading any further now past MyBB 1.6.1. I've made way too made changes with things since.

Thanks for the advice, but I'm really looking to edit the core file with the correct permission to do it that way.

No problems - if you don't intend to upgrade then changing core files is ok....as long as you don't NEED to upgrade for any as yet unknown security issues etc. The plug-in from ZingaBurga is good though - it allows you to make quick changes at template level using php. Very handy, and I have had no issues with it.