Is It Possible to Comment Out HTML Code in a Wordpress Post?

user249493 picture user249493 · Jul 27, 2010 · Viewed 31.8k times · Source

Sometimes I need to inject some raw HTML code into a Wordpress post, and sometimes I need to comment out a chunk of that code.!

With a plain text editor, I can just use <!-- Comment --> around the chunk I want to hide.

But when I try this in a WP post, it does hide the code but I still see the "closing comment tag" -->.

What's the right way, if possible, to comment out code in a WP post?

Thanks!

Answer

jharrell picture jharrell · Mar 4, 2013

wpautop() contains a bug that breaks comments containing HTML code. An easy workaround is to add a second opening HTML comment tag just before the closing - this tricks WordPress into working like you would expect it to. see http://core.trac.wordpress.org/ticket/2691

This will work in WordPress:

<!-- <div class="book floatleft"><a href="#">
<img src="http://www.myreallycoolsite.com/wp-content/uploads/2013/02/button.png" alt="" />
</a></div> <!-- -->

This will not work in WordPress:

<!-- <div class="book floatleft"><a href="#">
<img src="http://www.myreallycoolsite.com/wp-content/uploads/2013/02/button.png" alt="" />
</a></div> -->