# Sync Wordpress and Discourse categories

**URL:** https://meta.discourse.org/t/sync-wordpress-and-discourse-categories/236966
**Category:** WordPress
**Created:** [August 23, 2022, 2:43pm UTC](https://meta.discourse.org/t/sync-wordpress-and-discourse-categories/236966 "2022-08-23T14:43:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Evgeniy\_Zubrilin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@Evgeniy\_Zubrilin](https://meta.discourse.org/u/Evgeniy_Zubrilin)
#### Post date: [August 23, 2022, 2:43pm UTC](https://meta.discourse.org/t/sync-wordpress-and-discourse-categories/236966/1 "2022-08-23T14:43:17Z")

</div>

Hey, could you help me how to sync Wordpress and Discourse categories?

E.g., when user on my website create new category, I want it to be created in Discourse, and both post and forum topic will have the same category.

---

<div class="post-metadata">

### Author: ![Genyus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/genyus/32/119655_2.png) [@Genyus](https://meta.discourse.org/u/Genyus)
#### Post date: [August 24, 2022, 3:58am UTC](https://meta.discourse.org/t/sync-wordpress-and-discourse-categories/236966/2 "2022-08-24T03:58:14Z")

</div>

This code isn’t fully tested and would need error handling/safety checks/logging for production use, but should get you started:

```plaintext
add_action( 'create_category', 'discourse_create_category', 10, 1 );
function discourse_create_category( $term_id ) {
	$category = get_term_by( 'id', $term_id, 'category' );
	$name = $category->name;
	$path = '/categories.json';
	// Use the WPDiscourse plugin to generate a new category
	new \WPDiscourse\Utilities\PublicPluginUtilities()->discourse_request(
		$path,
		[
			'method' => 'POST',
			'name' => $name,
		]
	);
}

add_action( 'set_object_terms', 'discourse_set_object_terms', 10, 1 );
function discourse_set_object_terms( $object_id ) {
	if ( ! metadata_exists( 'post', $object_id, 'publish_to_discourse' ) ) {
		// Enable publishing in Discourse, during initial save only.
		update_post_meta( $object_id, 'publish_to_discourse', true );
		$discourse_publish_option = get_option( 'discourse_publish' );
		// set the Discourse category based on WP category
		if ( is_array( $discourse_publish_option ) && array_key_exists( 'publish-category', $discourse_publish_option ) ) {
			$categories_list = wp_get_post_categories( $object_id );
			update_post_meta( $object_id, 'publish_post_category', $categories_list[0]->name );
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![Evgeniy\_Zubrilin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@Evgeniy\_Zubrilin](https://meta.discourse.org/u/Evgeniy_Zubrilin)
#### Post date: [August 24, 2022, 12:44pm UTC](https://meta.discourse.org/t/sync-wordpress-and-discourse-categories/236966/3 "2022-08-24T12:44:21Z")

</div>

Hey, Gary, thank you for your answer!

Unfortunately I have an error “syntax error, unexpected ‘-\>’ (T\_OBJECT\_OPERATOR)” for the following line:

`new \WPDiscourse\Utilities\PublicPluginUtilities()->discourse_request(`

The PHP version is 7.4, it must work. I don’t have the idea about the reason error occurs.

---

<div class="post-metadata">

### Author: ![Genyus](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/genyus/32/119655_2.png) [@Genyus](https://meta.discourse.org/u/Genyus)
#### Post date: [August 24, 2022, 2:40pm UTC](https://meta.discourse.org/t/sync-wordpress-and-discourse-categories/236966/4 "2022-08-24T14:40:52Z")

</div>

I _did_ say it was untested, right? 😅 I can’t offer you too much help with debugging right now, but here’s an updated version that should fix that issue as well as removing a redundant check in the second action:

```plaintext
add_action( 'create_category', 'discourse_create_category', 10, 1 );
function discourse_create_category( $term_id ) {
	$category = get_term_by( 'id', $term_id, 'category' );
	$name = $category->name;
	$path = '/categories.json';
	// Use the WPDiscourse plugin to generate a new category
	$utils = new \WPDiscourse\Utilities\PublicPluginUtilities();
	$utils->discourse_request(
		$path,
		[
			'method' => 'POST',
			'name' => $name,
		]
	);
}

add_action( 'set_object_terms', 'discourse_set_object_terms', 10, 1 );
function discourse_set_object_terms( $object_id ) {
	if ( ! metadata_exists( 'post', $object_id, 'publish_to_discourse' ) ) {
		// Enable publishing in Discourse, during initial save only.
		update_post_meta( $object_id, 'publish_to_discourse', true );
		$discourse_publish_option = get_option( 'discourse_publish' );
		// set the Discourse category based on WP category
		if ( is_array( $discourse_publish_option ) ) {
			$categories_list = wp_get_post_categories( $object_id );
			update_post_meta( $object_id, 'publish_post_category', $categories_list[0]->name );
		}
	}
}

```

> [@Evgeniy\_Zubrilin](#):
>
> The PHP version is 7.4

7.4 is only about three months away from EOL, so you should really start planning to upgrade your hosting environment, if you haven’t already 📅

---

<div class="post-metadata">

### Author: ![Evgeniy\_Zubrilin](https://avatars.discourse-cdn.com/v4/letter/e/8e7dd6/32.png) [@Evgeniy\_Zubrilin](https://meta.discourse.org/u/Evgeniy_Zubrilin)
#### Post date: [August 24, 2022, 2:47pm UTC](https://meta.discourse.org/t/sync-wordpress-and-discourse-categories/236966/5 "2022-08-24T14:47:48Z")

</div>

Yeah, you did 😅 But the fact that I am not well at coding makes me look everywhere for a solution. I haven’t found any freelancer yet, so I was hoping to make it work by myself or with your advice 🙂

Thank you again, I’ll check!
