Remove the deleted users avatar from list

Problem

Hi everyone, I have a bit of a problem. I have a function that outputs the avatars of users that follow me . It works great, but if the user is deleted the avatars remain visible as a blank image box with a broken hyperlink missing the users username. Is there any way to get rid of the deleted users avatar from the list?

Function

function get_status_following($userid, $count = 0){

$followers = get_the_author_meta('following', $userid);

/** if no followers at the moment */
if( !is_array($followers)){
    $return = "";
} else {

    $return = '<ul class="widget_follow">' . "n";
    foreach( $followers as $folow){

        $return .= "<li>";
            $return .= '<a href="' . get_author_posts_url($folow) . '" title="' . get_the_author_meta('display_name', $folow) . '">';
                if( get_the_author_meta( 'user_custom_avatar', $folow ) != "" ) {
                    $return .= '<img src="' . get_the_author_meta( 'user_custom_avatar', $folow ) . '" alt="" />';
                } else {
                    $return .= get_avatar( get_the_author_meta( 'user_email', $folow ), '40' );
                }
            $return .= '</a>';
        $return .= "<li>";

    }
    $return .= '</ul>' . "n";
}

echo $return;

}

Outputing the function in a page

<?php printf( __('&nbsp;%1$s', 'bo'), count_following($curauth->ID) ); ?>
<?php get_status_following($curauth->ID); ?>

Thank You for the help in advance

Related posts

Leave a Reply

2 comments

  1. When a user say user A is deleted, you’re not cleaning up all traces of that user.

    Namely you need to go into each user following A and remove it from their user meta. This is why your function is showing blank users, because its being given stale information that’s out of date, and refers to users that no longer exist

    You’ll want to do this on the deleted_user hook

    // when a user is deleted, the deleted_user action is fired
    // attach our example_cleanup function to this action/hook
    add_action('deleted_user','example_cleanup');
    
    // When the deleted_user action/hook/event is fired, this function will be called
    function example_cleanup($user_id){
        // remove this user from the users following this user
    }
    

    I don’t know if you have a list of users following a user, if not, you may want to do this else this operation will be costly as you’ll need to iterate over every single user and remove the meta if present to unfollow the deleted user

    note: I would avoid using language keywords as names for variables, so no $function $foreach $return or $class

    How I Would Have Implemented This

    Right now you have 2 sets of duplicated data. You have a piece of meta saying A follows B, and some data saying B is followed by A.

    So instead of using user meta, I would have used a user taxonomy instead. My user taxonomy would be called “following”, and each term in the taxonomy would represent a user.

    Say I have the user “admin” and I have 5 followers, users A,B,C,D, and E, each user would be assigned the term “admin” in my taxonomy.

    I would then have a very easy way of grabbing who is following me, and who a person follows. To grab who I’m following I just do wp_get_object_terms passing in my user ID rather than a post ID. To see who is following me, I grab all objects assigned to the term that has the same name as me.

    How do I create a user taxonomy?

    See this article by Justin Tadlock, it covers everything from admin UIs to registering the taxonomy, to a frontend template

    Some final notes, you will need to hook into user creation and deletion to create/delete associated terms for that user. I’d recommend using the login name as the term slug as it doesn’t change, whereas display name does meaning you have to do additional work.

    The benefits of doing it this way:

    • Cleaning up is as simple as deleting a term, no iterating through users and user meta
    • 1 piece of data to store following/followers not 2
    • It’s a more logical relationship in the database to follow
    • As taxonomy querys get faster so do yours
    • You can use the WordPress core APIs to access all the data you need, rather than writing your own wrapper functions, you can outsource that part to the experienced developers working on WordPress itself saving time
    • It’s a lot easier to do listings, or rankings of who has the most followers, in the same way we can rank which categories have the most posts
    • With some work you can retrofit things like tag clouds to work for followers showing who has the most visually
    • You get most of an admin interface for it right out of the box

    The downside being it assumes more knowledge and requires more initial effort for those unfamiliar with the APIs

  2. Quick and dirty temporary fix, while you re write your theme function.
    Do a get_user_by query on foreach($followers as $follow). That way you will only print existing user, not deleted ones.

    foreach( $followers as $folow){
    
        $existing_user = get_user_by('id', $folow);
        if($existing_user){ // you got a user
    
        $return .= "<li>";
            $return .= '<a href="' . get_author_posts_url($folow) . '" title="' . get_the_author_meta('display_name', $folow) . '">';
                if( get_the_author_meta( 'user_custom_avatar', $folow ) != "" ) {
                    $return .= '<img src="' . get_the_author_meta( 'user_custom_avatar', $folow ) . '" alt="" />';
                } else {
                    $return .= get_avatar( get_the_author_meta( 'user_email', $folow ), '40' );
                }
            $return .= '</a>';
        $return .= "<li>";
    
        }
    
    
    }