Link custom user field to external website

This needs to be done on SSO side, so you will need to look into how your SSO is set-up.

1 Like

I’m just using the WP Discourse plugin.

When I visit the user profile as admin (/admin/users/2/[username]), I do see the External ID under the Single Sign On section at the bottom.

Does that move me any further ahead in the process? Is it just a matter of properly modifying OP’s code?

@Simon_Cossar, is there a good way to do this in the WP Discourse plugin?

Yes, there is. What data do you want to add to the field?

1 Like

Just the External ID to a custom field.

Aside from that, it would be cool if there was an easy way to do it for other fields as well. (ie location or any custom fields on the WP side).

It’s not as easy as I thought. I might not be doing this correctly.

It’s possible to set the value of a UserCustomField through the SSO parameters sent from WordPress by hooking into the wpdc_sso_params filter. If you create a ‘wordpress id’ User Field on Discourse, you can then create new Discourse UserCustomField record by doing something like this:

add_filter( 'wpdc_sso_params', 'my_namespace_custom_fields', 10, 2 );
function my_namespace_custom_fields( $sso_params, $user ) {
    $sso_params['custom.wordpress_id'] = $user->ID;

	return $sso_params;
}

The problem I’m having is that I can’t figure out how to access the value of that record on the front-end of Discourse.

3 Likes

Thanks Simon! Yeah I just poked around as well and couldn’t find the field name anymore. How did you find it through the console?

My solution seems wrong. I’ll look at this some more when I get a chance.

Edit:
I was having some trouble understanding why this works, but after looking through the code a bit more this seems to make sense as a way of setting custom fields through SSO.

Create a user field on Discourse in the way outlined in the OP and then go to your profile as an admin and add a value (any value) to it. You can then go to the Discourse rails console and enter:

UserCustomField.all

A list of all the user_custom_fields on your site will be returned. It will be made up of one or more entries that look something like this:

#<UserCustomField:0x007fa02bb39d18 id: 18, user_id: 1, name: "user_field_8", value: "test value", created_at: Wed, 07 Jun 2017 01:48:10 UTC +00:00, updated_at: Wed, 07 Jun 2017 01:48:10 UTC +00:00>]
Look for the entry that has a value attribute that matches the value you just saved in your custom field. Copy the name of that field into a function on your WordPress site that is something like this:

add_filter( 'wpdc_sso_params', 'my_namespace_set_discourse_custom_field', 10, 2 );
function my_namespace_set_discourse_custom_field( $sso_params, $user ) {
    $sso_params['custom.user_field_7'] = $user->ID;

    return $sso_params;
}

To use the code in the OP, replace the name value (‘User Profile’) in this line:
const externalUserIdField = siteUserFields.filterBy('name', 'User Profile')[0]
with whatever you have called the field you added on Discourse. This is the part of the code that I wasn’t understanding, but it makes more sense after looking at this: discourse/users_controller.rb at master · discourse/discourse · GitHub

Unless there is a better way to do this, I’ll add it to the WP Discourse Plugin Tips and Tricks topic.

3 posts were split to a new topic: How to update user Web Site field via API

This guide is now updated to deprecate api.container.lookupFactory in favor of api.modifyClass.

5 Likes

I want to do something like this but then dont the users on your website now have 2 user profiles, one from discourse and one from your site?

Same here, I want to link to the external profile from the users profile/card. I have the external_id synchronised during sso, so how to create that link or rather: how do I get the external_id available as a user field as described in the first post?

2 Likes

For anyone who is not a coder wanting to link words in a customized user field to a webpage, here is how you do it.

I wanted to add this customized field at sign up, so I filled in the form like so:

With this code in the field description (thanks to @Dax) for the linked words:

I am cool with the guidelines <a href=https://forums.wethepeoplejournal.com/faq">found here</a>

This is the custom field new members see. They click on “found here” to review our groundrules, which are strict due to the toxic climate in local town forums, which we are working to avoid:

45%20AM

8 Likes

Awesome, thanks a lot. You can even shorten the ÙRL being a relative one if you just want to link to the FAQ or ToS on the same host. That makes the string shorter and more reliable if you ever change the VHost. If you prefer to open them in a new tab, add target="_blank" after the href.

I've read the <a href="/faq">FAQ</a> and <a href="/tos">ToS</a>.


5 Likes

Thanks, @dnsmichi. The shorter url and blank target edit worked like a charm.

2 Likes

So instead of waiting for the external user ID become available I implemented as described in the first post. Replaced the constants with my data and created a rewrite from the Discourse user ID to my main sites user ID.

It’s not working though. Only when I manually put the user id into the field of a profile it works. :cry:

Wasn’t the point not to have the need to manually put the user ID into this field as per the inactive option “Editable after signup?” What am I missing?

1 Like

@techAPJ, I’m on Discourse 2.0.0b10, I’ve tried a bunch of modifications but can’t get the custom field to blend in correctly. With the default CSS it just shifts other custom fields. Any thoughts?

Also, I usually have the “Website” custom field showing after the Farm/Company…" field, any way to do that?

2 Likes

Also today I realized that the User Profile page actually displays the custom field as it should, although compared to the user card, now the custom field (Website) is displayed last instead of first…

Capture

1 Like

I feel like I’ve seen a discussion of this, but I can’t find it now.

I’ve got a custom profile field that holds a user’s website implemented using this tutorial, but allowing for any external site to be entered, rather than just linking to Twitter or something. The problem I have is that about 80% of the users don’t bother to put “http(s)://” in front of the URL they enter. They (rightly) expect that “www.example.com” should just work. So how do I make it just work?

Edited to add: Figured it out. Below is the entire if block of javascript I used to determine whether to attempt to show an external link. The inner if is what determines whether the external URL needs to have “http” added to it, and takes care of doing it.

    if (userFields && userFields[churchURLFieldId]) {
      const rawChurchURL = userFields[churchURLFieldId];
      var url = rawChurchURL;
      if (!/^https?:\/\//i.test(rawChurchURL)) {
        url = 'http://' + rawChurchURL;
      }
      const link = "<a href='"+url+"' target='_blank'>"+userFields[churchNameFieldId];+"</a>";
      return Ember.Object.create({ link, name: churchURL.get('name') });
    } else {
      return null;
    }

Is there a way to edit existing user profile fields rather than adding new one? So that the existing twitter field value can be edited to contain anchor tag

the main post that allows us to add new clickable profile fields using connectors/handlebar outlets etc, those get “added” and so I had to hide the existing profile fields
e.g. I could add a clickable twitter field and so have to hide the existing twitter field
Now the newly added twitter field will be in a separate div rather than belonging to that same ember div which was pre-existing, resulting in weird css

Would using /connectors/user-profile-public-fields/Twitter instead of using /connectors/user-profile-primary/ help here?