# Multi-line HTML comments not hidden on post from wp-discourse

**URL:** https://meta.discourse.org/t/multi-line-html-comments-not-hidden-on-post-from-wp-discourse/157177
**Category:** Support
**Created:** [July 8, 2020, 6:51pm UTC](https://meta.discourse.org/t/multi-line-html-comments-not-hidden-on-post-from-wp-discourse/157177 "2020-07-08T18:51:28Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![orenwolf](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/orenwolf/32/119529_2.png) [@orenwolf](https://meta.discourse.org/u/orenwolf)
#### Post date: [July 8, 2020, 6:51pm UTC](https://meta.discourse.org/t/multi-line-html-comments-not-hidden-on-post-from-wp-discourse/157177/1 "2020-07-08T18:51:28Z")

</div>

Hello,

I’m not sure if this is an issue with `wp-discourse` specifically or with the html parser in Discourse itself, but it looks like multi-line HTML comments aren’t hidden in posts generated by wp-discourse.

I’ve just finished setting up [chinwag.pluralistic.net](http://chinwag.pluralistic.net), based on the [pluralistic.net](http://pluralistic.net) posts from Cory Doctorow. His posts include some metadata at the top in the form of HTML comments. Recently, this format changed to multiline instead of single-line.

you can see in this post:

> **[Pluralistic: 04 Jul 2020](https://chinwag.pluralistic.net/t/pluralistic-04-jul-2020/14)**
>
> Originally published at: https://pluralistic.net/2020/07/04/pluralistic-04-jul-2020/ Today's links What to the Slave Is the 4th of July: Scan of the original 1852 pamphlet. This day in history: 2010, 2015, 2019 Colophon: Recent...

The single-line comment from the origin article is removed:

> **[Pluralistic: 04 Jul 2020 – Pluralistic: Daily links from Cory Doctorow](https://pluralistic.net/2020/07/04/pluralistic-04-jul-2020/)**

However, the multiline comment from a later post is displayed directly:

> **[Pluralistic: 06 Jul 2020](https://chinwag.pluralistic.net/t/pluralistic-06-jul-2020/15)**
>
> Originally published at: Pluralistic: 06 Jul 2020 – Pluralistic: Daily links from Cory Doctorow

---

<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 8, 2020, 7:34pm UTC](https://meta.discourse.org/t/multi-line-html-comments-not-hidden-on-post-from-wp-discourse/157177/2 "2020-07-08T19:34:20Z")

</div>

I don’t think the problem is being caused by the WordPress plugin. Here’s some example markup that is not interpreted as a comment by Discourse:

```plaintext
<p><!--
Tags:

Summary:
New podcast; Europe's interop coalition; Scarfolk beermats; Miami cop owns illegal mansion nightclub; Video and transcript of my OII talk; Shower temperature vs handle position

URL:
https://pluralistic.net/2020/07/06/polbathic/

Title:
Pluralistic: 06 Jul 2020 polbathic

Bullet:
🧔🏿

Separator:
_,.-'~'-., __,.-'~'-.,__ ,.-'~'-., __,.-'~'-.,__ ,.-'~'-.,_

Top Sources:
Today's top sources: Fipi Lele, Naked Capitalism (https://www.nakedcapitalism.com/).

--><br></p>

```

Renders as:

 ![image](https://global.discourse-cdn.com/meta/original/3X/4/f/4faa81d2754e724c46fa875b9d9ad82ba4b45894.png)

The problem is that the empty lines within the comment are interpreted as paragraphs by Discourse. When that is done, the HTML comment is no longer valid markup. If the empty lines are removed from within the comment, it will be interpreted correctly by Discourse:

```plaintext
<p><!--
Tags:
Summary:
New podcast; Europe's interop coalition; Scarfolk beermats; Miami cop owns illegal mansion nightclub; Video and transcript of my OII talk; Shower temperature vs handle position
URL:
https://pluralistic.net/2020/07/06/polbathic/
Title:
Pluralistic: 06 Jul 2020 polbathic
Bullet:
🧔🏿
Separator:
_,.-'~'-., __,.-'~'-.,__ ,.-'~'-., __,.-'~'-.,__ ,.-'~'-.,_
Top Sources:
Today's top sources: Fipi Lele, Naked Capitalism (https://www.nakedcapitalism.com/).
--><br></p>

```

Other than editing the post content before publishing the posts to Discourse, I’m not sure what the best approach would be for dealing with this.

---

<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 8, 2020, 10:31pm UTC](https://meta.discourse.org/t/multi-line-html-comments-not-hidden-on-post-from-wp-discourse/157177/3 "2020-07-08T22:31:39Z")

</div>

> [@simon](#):
>
> I’m not sure what the best approach would be for dealing with this.

Probably the best way to deal with this would be to remove the comments from the post before publishing the post to Discourse. Maybe the plugin should do this by default, but this is the first time I’ve come across this issue.

The [WP Discourse](https://github.com/discourse/wp-discourse) plugin has a filter that can be hooked into to parse post content before it is published to Discourse. The filter is called `wp_discourse_excerpt` . The post content is passed to it as a variable. Here is how that filter can be used to remove all comments from the WordPress post before it gets published to Discourse. This pattern could be used to alter the post in other ways too:

```php
add_filter( 'wp_discourse_excerpt', 'wpdc_custom_discourse_excerpt' );
function wpdc_custom_discourse_excerpt( $html ) {
	if ( ! extension_loaded( 'libxml' ) ) {

		return $html;
	}

	$use_internal_errors = libxml_use_internal_errors( true );
	$disable_entity_loader = libxml_disable_entity_loader( true );
	$html_doc = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/></head><body>' . $html . '</body></html>';
	$doc = new \DOMDocument( '1.0', 'utf-8' );
	$doc->loadHTML( $html_doc );
	$finder = new \DOMXPath( $doc );
	$comments = $finder->query( '//comment()' );
	if ( $comments->length ) {
		foreach ( $comments as $comment ) {
			$comment->parentNode->removeChild( $comment );
		}

		$parsed = $doc->saveHTML( $doc->documentElement );
		libxml_clear_errors();
		libxml_use_internal_errors( $use_internal_errors );
		libxml_disable_entity_loader( $disable_entity_loader );

		return preg_replace( '~<(?:!DOCTYPE|/?(?:html|head|meta|body))[^>]*>\s*~i', '', $parsed );
	}

	libxml_clear_errors();
	libxml_use_internal_errors( $use_internal_errors );
	libxml_disable_entity_loader( $disable_entity_loader );

	return $html;
}

```

Adding that code to your WordPress theme’s `functions.php` file should solve the problem. If it is not working for you, check to be sure that the `libxml` extension is enabled on your WordPress site’s server.

---

<div class="post-metadata">

### Author: ![orenwolf](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/orenwolf/32/119529_2.png) [@orenwolf](https://meta.discourse.org/u/orenwolf)
#### Post date: [July 9, 2020, 10:34pm UTC](https://meta.discourse.org/t/multi-line-html-comments-not-hidden-on-post-from-wp-discourse/157177/4 "2020-07-09T22:34:23Z")

</div>

> [@simon](#):
>
> Adding that code to your WordPress theme’s `functions.php` file should solve the problem. If it is not working for you, check to be sure that the `libxml` extension is enabled on your WordPress site’s server.

I will indeed give this a shot, but it sounds like a larger issue if HTML entities in a comment result in Discourse decising that comment is now invalid HTML, no?

---

<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 9, 2020, 11:00pm UTC](https://meta.discourse.org/t/multi-line-html-comments-not-hidden-on-post-from-wp-discourse/157177/5 "2020-07-09T23:00:43Z")

</div>

> [@orenwolf](#):
>
> it sounds like a larger issue if HTML entities in a comment result in Discourse decising that comment is now invalid HTML, no?

I’m not sure how Discourse should handle this. I think that for posts that are created directly on Discourse it makes sense to say that only a limited subset of HTML is supported, but for posts that are created through the API, or by pulling in an RSS feed it’s not clear to me how far Discourse should go to support HTML.

I think it would make sense to add the code that I posted above directly to the [WP Discourse](https://github.com/discourse/wp-discourse) plugin. The plugin is already using similar functionality to clean up comments that are pulled to WordPress from Discourse. That has been in use for some time now with no reports of problems. I’ll get back to you about this within the next couple of days.

---

<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 11, 2020, 9:44pm UTC](https://meta.discourse.org/t/multi-line-html-comments-not-hidden-on-post-from-wp-discourse/157177/6 "2020-07-11T21:44:27Z")

</div>

> [@simon](#):
>
> I think it would make sense to add the code that I posted above directly to the [WP Discourse](https://github.com/discourse/wp-discourse) plugin.

This has been added in [WP Discourse version 2.0.6](https://wordpress.org/plugins/wp-discourse/). When the full post content is published from WordPress to Discourse, any comment blocks in the post will be removed before the post is sent to Discourse.

---

<div class="post-metadata">

### Author: ![orenwolf](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/orenwolf/32/119529_2.png) [@orenwolf](https://meta.discourse.org/u/orenwolf)
#### Post date: [July 11, 2020, 10:16pm UTC](https://meta.discourse.org/t/multi-line-html-comments-not-hidden-on-post-from-wp-discourse/157177/7 "2020-07-11T22:16:05Z")

</div>

Thanks, Simon! I’ll install as soon as this is available and report back.

ETA: Works like a charm. Thanks!

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [August 10, 2020, 10:16pm UTC](https://meta.discourse.org/t/multi-line-html-comments-not-hidden-on-post-from-wp-discourse/157177/8 "2020-08-10T22:16:06Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
