# Creating WP native comments

**URL:** https://meta.discourse.org/t/creating-wp-native-comments/47557
**Category:** WordPress
**Created:** [July 20, 2016, 5:05pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557 "2016-07-20T17:05:39Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [July 20, 2016, 5:05pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/1 "2016-07-20T17:05:39Z")

</div>

Has anyone worked on creating native wordpress comments from Discourse posts instead of just displaying Discourse posts separately?

The reason I ask is because my theme (and I’m sure others) displays the comment count below the post image and title and it would be good to populate this with accurate data.

It would also make styling easier and make the Wordpress plugin compatible with any theme from a design point of view.

I’m assuming there is a good reason that the plugin works the way it does, someone please enlighten me! 🙂

---

<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: [July 20, 2016, 5:16pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/2 "2016-07-20T17:16:59Z")

</div>

> [@jamesmarkcook](#):
>
> The reason I ask is because my theme (and I’m sure others) displays the comment count below the post image and title and it would be good to populate this with accurate data.

This is coming soon.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [July 20, 2016, 5:18pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/3 "2016-07-20T17:18:38Z")

</div>

@simon you seem to be the man in the know with anything [WP Discourse](https://github.com/discourse/wp-discourse) right now 🙂

Do you know roughly how soon this will arrive? Is there a PR created or is it in development or on the roadmap?

Thanks

---

<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: [July 20, 2016, 5:22pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/4 "2016-07-20T17:22:59Z")

</div>

Yes I have a PR for this:

[https://github.com/discourse/wp-discourse/pull/213](https://github.com/discourse/wp-discourse/pull/213)

We just need to be sure that all the recent changes haven’t broken anything before anything new gets added. Hopefully it can be merged in the next week or so.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [July 20, 2016, 5:28pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/5 "2016-07-20T17:28:30Z")

</div>

> [@simon](#):
>
> We just need to be sure that all the recent changes haven’t broken anything before anything new gets added. Hopefully it can be merged in the next week or so.

AWESOME 👏 thank you.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [July 20, 2016, 5:44pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/6 "2016-07-20T17:44:59Z")

</div>

@simon I just took a look at the PR.

Correct me if I’m wrong but it looks like you’re just displaying a comment count on the Discourse comment template, not actually creating comments in the Wordpress system that can be utilised by the rest of the system.

The reason I was asking about native Wordpress comments being created from Discourse posts is because other parts of Wordpress display the count and use it for other things such as the concept of ‘Popular topics’ for example to display in a widget.

Lots of themes also show comment count underneath Post titles and excerpts which wouldn’t be affected by this PR if I’m not mistaken. Please let me know if I’ve missed something.

Is anyone aware of a reason why the approach to create native Wordpress comments from Discourse data wasn’t pursued?

Thanks 😃

---

<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: [July 20, 2016, 6:31pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/7 "2016-07-20T18:31:13Z")

</div>

> [@jamesmarkcook](#):
>
> other parts of Wordpress display the count

In theory, your theme should have access to the comments number now. The plugin filters the value returned by the wordpress `comments_number` function. This is how it looks with the twentysixteen theme:

 ![](https://global.discourse-cdn.com/meta/original/3X/1/b/1ba6eb9c79b200e2160480d79b7b7b321cf99283.png)

But the plugin is not filtering the value returned by the `get_comments_number` function. This is the function that returns the comment number as an integer (instead of a string like ‘2 Replies’). If you are not seeing a comments number where you expect one, this is probably why. It’s a little tricky, because that number is used to keep track of the number of WordPress comments as opposed to the comments generated on Discourse, but it should definitely get fixed. I’ll look at it asap.

> [@jamesmarkcook](#):
>
> Is anyone aware of a reason why the approach to create native Wordpress comments from Discourse data wasn’t pursued?

I don’t know, but the plugin’s comment html can be overwritten in your theme. It wouldn’t be very difficult to match a theme’s comment styles. I’ll look into that soon.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [July 20, 2016, 6:34pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/8 "2016-07-20T18:34:32Z")

</div>

Thanks for the reply. I’m currently running into some issues with SSO but once I’ve fixed that I will test it out 👍

---

<div class="post-metadata">

### Author: ![elijah](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/elijah/32/104055_2.png) [@elijah](https://meta.discourse.org/u/elijah)
#### Post date: [July 20, 2016, 7:30pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/9 "2016-07-20T19:30:40Z")

</div>

I can verify that comment counts are working for me with my brand new test set-up.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [July 21, 2016, 3:34pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/10 "2016-07-21T15:34:13Z")

</div>

@simon I only truly understand what you said here now after spending some time looking at the code and modifying my theme and reading up on filters! 🙂

Currently my theme is using `comments_number` where you leave a comment but `get_comments_number` for the number next to the comments icon. It was this icon, and hence `get_comments_number` that i was referring to in my original question.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [August 19, 2016, 2:40pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/11 "2016-08-19T14:40:39Z")

</div>

@simon I suspect you understand how this plugin is working a bit better than me and just wanted to get your opinion on how I would also overwrite the value for `get_comments_number()` and not just `comments_number()`.

My theme uses the former to display the number of comments in the header, along with number of shares and views etc. At the moment this is always 0 due to not being compatible with the `wp-discourse` plugin. I suppose in the short term just overwriting this number when each post is loaded would do, but ideally I’d like Wordpress to be aware of the number of Discourse comments at a lower level.

### Count in header

![](https://global.discourse-cdn.com/meta/original/3X/b/5/b50cdc25f32cb22146c3d0e58c4f301f7944494a.png)

### Count in comments section

 ![](https://global.discourse-cdn.com/meta/original/3X/5/d/5d97ec9c0da9abc3bd556cff89754c427e7ce004.png)

This is so that I can determine popular posts and things like that in Wordpress. At the moment Wordpress knows nothing of the Discourse comments if I’m not mistaken.

Would this require rewriting the plugin entirely?

---

<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: [August 19, 2016, 3:50pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/12 "2016-08-19T15:50:05Z")

</div>

A fix that will work for all cases requires changing the plugin a bit, but if you are not using native WordPress comments at all and are instead only using comments that are generated through Discourse, you can add something like this to your theme’s `functions.php` file.

I haven’t tested this, so if it’s not working let me know.

```php
function wp_discourse_comments_number() {
  $post = get_post();
  $count = intval( get_post_meta( $post->ID, 'discourse_comments_count', true ) );
  return $count;
}
add_filter( 'get_comments_number', 'wp_discourse_comments_number' );

```

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [August 19, 2016, 4:01pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/13 "2016-08-19T16:01:15Z")

</div>

Thanks for your help. I will give this a go and let you know.

---

<div class="post-metadata">

### Author: ![jamesmarkcook](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jamesmarkcook/32/168563_2.png) [@jamesmarkcook](https://meta.discourse.org/u/jamesmarkcook)
#### Post date: [August 19, 2016, 4:03pm UTC](https://meta.discourse.org/t/creating-wp-native-comments/47557/14 "2016-08-19T16:03:21Z")

</div>

@simon it works a treat! Thank you very very much. I owe you a 🍺
