Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
1  
Hi Jimmy. +1 on @Tom's answer. Just wondering, why did you put the followers on a single user meta? Wouldn't it be easier to put each follower ID in a different user meta with the same key? That way you can easily delete the user meta value with the value of that deleted user ID. –  ifdion Nov 17 '12 at 1:01
    
I would love to do it if possible. Any idea how to do it? –  jimilesku Nov 17 '12 at 1:24
    
@ifdion this is the default function that I had in the theme I'm experimenting with. Don't know how to do it Your way, any possible solution that will make my life easy? –  jimilesku Nov 17 '12 at 1:49
    
There are advantages to putting it on a single user meta too, if we're talking about precise data, the best way would actually be to do it via a user taxonomy where each term slug is the ID of a user, so all users following User 5 would have the following term 5 –  Tom J Nowell Nov 17 '12 at 14:34
add comment

2 Answers 2

up vote 3 down vote accepted

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

share|improve this answer
    
Yes I have the function that every user has it's own count of people that are following him and that he follows. Can You please help me out and pointing me where should I place Your code: add_action('deleted_user'.... to clean up the image? –  jimilesku Nov 17 '12 at 0:16
    
I'm supposed to have a lot of users that follow each other and I need an automated function that cleans up the deleted users image information so I don't have blank images. Is it doable? –  jimilesku Nov 17 '12 at 0:18
    
the hook goes along with all your other code, I assumed you were aware of how hooks/actions/filters work? Whenever a user is deleted, the example_cleanup function will be executed, inside that function write your cleanup code for that user –  Tom J Nowell Nov 17 '12 at 14:28
    
See here: codex.wordpress.org/Plugin_API Hooks/actions/filters are an important part, if you don't udnerstand what they are then you won't see why the question "where do I put this" is a somewhat meaningless question. The code is executed when the action/hook is fired, wether it was added at the beginning or end of a plugin of functions.php file is not relevant ( though for other hooks it can be relevant but thats something to read up on ) –  Tom J Nowell Nov 17 '12 at 14:32
    
show 2 more comments

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>";

    }


}
share|improve this answer
    
It does the trick, but now some of the custom user avatars return as a big image. Some of them uploaded a big image 1024x860 and I just resized it in css to fit the avatar size. Now it displays the big image instead of the resized one. Can I fix it quickly? –  jimilesku Nov 17 '12 at 14:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.