我正在学习如何使用wordpress api进行开发。我想要显示我自己的头像,所以我正在点击“获取头像”过滤器。
这是我目前的代码...
function set_profile_avatar($id='', $size = "96", $default = '', $alt = 'profile avatar', $avatar_class = 'profile-avatar' ) {
echo $id;
//get current user id
global $current_user;
if(!$id){ $id = $current_user->ID; }
//set the default avatar img
$default= get_bloginfo('template_directory').'/images/default.png';
//check to see if user has set custom avatar
$gravatar_pic_url = get_user_meta($id, 'display_pic_url', true);
if(!$gravatar_pic_url){
$gravatar_pic_url = $default;
}
//return the complied img tag
return ("<img src='$gravatar_pic_url' width='$size' height='$size' class='$avatar_class' alt='$alt' />");
}
add_filter('get_avatar', 'set_profile_avatar'); 我像这样调用函数...
<?php echo get_avatar($pending_member->ID, '150'); ?>我在回调函数中回显了$id,我发现里面有一个生成的img标记。为什么我的ID变量没有传递给函数。
我猜我搞乱了过滤器钩子的工作方式。
发布于 2014-04-03 20:59:28
我发现了问题所在。add_filter钩子似乎将原始函数的输出作为第一个参数传递,所以即使您不打算使用它,也必须捕获它。
这就是现在运行的代码
//filter for profile avatar pic
function set_profile_avatar($content, $id='', $size = '96', $avatar_class = 'profile-avatar', $default = '', $alt = 'profile avatar') {
//get current user id
global $current_user;
if(!$id){ $id = $current_user->ID; }
//set the default avatar img
$default= get_bloginfo('template_directory').'/images/default.png';
//check to see if user has set custom avatar
$gravatar_pic_url = get_user_meta($id, 'display_pic_url', true);
if(!$gravatar_pic_url){
$gravatar_pic_url = $default;
}
//return the complied img tag
return ("<img src='$gravatar_pic_url' width='$size' height='$size' class='$avatar_class' alt='$alt' />");
}
add_filter('get_avatar', 'set_profile_avatar', 10, 5); 现在,当我像这样调用get头像函数时...
<?php echo get_avatar($pending_member->ID, '150'); ?>它将返回该配置文件ID的自定义头像。您必须考虑到原始函数的输出会自动作为第一个参数传递。
我希望这能为其他开发人员节省很多时间。
https://stackoverflow.com/questions/22833576
复制相似问题