'Tag comments like <!-- sample comment --> in html for pages and posts in content WordPress

  1. I write content in html with comments, followed by insertion on the page (visual editors and builders are disabled).
  2. I insert this html into the WordPress page and the comments, although not visible, are taken into account, this leads to a bunch of unnecessary vertical indentation.
  3. Question: how to fix? Who has encountered it? UPD: in the page code, it turns into paragraphs: <p><!-- comment --></p> Thank you.

UPD2: It became clear that it is possible (necessary) to use the functions add_filter() and the_content

But how do you do it right? Prompt?

It is necessary that there are tags on the page or records and "nothing" is displayed in their place.

Something like.. (I don't know what to write myself to display in place of the "nothing" comment tags.


/* Add empty paragraph only to Pages. */
function tag_comment_empty_added_page_content ( $content ) {
    if ( is_page() ) {
        return $content . '<!-- * -->';
    }

    return $content;
}
add_filter( 'the_content', 'tag_comment_empty_added_page_content');

Or maybe in that vein?


<?php
function tag_comment_empty_replace_content( $text_content ) {
    if ( is_page() ) {
        $text = array(
            '<!-- * -->' => '',
        );

        $text_content = str_ireplace( array_keys( $text ), $text, $text_content );
    }

It all doesn't work, somewhere a mistake.



Solution 1:[1]

Assuming you store your content as posts, can create a filter for the_content to remove all comments when the content is echoed out.

https://developer.wordpress.org/reference/hooks/the_content/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Olivier