# Bring Discourse information to WordPress

**URL:** <https://meta.discourse.org/t/bring-discourse-information-to-wordpress/64894>\
**Category:** WordPress\
**Created:** [Juin 21, 2017, 3:13 UTC](https://meta.discourse.org/t/bring-discourse-information-to-wordpress/64894 "2017-06-21T15:13:05Z")\
**Posts on this page:** 1\
**Showing post:** 5

<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:** [Juin 22, 2017, 4: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.

---

_[View the full topic](https://meta.discourse.org/t/bring-discourse-information-to-wordpress/64894)._
