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!
This element is used to add a comment to an HTML document. An HTML comment begins with <! –– and the comment closes with ––> . HTML comments are visible to anyone that views the page source code, but are not rendered when the HTML document is rendered by a browser.
Comments in HTML start with <! -- and end with --> . Don't forget the exclamation mark at the start of the tag! But you don't need to add it at the end.
The leading characters // are added to the beginning of each line when commenting one or more lines of code. You can also block comment multiple lines of code using the characters /* */ .
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> -->
Use a hidden div block
like this:
<div style="display: none;"> ...comment... </div>
works like a charm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With