Recently my team and I have been working on a feature that allows a user’s Discourse username to be edited from Wordpress with the WP Discourse plugin installed and our Wordpress site set as the SSO provider for Discourse. Currently we have had some success using the Discourse API, where we have a custom meta field in Wordpress that, when updated, calls our site’s Discourse instance with a PUT request to update the user’s username (similar to a solution proposed here: How does one change a username via the API?)
However, there is an existing option within WP Discourse as shown here:
but this field is only used for publishing posts (from WP to Discourse) and does not actually update the user’s Discourse username in Discourse. It syncs when a user changes their username in Discourse, but it does not sync from Wordpress to Discourse when changed.
My question is, will this type of two-way sync be implemented at some point? Why is there an option to allow users to change this username field if it does not sync from Wordpress to Discourse? My team would also be interested in contributing this feature if it is not something already in the works. Our site needs this functionality, and we imagine it would be useful for all WP Discourse sites as well. Please let me know about this!
@angus@simon since you are both involved heavily in WP Discourse and helped us with our SSO issues I think I should direct this question to you guys!
WordPress 플러그인에서 ‘Discourse Username Editable’ 필드는 이름이 적절하지 않을 수 있습니다. 해당 설정의 설명 문구를 업데이트하여 이 설정의 용도를 명확히 해야 합니다. ‘Discourse Username Editable’ 설정이 활성화되면 WordPress 사이트 사용자는 자신의 WordPress 프로필 페이지에서 Discourse 사용자 이름을 설정할 수 있습니다. 이 설정이 비활성화되어 있으면, WordPress 사이트의 관리자만 사용자의 Discourse 사용자 이름을 설정할 수 있습니다. Discourse 사용자 이름은 플러그인이 WordPress에서 Discourse로 게시물을 게시할 때만 사용됩니다.
WordPress와 Discourse 사이에 DiscourseConnect가 사용되는 경우, 사용자의 Discourse 사용자 이름은 처음에는 WordPress 사용자 이름에서 가져와서 설정됩니다. Discourse 사이트에서 auth_overrides_username 설정이 활성화되어 있으면, Discourse의 사용자 이름은 WordPress에서만 설정할 수 있습니다. Discourse에서 auth overrides username 설정이 _비활성화_되어 있으면, 사용자가 Discourse에서 사용자 이름을 편집할 수 있으므로 두 시스템 간의 사용자 이름이 동기화되지 않을 수 있습니다.
‘Discourse Username’ 필드에 관해, 이상적으로는 이 필드가 Discourse 사용자 이름에 기반하여 백그라운드에서 항상 설정되어야 합니다. 관련 코드를 마지막으로 확인한 지 오래되었지만, WordPress 사이트가 Discourse의 SSO 제공자로 사용되고 WordPress의 DiscourseConnect Provider 옵션 탭에서 “로그인 시 Discourse 사용자 생성 또는 동기화” 옵션이 활성화되어 있으면 Discourse Username 필드가 자동으로 채워진다고 생각합니다. Discourse가 WordPress의 SSO 제공자로 사용되는 경우( DiscourseConnect Client 설정과 함께)에도 Discourse Username 필드는 자동으로 설정됩니다.
이상적으로는, Discourse의 “로그인 시 Discourse 사용자 생성 또는 동기화” 옵션이 활성화되어 있든 아니든, WordPress가 Discourse의 SSO 제공자인 경우 Discourse Username 필드가 항상 자동으로 설정되어야 합니다. 두 사이트 사이에 DiscourseConnect가 사용되지 않는 경우를 위해 무엇을 할 수 있을지 모르겠지만, @angus가 그 문제에 대해 아이디어를 가지고 있을 수도 있습니다.
기본적으로 WordPress는 사용자가 사용자 이름을 변경하는 것을 허용하지 않으므로, 이 문제에 대해 많은 질문을 받은 적은 없습니다. WordPress와 Discourse 간에 사용자 이름이 동기화되도록 하려면 Discourse에서 auth_overrides_username 설정을 활성화해야 합니다. WordPress에서 “로그인 시 Discourse 사용자 생성 또는 동기화” 옵션도 활성화하는 것이 좋습니다. 이 옵션이 활성화되면, 사용자가 WordPress 사이트에 로그인할 때마다 Discourse에서 사용자의 정보가 업데이트됩니다. 이 옵션이 비활성화되어 있으면, 사용자가 Discourse에서 로그아웃한 후 DiscourseConnect로 다시 로그인할 때만 Discourse에서 사용자의 정보가 업데이트됩니다.
Thanks for the reply @simon and explanation of the existing feature.
Our site may be a bit of a special case, because we do not want to have the user’s Wordpress username synced with Discourse precisely because Wordpress does not allow users to change their usernames by default. And we do not want to install a plugin that adds this WP username edit functionality due to the instability that it could create.
But since usernames are editable in Discourse, I think it would make sense to extend this functionality into Wordpress via the WP Discourse plugin. This field seems like the perfect place to do that, but I understand that it could conflict with the WP username since that would never change by default. So I take it that this is not something you want us to contribute to the plugin?
Also, allowing users to edit their username in the forum is a major requirement for us. We already have Discourse setup to allow this, but most of our users do not change their Discourse username because it is hidden within the Discourse settings. We want it in plain site within their profile page on our Wordpress site, alongside their other info like name, avatar, etc.
However they will be the same on new signups, as you’re using DiscourseConnect. I’m assuming you’re ok with that, and you’re just concerned about syncing per se (i.e. after account creation)?
How are you envisaging the case when a user creates a Wordpress account and has not yet signed in to Discourse? No Discourse account will exist. In that case the Discourse Username field would not be editable?
Note that the “Update Userdata” webhook (see “Webhooks” in the WP Discourse settings) will update the Discourse Username in Wordpress when it’s changed in Discourse. That’s one part of the two way sync you’re envisaging.
So specifically what you’re after is just for the WP Discourse plugin to update the user’s Discourse username when it changes, yes?
Now, that field is used by different people for different purposes. Some people want to be able to edit that field without updating the username of the Discourse user associated with the account they use with DiscourseConnect.
However, there’s a relatively straightforward solution. We could just add an action somewhere around here so that you could use a PUT request to Discourse to update the username on Discourse, i.e. like you are now. I’d just add that the easy way to do that is to use the abstracted discourse_request method in the WP Plugin’s Utilities, i.e.
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;
$path = ''
$args = array(
);
$response = DiscourseUtilities::discourse_request( $path, $args );
Using both the Webhook and action callback, you’d have a two-way sync in the way you’re envisaging, assuming you’ve accounted for the two questions I raised above.
Happy to review and merge a PR with an additional action along those lines.
We are actually overriding this behavior with the wpdc_sso_params filter hook because our SSO system integrates Firebase and everyone’s WP username defaults to their Firebase UID, so we are changing the Discourse default to be memberXXX (with XXX being a number). We actually want to change this default to be the user’s Firstname_Lastname at some point as well. But yes our primary concern is users being able to change their username easily via their profile page within our Wordpress site.
In this case I am thinking we can either store the username field and have it sync when the account is created (via the wpdc_sso_params filter hook on the first SSO), or we could force a Discourse account to be created for every member when they sign up on our Wordpress site via the Discourse API. The second option would probably be more advantageous because we would know that everyone has a Discourse account and there would thus be fewer special cases of some users having accounts and others not. But we are really going for a seamless experience, so we want this username to be what the user thinks of as their username for our entire platform. We want it to always be editable, and in the future we will probably use it for more than just Discourse (i.e. in leaderboards, etc.).
Yes this is exactly what we need!
Understood! Thanks for this direction, my team and I will work on testing this with our implementation first and should make a PR soon!