Daily Tip: How to Comment Out Code in WordPress
May 21, 2011
Here is a basic tip for working with WordPress files. “Commenting out” code is a way making a section or block of code inactive. It can be handy, because there are times when you may want to temporarily comment code out but leave it in there so that you can use it later. It essentially hides the code until you remove the comment code.
Depending on what type of file you’re working in, there are different bits of code you’ll need to use. These examples are taken directly from the WordPress codex:
To comment out HTML:
<!-- <div class="redblock"><p>This is some HTML in the redblock class.</p></div> -->
CSS file example:
/* .redblock {font-size: 80%; font-style: italic; color: red;} */
Within PHP tags:
<?php // the_content(Continue Reading...); ?>
or
<?php /* the_content(Continue Reading...); */ ?>
This is a very basic skill that you will want to have under your belt when working with WordPress files. For more information about commenting code, please refer to the WordPress codex.



HA! I thought I might run into that problem. Let me try to rewrite a different example block:
<!– –>
That would be a loop to print variables in an unordered list, commenting out PHP and HTML, individually… unless of course this one doesn’t display either. In which case I’ll just give up and get back to work…
Additionally keep in mind that code may be mixed, and commenting for each “language” may be necessary.
For example, a section of PHP that sets a variable,and then displays it in an H1 tag will need to contain comment marks for the PHP and the HTML, like so:
<!– –>
Simply putting /* block comments */ around the wrapping PHP tags won’t prevent those list items in HTML from being displayed.
Now, code should ideally be written to reduce the chance of a situation like this (using “echo” or something of the like), but it *is* a fairly common occurrence.