Posted on

Indexing HTML comments

By default, Relevanssi does not index HTML comments inside your posts. Relevanssi removes all HTML tags before indexing, and HTML comments (<!– like this –>) are counted as HTML tags.

If you have content inside comments that you want indexed, you need to modify the punctuation control โ€“ which is responsible for removing the comments โ€“ so that the comments are not removed.

The solution is simple: just add this bit of code to your theme functions.php file and rebuild the index.

add_filter( 'relevanssi_remove_punctuation', 'rlv_keep_comments', 9 );
function rlv_keep_comments( $string ) {
	$string = str_replace( '&lt;!', '', $string );
	return $string;
}

This function attaches itself to the punctuation removal filter hook and since it has priority 9, it runs before the default punctuation removal which runs on priority 10. This function then simply removes the HTML comment start <!, which causes Relevanssi to not remove the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.