How to comment out code in WordPress theme templates

The WordPress publishing platform is built on PHP and the underlying theme templates that control the way your blog or website look when published also employ PHP code which produces HTML. This HTML is what visitors see rendered by the browser when they visit your site. What this means is, if you want to comment out sections of code in your theme templates you can either use traditional HTML commenting techniques such as <!–– ––> (shown at the bottom of this image in green) or you can comment out sections of PHP using // for a single line or /* */ for multiple lines. This image shows the source code of a blog entry from one of my WordPress sites using the single.php template file:

So what’s the difference between HTML commenting and PHP commenting? Well, when you comment out HTML you can still see the code when you view the source of your web page. You can see it and so can search engines! By contrast, when you comment out bits of PHP, nothing is printed to the HTML that is produced. Using the image above as an example, we can see the commented out code because we used HTML commenting. If we had used PHP commenting, that bit of code wouldn’t be rendered in HTML at all and you wouldn’t be able to see it in the source code. This means that the web page would produce cleaner, shorter code overall and I imagine this technique has the added benefit of search engine optimization in that key word density is increased and potential validation errors are avoided.

Think about it. PHP code is sent to an interpreter before producing HTML which is then rendered by the web browser. Plain HTML is rendered directly and therefore nothing can be removed entirely… just hidden from rendering. When you comment out code in PHP the end user never sees it because it isn’t interpreted into HTML at all… Instead of commenting out bits of unused code, maybe pieces you want to save but don’t want showing up to end users, you can can now use PHP commenting and wind up with cleaner web pages!

That pretty much sums it up but keep in mind as you start using this technique you may have to scan your HTML, inline CSS and JavaScript for disruptive commenting. This means that within your commented out section there is another bit of commenting (which breaks the flow). If this happens and you don’t catch it you’ll get an error like this:

Parse error: syntax error, unexpected ‘=’ in /home/folder/html/folder/wp-content/themes/theme/single.php on line 29

So how do we comment out code in PHP? First you have to make sure the section of code you want to hide is actually PHP! You also have to make sure you aren’t encapsulating other bits of PHP along the way. My advice is to comment out small bits and then expand as you test that it is working properly. Let’s pretend we’re commenting out a linked image in your code and that it’s HTML. It would look like this <a href=”testing”><img src=’proof’></a>

  1. Wrap the code in PHP tags like this <?php <a href=”testing”><img src=’proof’></a> ?>
  2. Add PHP commenting <?php /* <a href=”testing”><img src=’proof’></a> */ ?>
  3. That’s it, now this link and image code won’t show up at all to visitors or search engines but it will still be retained in your Appearance > Editor area of WordPress for future reference or use

If you have any errors you should make sure you aren’t overlapping other PHP, don’t have interspersed commenting like /* for CSS or JavaScript and you can also employ escaping by using the back slash character like this before any troublesome characters like quote and double quote inside of PHP echo statements.

Check out the official WordPress codex entry on commenting for HTML, CSS and PHP and here’s a similar overview at About.com. As a side note, here is a list of escape sequences for PHP that can be used with the echo command and double quotes. In PHP, anything echoed in double quotes is interpreted whereas single quotes is not. So something like <?php echo “testing mom’s pudding”; ?> would fail without a forward slash in front of the apostrophe s.

  • ” – print the next character as a double quote, not a string closer
  • ‘ – print the next character as a single quote, not a string closer
  • n – print a new line, outputs <br /> in html
  • t – print a tab character
  • r – print a carriage return in code
  • $ – print the next character as a dollar, not as part of a variable
  • \ – print the next character as a backslash, not an escape character

As always, visit www.php.net/manual/ for more information and good luck! Here’s another overview of PHP escape characters in case you’re interested www.tuxradar.com/practicalphp/2/6/2