admin管理员组文章数量:1131208
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
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
Share Improve this question asked Nov 16, 2012 at 23:50 jimileskujimilesku 2632 gold badges10 silver badges22 bronze badges 4- 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 Commented Nov 17, 2012 at 1:01
- I would love to do it if possible. Any idea how to do it? – jimilesku Commented Nov 17, 2012 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 Commented Nov 17, 2012 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 ♦ Commented Nov 17, 2012 at 14:34
2 Answers
Reset to default 3When 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
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>";
}
}
本文标签: functionsRemove the deleted users avatar from list
版权声明:本文标题:functions - Remove the deleted users avatar from list 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736725728a1949715.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论