# If someone changes their email in WordPress, change it in Discourse - sso\_sync

**URL:** https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572
**Category:** WordPress
**Created:** [21.Декабрь.2017 18:45:17 UTC](https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572 "2017-12-21T18:45:17Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [21.Декабрь.2017 18:45:17 UTC](https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572/1 "2017-12-21T18:45:17Z")

</div>

Howdy all. I’ve spent a couple hours reading threads and trying to get this work with no luck.

Basically we just want that if someone changes their email in WordPress, it gets changed in Discourse.

I think I can use the sso\_sync api call but I can’t seem to figure out how to use it. Can anyone help with a php code example? (I use other discourse api calls successfully. Just can’t figure out how to use this one.)

Thanks!

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [21.Декабрь.2017 18:48:22 UTC](https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572/2 "2017-12-21T18:48:22Z")

</div>

I should add that I know the email change will happen when the user next logs in to discourse, but we need to instantaneously change it when they change it in WordPress.

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [22.Декабрь.2017 04:54:22 UTC](https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572/3 "2017-12-22T04:54:22Z")

</div>

This is working to sync SSO records when I run it in my development environment. I’m testing this inside of a class in the wp-discourse plugin. `$this->options[]` is from wp-discourse. You could just hard-code the values. I’ll look at it some more tomorrow.

```php
Class DiscourseSSO {
    // Add the action hook
    public function __construct() {
        add_action( 'profile_update', array( $this, 'sync_sso' ), 10, 2 );
     }

    public function sync_sso( $user_id, $old_data ) {
        $user = get_user_by( 'id', $user_id );

        if ( $user->user_email !== $old_data->user_email ) {
            $url = $this->options['url'] . '/admin/users/sync_sso';
            $api_key = $this->options['api-key'];
            $api_username = $this->options['publish-username'];
            $sso_secret = $this->options['sso-secret'];
            $params = array(
                'username' => $user->user_login,
                'email' => $user->user_email,
                'external_id' => $user_id,
            );

            // base64 encode the SSO params.
            $sso_user = base64_encode( http_build_query( $params ) );
            // Create the signature for Discourse to match against the payload.
            $sig = hash_hmac( 'sha256', $sso_user, $sso_secret );

            $response = wp_remote_post( esc_url_raw( $url ), array(
                'body' => array(
                    'sso' => $sso_user,
                    'sig' => $sig,
                    'api_key' => $api_key,
                    'api_username' => $api_username,
                ),
            ) );

            $response = json_decode( wp_remote_retrieve_body( $response ) );

            // Do something with the response.
        }
    }
}

```

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [02.Январь.2018 18:17:25 UTC](https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572/4 "2018-01-02T18:17:25Z")

</div>

Thanks @simon. Finally got a minute to try this and it works great!

---

<div class="post-metadata">

### Author: ![andrec](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/andrec/32/120211_2.png) [@andrec](https://meta.discourse.org/u/andrec)
#### Post date: [29.Январь.2018 20:57:09 UTC](https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572/5 "2018-01-29T20:57:09Z")

</div>

Hey Leah - would love to understand how you managed to connect MemberMouse up to Discourse (allow signups to a specific MM bundle; and disable a users account if they don’t renew a yearly subscription).

Mind if I pick your brain?

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [31.Январь.2018 14:55:11 UTC](https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572/6 "2018-01-31T14:55:11Z")

</div>

Hi André. I’d be happy to write up all the things I did to connect WordPress and MemberMouse. I’ll do it in a separate thread here for any other MemberMouse users. Might take me a week to get to it though.

---

<div class="post-metadata">

### Author: ![lkramer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lkramer/32/83897_2.png) [@lkramer](https://meta.discourse.org/u/lkramer)
#### Post date: [02.Февраль.2018 15:36:20 UTC](https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572/7 "2018-02-02T15:36:20Z")

</div>

OK @andrec, [I wrote some MemberMouse stuff up here](https://meta.discourse.org/t/wp-membermouse-discourse-integration-guide/79636). I didn’t include very much actual code but feel free to ask me if you get stuck on any specific part of things.

---

<div class="post-metadata">

### Author: ![andrec](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/andrec/32/120211_2.png) [@andrec](https://meta.discourse.org/u/andrec)
#### Post date: [07.Февраль.2018 14:26:45 UTC](https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572/8 "2018-02-07T14:26:45Z")

</div>

@lkramer - you rock, thx a ton!
