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( __(' %1$s', 'bo'), count_following($curauth->ID) ); ?>
<?php get_status_following($curauth->ID); ?>
Thank You for the help in advance
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
hookI 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:
The downside being it assumes more knowledge and requires more initial effort for those unfamiliar with the APIs
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.