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

34,214

Solution 1

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> -->

Solution 2

Use a hidden div block

like this:

<div style="display: none;">

...comment...

</div>

works like a charm

Solution 3

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.

Solution 4

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 );

Solution 5

Instead of typeing <!--Comment--> in the editor for your post, Make sure you place the comment tag inside the raw html editor.

alt text
(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.

Share:
34,214
user249493
Author by

user249493

Updated on July 22, 2022

Comments

  • user249493
    user249493 almost 2 years

    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!