Help with functions code to Checking for Avatar or Profile Photo

Help please with functions code to Checking for Avatar or Profile Photo

Currently the code always displays ‘Neither avatar nor Gravatar exist.’

‘Neither avatar nor Gravatar exist.’ displays when no photo, and when with a photo uploaded.

My Wordpress them uses this code:

if(ThemexCore::checkOption('user_avatar')) {

The original code is here:

I currently have this code in functions.php

function validate_gravatar($email) {
$hash = md5($email);
$uri = 'http://www.mydomainname.com/wp-content/uploads/2016/06/' . $hash . '?d=404';
$headers = @get_headers($uri);
if (!preg_match("|200|", $headers[0])) {
$has_valid_avatar = FALSE;
} else {
$has_valid_avatar = TRUE;
}
return $has_valid_avatar;
}

and this code to display a notice to the user/member:

<?php
$avatarcount = 0;
if ( is_user_logged_in() ) {
global $bp;
global $current_user;
get_currentuserinfo();
$local = BP_AVATAR_UPLOAD_PATH . '/wp-content/uploads/2016/06/' . $bp->loggedin_user->id;
$useremail = $current_user->user_email;
if (file_exists($local) ) {
echo 'Avatar exists';
}
elseif (validate_gravatar($useremail)) {
$avatarcount++;
echo 'Gravatar exists';
}
else { echo 'Neither avatar nor Gravatar exist.';
}
}
?>

I am trying a simpler code in functions.php

Yet “111” or “222” are being displayed.

All help appreciated :slight_smile:

function current_user_has_avatar() {
$user_avatar = get_user_meta( $user->ID, 'user_avatar', true );
if ( empty( $user_avatar ) ) {
echo ("111");
} else {
echo ("222");
}
}

Help please, I almost have this code correct,

But it always display the same message “Please upload a photo” with and without an avatar.

<?php 
    
$avatar=ThemexCore::getUserMeta($user_id, 'avatar');
$user_id = get_current_user_id();
if(empty($avatar)) { 
 echo "Please upload a photo"; 
 } else {
 echo "Thank you";
}    

?>

Assuming there’s not a problem with ThemexCore, shouldn’t these 2 lines be switched?

$avatar=ThemexCore::getUserMeta($user_id, 'avatar');
$user_id = get_current_user_id();
1 Like

That’s to obvious.

1 Like

Thank you Mittineague for your quick correct reply, Yes the code is working correctly now, Much appreciated, Thank you :slight_smile:

If you could tell me how do I change the echo "Please upload a photo"; into being able to display a <div class

<div class="profile-warning">
    <span class="profile-warning">Please upload a photo</span>
    </div>

Hello oddz, What is obvious to you is cloudy grey areas to me :slight_smile: Thank you :slight_smile:

Don’t feel bad, there’s been more than one morning I’ve poured my first cup of coffee before setting down the mug. :coffee:

I’m not sure what your code looks like, seems like every example in this topic is different.

But as a general rule, unless you are writing hacky quick-and-dirty one-time-use throw away code for your own personal use (did I use enough adjectives to convey how much a Bad Practice it is?) you want to not have a function output any HTML.
You want to have the function return something.

If you have procedural code with conditionals, you can get away with outputting HTML from within the conditionals.
Still not best practice, but commonly enough done. eg.

$avatar = my_awesome_function($id);
if (!$avatar) {
$output = <<<PWDIV 
<div class="profile-warning">
    <span class="profile-warning">Please upload a photo</span>
 </div>
PWDIV;

echo $output;
} else 
1 Like

Hello Mittineague, Thank you again for your quick reply and helpful code :slight_smile:

Update: Hello Mittineague I have it working by adding <html> <body> to your code

    <?php  

    $user_id = get_current_user_id();
    $avatar=ThemexCore::getUserMeta($user_id, 'avatar');
    if(empty($avatar)) { 

   $output = <<<PWDIV
<html>
<body>
<div class="profile-warning">
    <span class="profile-warning">Please upload a Profile Photo</span>
 </div>
</body>
</html>
PWDIV;
echo $output;

     } else {
     echo "Great Profile Photo";
    }    

    ?>

Are you working for money?

1 Like

Hello oddz, Money? no money involved, I could not make money coding … lol, my site with a wordpress theme, making a few beneficial changes. best wishes :slight_smile:

1 Like

What is ThemexCore returning? i.e. what does

$avatar=ThemexCore::getUserMeta($user_id, 'avatar');
var_dump($avatar);

display?

1 Like

Hello Mittineague, Thank you for your reply, if I have placed your code correctly it displays: string(0) “”

    <?php  
$user_id = get_current_user_id();
$avatar=ThemexCore::getUserMeta($user_id, 'avatar');
var_dump($avatar);
    
?>

Testing an empty string with empty() will be true.

So if that isn’t what you’re expecting it to be the problem lies elsewhere.

I see ThemexCore is Premium.

For the money, is the Support no good?

1 Like

How about a var dump on user id.

1 Like

Hello Mittineague, Good to hear from you, I purchased a domain with this theme on it, so not an actual direct purcahase, yet theme support has been kind by giving small amounts of code and pointing me in the right direction, yet not complete coding, so that is why I am here, Mittineague your help is greatly appreciated :slight_smile:

A var_dump of $user_id is a good idea.

You could also try

$my_known_id  = 2; // or what ever it is
$avatar=ThemexCore::getUserMeta($my_known_id, 'avatar');
1 Like

This code is working well even with the <body> </body> removed.

 <?php  

    $user_id = get_current_user_id();
    $avatar=ThemexCore::getUserMeta($user_id, 'avatar');
    if(empty($avatar)) { 

   $output = <<<PWDIV
<html>

<div class="profile-warning">
    <span class="profile-warning">Please upload a Profile Photo</span>
 </div>

</html>
PWDIV;
echo $output;

     } else {
     echo "Great Profile Photo";
    }    

    ?>

That code works well to notify a User “Please upload a Profile Photo”

Yet I wanted to prevent Users without a profile photo from sending messages.

So I have added the same conditional code to the message function:

public static function addMessage($ID, $user, $message) {
        
        $user_id = get_current_user_id();
        $avatar=ThemexCore::getUserMeta($user_id, 'avatar');
        if(empty($avatar)) { 
        $message=trim(preg_replace($filters, '', $message));
        }

You will see it also has the $message last line, to use the themes error messages:

self::updateMembership($ID, 'messages', -1);
                } else {
                    ThemexInterface::$messages[]=__("You've been added to the ignore list", 'theme');
                }
            } else {
                ThemexInterface::$messages[]=__('Message field must not be empty', 'theme');
            }            
        } else {
            ThemexInterface::$messages[]=__('You have exceeded the number of messages', 'theme');
        }
        
        if(empty(ThemexInterface::$messages)) {
            wp_redirect(themex_url());
            exit();
        }
    }

Yet when I try to add 1 more error message, I get an “Internal Error”

  self::updateMembership($ID, 'messages', -1);
                    } else {
                        ThemexInterface::$messages[]=__("You've been added to the ignore list", 'theme');
                    }
                } else {
                    ThemexInterface::$messages[]=__('Message field must not be empty', 'theme');
                }   
      
 } else {
 ThemexInterface::$messages[]=__('You must upload a Profile Photo to send messages', 'theme');
 }     
   
            } else {
                ThemexInterface::$messages[]=__('You have exceeded the number of messages', 'theme');
            }
            
            if(empty(ThemexInterface::$messages)) {
                wp_redirect(themex_url());
                exit();
            }
        }

Many thank yous Mittineague, and oddz for your support with this code, the code is now working correctly :slight_smile: