# 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:** 1
**Showing post:** 3

<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.
        }
    }
}

```

---

_[View the full topic](https://meta.discourse.org/t/if-someone-changes-their-email-in-wordpress-change-it-in-discourse-sso-sync/76572)._
