uffehe
(Uffe Henrichsen)
1
我安装了 DiscourseConnect 提供商的 Wordpress 和 Discourse 设置。我正在使用 WP Discourse 插件,到目前为止运行良好。到目前为止遇到的唯一问题是,我无法在 WP 中显示 Discourse 的头像图片。
我尝试在 Could you update user avatar for wordpress plugin sso? - #2 by angus 中实现解决方案 - 将代码添加到我的 WP 网站的 function.php 文件。但对我来说效果不佳 - 我不知道该添加什么代码来设置头像。
我想知道是否有人成功过?我正在使用 Simple Local Avatar 插件,但如果有人使用其他插件成功过,我很乐意更换。
angus
(Angus McLeod)
2
嘿 @uffehe
您正在添加什么代码,Simple Local Avatar 插件如何保存头像?
uffehe
(Uffe Henrichsen)
3
您好 @angus
我不是开发人员,所以我在猜测,但据我所知,Simple Local Avatar 插件通常会将图片添加到媒体库。我找不到关于通过 URL 提供的图片如何工作的太多文档。我正在尝试的代码如下:
add_filter( 'wpdc_sso_client_updated_user', 'my_wpdc_sso_client_updated_user', 10, 2 );
function my_wpdc_sso_client_updated_user( $updated_user, $query ) {
if (isset($query['avatar_url'])) {
$new_avatar_url = $query['avatar_url'];
$avatar_data = array(
'full' => $new_avatar_url, // URL of the new avatar image
);
update_user_meta($updated_user, 'simple_local_avatar', $avatar_data);
}
}
当我尝试使用 SSO 登录时,这会给我一个未处理的异常。
我或许可以雇佣一名开发人员来帮助我,但这似乎是一个相当常见的用例,其他人可能已经实现了——即使不是使用此插件。
simon
4
我认为异常的原因是您的 function my_wpdc_sso_client_updated_user 没有返回 $updated_user 数组。该数组对于添加 wpdc_sso_client_updated_user 过滤器的函数是必需的。
您还需要在调用 update_user_meta 时设置用户的 WordPress 用户 ID。
这可以工作,但会禁用 Simple Local Avatars 插件的重缩放功能:
add_filter( 'simple_local_avatars_dynamic_resize', '__return_false' ); // prevent resizing of avatars
add_filter( 'wpdc_sso_client_updated_user', 'my_wpdc_sso_client_updated_user', 10, 2 );
function my_wpdc_sso_client_updated_user( $updated_user, $query ) {
if ( isset( $query['avatar_url'] ) ) {
$new_avatar_url = $query['avatar_url'];
$wp_user_id = $updated_user['ID'];
$avatar_data = array(
'full' => $new_avatar_url, // URL of the new avatar image
);
update_user_meta( $wp_user_id, 'simple_local_avatar', $avatar_data );
}
return $updated_user;
}
阻止重缩放头像的原因是重缩放是使用 WordPress 图像编辑器代码完成的。为了使其正常工作,需要将图像下载到 WordPress。请注意,如果从上面的代码中删除 add_filter( 'simple_local_avatars_dynamic_resize', '__return_false' ) 行,Simple Local Avatars 插件将尝试重缩放图像,抛出 PHP 警告,然后使用完整尺寸的图像 - 因此从用户的角度来看似乎没有任何问题。
我不太确定将 Discourse 头像与 Simple Local Avatars 插件结合使用。我看到的问题是,该插件为用户提供了在 WordPress 上传自定义头像的选项。如果他们这样做了,然后从 Discourse 重新登录到 WordPress,他们可能会想知道他们上传的头像发生了什么。开发起来会更复杂,但也许最好在用户 WordPress 个人资料页面上添加将 Discourse 头像明确设置为 WordPress 头像的功能。
2 个赞
simon
5
也许是吧。既然我已经在看了,我想我也可以尝试一种不使用 Simple Local Avatars 插件的方法。
下面的代码挂钩到用户通过 Discourse 登录 WordPress 时调用的 'wpdc_sso_client_updated_user' 过滤器。如果用户在 Discourse 上传了自定义头像,他们的 Discourse custom_avatar_template 将作为用户元数据保存在 WordPress 上。然后,它挂钩到 WordPress 的 'get_avatar_url' 过滤器,并使用 Discourse custom_avatar_template 来设置头像的 URL。我喜欢这种方法,因为它利用了 Discourse 的模板调整大小功能。
请注意,我只在本地测试过此代码。
use WPDiscourse\\Utilities\\Utilities as DiscourseUtilities;
add_filter( 'wpdc_sso_client_updated_user', 'my_wpdc_sso_client_updated_user', 10, 2 );
function my_wpdc_sso_client_updated_user( $updated_user, $query ) {
$options = DiscourseUtilities::get_options();
$base_url = $options['url'];
$discourse_username = $query['username'];
$discourse_profile_url = "{$base_url}/u/{$discourse_username}.json";
$wp_user_id = $updated_user['ID'];
$profile_data = DiscourseUtilities::discourse_request( $discourse_profile_url );
if ( is_wp_error( $profile_data ) ) {
return $updated_user;
}
// 检查用户是否在 Discourse 上有上传头像的模板。
$user_data = $profile_data->user;
if ( ! property_exists( $user_data, 'custom_avatar_template' ) ) {
return $updated_user;
}
$custom_avatar_template_path = $profile_data->user->custom_avatar_template;
$custom_avatar_template = "{$base_url}{$custom_avatar_template_path}";
// 保存头像模板。显示的大小将在 `get_avatar_url` 过滤器中替换。
update_user_meta( $wp_user_id, 'discourse_avatar_template', $custom_avatar_template );
return $updated_user;
}
add_filter('get_avatar_url', 'my_get_avatar_url', 10, 3 );
function my_get_avatar_url($url, $id_or_email, $args ) {
// 以 $user_id 设置为 `null` 开始。下面的代码可能遗漏一些情况。
$user_id = null;
if ( is_numeric( $id_or_email ) ) {
$user_id = $id_or_email;
} elseif ( $id_or_email instanceof WP_User ) {
$user_id = $id_or_email->ID;
} elseif ( $id_or_email instanceof WP_Post ) {
$user_id = $id_or_email->user_id;
} elseif ( $id_or_email instanceof WP_Comment ) {
$user_id = $id_or_email->user_id;
}
// 如果 $user_id 尚未设置,则返回 $url 参数。
if ( empty( $user_id ) ) {
return $url;
}
$discourse_avatar_template = get_user_meta( $user_id, 'discourse_avatar_template', true );
// 如果模板元数据尚未设置,则返回 $url。
if (empty( $discourse_avatar_template ) ) {
return $url;
}
$size = isset( $args['size']) ? $args['size'] : 128; // 我不确定 'size' 是否可能为空。128 似乎是一个安全的后备值。
return str_replace( '{size}', $size, $discourse_avatar_template );
}
3 个赞
uffehe
(Uffe Henrichsen)
6
你好 @simon
太棒了 - 它完全按照描述工作。我同意,不需要 WP 插件并且避免了头像冲突的混乱,这样更简洁。谢谢!
2 个赞
system
(system)
关闭
7
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.