Creating WP native comments

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! :slight_smile:

This is coming soon.

@simon you seem to be the man in the know with anything WP Discourse right now :slight_smile:

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

Thanks

Yes I have a PR for this:

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.

4 Likes

AWESOME :clap: thank you.

@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 :smiley:

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:

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.

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.

1 Like

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

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

3 Likes

@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! :slight_smile:

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.

1 Like

@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

Count in comments section

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?

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.

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' );
2 Likes

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

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

3 Likes