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
// when a user is deleted, the deleted_user action is fired
// attach our example_cleanup function to this action/hook
// When the deleted_user action/hook/event is fired, this function will be called
// 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
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