Is It Possible to Comment Out HTML Code in a Wordpress Post?
Sometimes I need to inject some raw HTML code into a Wordpress开发者_如何学JAVA 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!
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
You could try one of the following plugins which preserves code formatting within the html editor:
- TRUEedit Plugin
- WP Super Edit
- ps-disable-auto-formatting
- Unfiltered MU (multisite only)
I believe most of these plugins removes the wptexturize filter that WordPress uses which replaces characters and patterns (which messes up some shortcodes and html).
If your using 'Deans FCKEditor' or 'Foliopress WYSIWYG' that could be the problem since they convert quotes to html quotes, add paragraph markup, mess up shortcodes, and do some other html character replacement.
This snippet should do what you're looking for.
// Add the unfiltered_html capability back in to WordPress 3.0 multisite.
function um_unfilter_multisite( $caps, $cap, $user_id, $args ) {
if ( $cap == 'unfiltered_html' ) {
unset( $caps );
$caps[] = $cap;
}
return $caps;
}
add_filter( 'map_meta_cap', 'um_unfilter_multisite', 10, 4 );
Try this:
<!-- Comment --!>
Works like a charm.
Instead of typeing <!--Comment-->
in the editor for your post, Make sure you place the comment tag inside the raw html editor.
(source: headwaythemes.com)
Also use a DOM Inspector to make sure that th -->
closing tag is actually coming form the post itself.
Another Tip, before you publish the article, hit the Close Tags
button to make sure that it validates your html better.
Try this:
<!--<br />
... commented out stuff ...<br >
<-->
but beware the HTML break tag WordPress will throw in at the end of the comment.
Like jharrel suggested, this works just fine:
<!-- content <!-- -->
精彩评论