# Bring Discourse information to WordPress

**URL:** https://meta.discourse.org/t/bring-discourse-information-to-wordpress/64894
**Category:** WordPress
**Created:** [2017年六月21日 15:13 UTC](https://meta.discourse.org/t/bring-discourse-information-to-wordpress/64894 "2017-06-21T15:13:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dylanb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dylanb/32/139102_2.png) [@dylanb](https://meta.discourse.org/u/dylanb)
#### Post date: [2017年六月21日 15:13 UTC](https://meta.discourse.org/t/bring-discourse-information-to-wordpress/64894/1 "2017-06-21T15:13:06Z")

</div>

I’m looking to implement Discourse/WP SSO as a way to allow users to login to my WordPress site with Discourse accounts that they already have. In that process I was hoping to be able to bring along information (group, trust level) and store that in WordPress to allow them to view/hide certain content. Is this currently possible?

---

<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: [2017年六月21日 15:37 UTC](https://meta.discourse.org/t/bring-discourse-information-to-wordpress/64894/2 "2017-06-21T15:37:38Z")

</div>

> [@dylanb](#):
>
> I was hoping to be able to bring along information (group, trust level) and store that in WordPress

The wp-discourse plugin isn’t doing this yet, but it’s something that people have been asking for. What I think could be done in the near future would be to bring that information across from Discourse to WordPress and store it as user metadata. It would be up to a site’s developer to make use of the information.

---

<div class="post-metadata">

### Author: ![dylanb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dylanb/32/139102_2.png) [@dylanb](https://meta.discourse.org/u/dylanb)
#### Post date: [2017年六月21日 15:57 UTC](https://meta.discourse.org/t/bring-discourse-information-to-wordpress/64894/3 "2017-06-21T15:57:25Z")

</div>

That is exactly the sort of functionality I would be looking for. Just store that information as user meta data and then the WP features can check against it.

---

<div class="post-metadata">

### Author: ![dylanb](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dylanb/32/139102_2.png) [@dylanb](https://meta.discourse.org/u/dylanb)
#### Post date: [2017年六月22日 13:32 UTC](https://meta.discourse.org/t/bring-discourse-information-to-wordpress/64894/4 "2017-06-22T13:32:09Z")

</div>

So if this isn’t standard right now, can anyone point me in the right direction of where to go toward implementing it?

---

<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: [2017年六月22日 16:19 UTC](https://meta.discourse.org/t/bring-discourse-information-to-wordpress/64894/5 "2017-06-22T16:19:15Z")

</div>

You can use something like this and hook it into the `wp_login` action (remove the `public` keyword.)

```plaintext
    public function lookup_discourse_user( $username, $wp_user ) {
		$base_url = $this->options['url'];
		$api_key = $this->options['api-key'];
		$api_username = $this->options['publish-username'];

		if ( empty( $base_url ) || empty( $api_key ) || empty( $api_username ) ) {

			return new \WP_Error( 'discourse_configuration_error', 'The WP Discourse plugin has not been properly configured.' );
		}

		// Try to get the user by external_id.
		$external_user_url = esc_url_raw( $base_url . "/users/by-external/$wp_user->ID.json" );
		$external_user_url = add_query_arg( array(
			'api_key' => $api_key,
			'api_username' => $api_username,
		), $external_user_url );

		$response = wp_remote_get( $external_user_url );

		if ( ! DiscourseUtilities::validate( $response ) ) {

			return new \WP_Error( 'discourse_response_error', 'There has been an error in retrieving the user data from Discourse.' );
		}

		$user_data = json_decode( wp_remote_retrieve_body( $response ), true );

		return $user_data;
	}

```

You can get the options in your theme or a plugin by calling `$options = WPDiscourse\Utilities\Utilities::get_options()`. You can use the `validate` method by calling `WPDiscourse\Utilities\Utilities::validate( $response )`.

In the next update to the plugin, I might be able to add an option to sync user\_data on login. It probably won’t sync all the data that you’re looking for, but an action could be added that would let you access the user\_data.
