• Hi.

    I need to print an html comment on my home page, and only. I’ve come with this snippet. But it doesn’t work has the output is “<!– site verification XXXXX –>”

    How can I print an html comment using php ?

    Thanks

    //Verification tag
    add_action('wp_head', 'verification_tag);
    
    function verification_tag(){
    if(is_front_page()) { 
    	$data = "site verification XXXXX";
    	echo htmlentities('<!-- ' . $data . ' -->',ENT_QUOTES);  
    }
    };
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi, kiliot, try this instead:

    //Verification tag
    add_action('wp_head', 'verification_tag');
    
    function verification_tag(){
    	if ( is_front_page() ) { 
    		$data = "site verification XXXXX";
    		//use a function to sanitize the $data variable if necessary here if it's being received as input from somewhere
    		echo "<!-- $data -->";  
    	}
    }

    This way the comment won’t be interpreted as text that is displayed on the page, which I’m assuming you don’t want to occur.

    Thread Starter kiliot

    (@kiliot)

    Thanks, but that doesn’t work. It doesn’t print the html comment. I tested changing echo “kkk <!– $data –>”; and it only prints the kkk.

    HTML comments will be hidden(can only be seen on page source) if you want to print the comment then you need to use the <pre></pre> tag to see all the content inside it.

    echo "<pre><!-- $data --></pre>";

    • This reply was modified 2 years, 9 months ago by Vijay Hardaha.
    Thread Starter kiliot

    (@kiliot)

    The html comments are not being printed on the code. I open the source code of the page and the comment is not there. What i’m looking for is a way to print an html comment that is only visible on the code (that’s the idea of an html comment). Thanks

    Then you might have an optimization plugin installed that removes HTML comments from your page sources.

    Thread Starter kiliot

    (@kiliot)

    Ahhh, never thought about that. Will investigate about that. Thanks for the tip

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Print HTML comments in the header’ is closed to new replies.