Cheat Sheets

The WordPress Coding Standards: Single Quotes and Double Quotes


In this series, we're taking a look at the WordPress PHP Coding Standards in order further understand how quality WordPress code should be written.

Sure, all of this is documented in the WordPress Coding Standards and it's a site that every WordPress developer should have bookmarked and on hand when working on a theme, a plugin, or an application; however, if you're just getting into WordPress development, then it's important to understand the rationale as to why the conventions are the way they are.

In this article, we're going to be taking a look at the use of single quotes and double quotes specifically when dealing with strings.

This may be the shortest, most straightforward article in the series, but it should cover some important nuances as it relates to working with single quotes, double quotes, and strings in WordPress.

Strings in PHP and Strings in WordPress

Before we actually discuss strings as they relate to WordPress, it's important to understand how the PHP language interprets strings when they are single quoted or double quoted.

Single Quotes in PHP

First, the simplest, most straightforward way to define a string in PHP is to simply wrap it with single quotes (that is, the ' character).

As with most programming languages, there are ways to escape characters so that you're able to write out a string literal. For example, if you wanted to write: "String's in PHP are easy," as a string, then you may do this:

'String\'s in PHP are easy.'

See? The backslashes will instruct PHP to write out the single quote rather than terminating the actual string.

The second thing to note is that if you have a variable, it will not be replaced when quoted in single quotes. For example, assume that you have a variable called $name and it contains the value "Envato".

More specifically, in code, this would look like this:

$name = 'Envato'

If you were to embed the $name variable into another string, 'Envato' would not be replaced.

$name = 'Envato';
echo 'I\'m writing for $name. It\'s a lot of fun.';

This would just echo: I'm writing for $name. It's a lot of fun.

Here is where double-quotes begin to come in handy.

Double Quotes in PHP

Straight from the PHP manual:

If the string is enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters.

You can read more about the escape characters and other information in the PHP manual, but let's say we were to rework some of the strings that we've defined above.

Our first example, that is the one with the escaped single quote, could now be defined like this:

"String's in PHP are easy."

Our second example could be taken a step further: Not only is the escape sequence no longer needed, but the variable will also be evaluated:

$name = 'Envato';
echo "I'm writing for $name. It's a lot of fun.";

The second line will literally echo: I'm writing for Envato. It's a lot of fun.

So with this covered as an introduction, let's talk about the specifics of strings in WordPress. The truth is, if you have a strong grasp on all of the above, there's not much more to add other than a few one-off cases.

Single Quotes in WordPress

In keeping consistent with PHP coding conventions, the general rule of thumb to follow is to always use single quotes to define your strings.

Since much of the work done in WordPress also includes writing out markup within a PHP string, it's best to place those strings in single quotes so that the attributes of the HTML element can be enclosed in double quotes.

For example:

$html = '<a href="" target="_blank">WordPress</a>';

Obviously, the above makes for a much cleaner reading experience than having to place several escape characters in to handle the double quotes, the slashes, and so on.

If you're dealing with markup that needs to span multiple lines or where you're creating several elements some of which are child elements of others, I always recommend indenting the code so that it reads like an HTML document would.

For example:

$name = 'Tom McFarlin';

$html = '<div id="container">';
	$html .= '<span>' . $name . '</span>';
$html .= </div><!-- /#container -->';

It's important to note that this isn't part of the WordPress Coding Standards - this convention is one that I simply find useful when it comes to maintaining code - however, recall that single quoted strings do not evaluate the value of a variable.

As such, it is important to note that, in this case, we're using string concatenation to include the variable definition.

Double Quotes in WordPress

Just as with double quotes in PHP, it's important to note that there are times in which it's more preferable to use them especially when you need to evaluate a variable.

In using the example from the code above, we would adapt it as follows:

$name = 'Tom McFarlin';

$html = "<div id='container'>";
  $html .= "<span>$name</span>";
$html .= "</div><!-- /#container -->";

Notice that we replaced all of the single quotes with double quotes and that we no longer have to perform any string concatenation with the $name variable since it will be evaluated.

It's also important to note that although it's arguably more common to see double quotes applied to HTML attributes, single quotes work just fine as well, especially in scenarios like this.

But There Are Exceptions

When working with strings and various programming languages - namely HTML - it can get a little complicated in terms of how you're nesting your code.

For example, say that you're using PHP to write out JavaScript which is responsible for rendering some HTML. In this scenario, you're going to be writing a lot of quotes.

Though it's possible to simply alternate your quoting styles, this won't work 100% of the time.

Even the Coding Standards state:

An exception to this is JavaScript, which sometimes requires double or single quotes.

Although there are times where you may not be able to do this, this reinforces the idea that we should strive to keep each programming language in its own file style and include them when and where necessary.

Are There Any Security Issues?

Yes, but WordPress makes them extremely easy to manage, and though this technically falls under data validation, it's closely related to exactly what we're discussing in this article.

The primary problem is that there are times where we may be dynamically writing out HTML attributes to our markup that have values we can't anticipate. In cases like this, we need to be able to escape said data.

Fortunately, WordPress provides the esc_attr function. Simply put, this function will take an incoming string and encode the characters to make sure they are rendered to the browser properly.

Practically speaking, you would use it exactly like the Codex demonstrates:

echo '<input type="text" name="fname" value="' . esc_attr( $_POST['fname'] ) . '">';

For more information, be sure to review the Codex article.


So, as a general rule of thumb, you should stick with using single-quotes unless you're going to be evaluating a string or you're going to be returning and echo'ing HTML, JavaScript, or potentially even CSS back to the caller.

Just as the Coding Standards state:

You should almost never have to escape quotes in a string, because you can just alternate your quoting style.

So as you're working with strings in your future work, keep these principles in mind and it could go along way to making sure that your strings are as clean, and easy to understand as possible within the context of your WordPress-based projects.

Related Posts
  • Code
    Theme Development
    Custom Controls in the Theme CustomizerTheme customizer custom control 400
    In the last article, we explored the advanced controls available in the Theme Customizer, and how to implement them. We’re going to look at how to create our own custom control, allowing you to choose which Category of Posts are displayed on the home page. To get started, download version 0.6.0 of our Theme Customizer Example.Read More…
  • Code
    Mastering WordPress Meta Data: Working With LoopsMetadata
    In the first two parts of this series, we covered what meta data is in WordPress and how to work with the arrays that are typically returned. Now that you've learned to do the detective work necessary to find the structure of an array, it's time to learn to use loops to automate the process of outputting an array. Once you learn this important skill you will never have to write repetitive HTML markup again.Read More…
  • Code
    Theme Development
    Creating a WordPress Theme from Static HTML: Creating an Archive TemplateCreating wordpress theme from html 400
    If you've been working your way through this series, you now have a functioning theme with two page templates. The steps I've demonstrated to this point are: preparing your markup for WordPress converting your HTML to PHP and splitting your file into template files editing the stylesheet and uploading your theme to WordPress adding a loop to your index file adding meta tags, the wp_head hook and the site title and description to your header file adding a navigation menu adding widget areas to the header and sidebar adding widget areas, a colophon and the wp_footer hook to the footer file creating template files for static pages. Read More…
  • Code
    Creative Coding
    Using WordPress For Web Application Development: Available Features, Part 5 - Retrieving DataApplication foundation 400
    By now, you know that the purpose of this series is to demonstrate how WordPress can be used as a foundation for web application development. We started by taking a high-level look at many web application design patterns, how WordPress differs, and why WordPress should be considered to be more of a foundation rather than a framework.Read More…
  • Web Design
    Walk Users Through Your Website With Bootstrap TourTour retina
    When you have a web application which requires some getting used to from your users, a walkthrough of the interface is in order. Creating a walkthrough directly on top of the interface makes things very clear, so that's what we're going to build, using Bootstrap Tour.Read More…
  • Code
    Cheat Sheets
    The WordPress Coding Standards: Bringing It All TogetherThe wordpress coding standards
    When it comes to writing a series of blog posts, one of the most challenging aspects as a reader is actually keeping up with every post that is published. Even if you do manage to try and keep up, posts that are in excess of 1,000 words - especially those that include code - can take time that many of us don't have especially when it comes to juggling our work lives, home lives, hobbies, and other things. So in order to make sure that the information presented throughout this series is still presented in a digestible way, I thought I'd experiment with doing a summary of the entire series. That way, for those of you who have missed an article or haven't had the time to sit down and go through each article, can still get the gist of each point mentioned throughout the articles. With that said, let's take a look at everything we covered when reviewing the WordPress Coding Standards.Read More…