You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: wordpress-coding-standards/php.md
+26-26Lines changed: 26 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,9 @@ Keep the following points in mind when writing PHP code for WordPress, whether f
6
6
7
7
See also: <ahref="https://developer.wordpress.org/coding-standards/inline-documentation-standards/php/">PHP Documentation Standards</a>.
8
8
9
-
<h2>PHP</h2>
9
+
## PHP
10
10
11
-
<h3>Single and Double Quotes</h3>
11
+
### Single and Double Quotes
12
12
13
13
Use single and double quotes when appropriate. If you're not evaluating anything in the string, use single quotes. You should almost never have to escape quotes in a string, because you can just alternate your quoting style, like so:
Text that goes into attributes should be run through <code>esc_attr()</code> so that single or double quotes do not end the attribute value and invalidate the HTML and cause a security issue. See <ahref="http://codex.wordpress.org/Data_Validation">Data Validation</a> in the Codex for further details.
21
21
22
-
<h3>Indentation</h3>
22
+
### Indentation
23
23
24
24
Your indentation should always reflect logical structure. Use <strong>real tabs</strong> and <strong>not spaces</strong>, as this allows the most flexibility across clients.
25
25
@@ -74,7 +74,7 @@ switch ( $type ) {
74
74
75
75
<strong>Rule of thumb:</strong> Tabs should be used at the beginning of the line for indentation, while spaces can be used mid-line for alignment.
76
76
77
-
<h3>Brace Style</h3>
77
+
### Brace Style
78
78
79
79
Braces shall be used for all blocks in the style shown here:
80
80
@@ -125,17 +125,17 @@ Note that requiring the use of braces just means that <em>single-statement inlin
125
125
<?php endif; ?>
126
126
```
127
127
128
-
<h3>Use <code>elseif</code>, not <code>else if</code></h3>
128
+
### Use <code>elseif</code>, not <code>else if</code>
129
129
130
130
<code>else if</code> is not compatible with the colon syntax for <code>if|elseif</code> blocks. For this reason, use <code>elseif</code> for conditionals.
131
131
132
-
<h3>Declaring Arrays</h3>
132
+
### Declaring Arrays
133
133
134
134
Using long array syntax ( <code>array( 1, 2, 3 )</code> ) for declaring arrays is generally more readable than short array syntax ( <code>[ 1, 2, 3 ]</code> ), particularly for those with vision difficulties. Additionally, it's much more descriptive for beginners.
135
135
136
136
Arrays must be declared using long array syntax.
137
137
138
-
<h3>Closures (Anonymous Functions)</h3>
138
+
### Closures (Anonymous Functions)
139
139
140
140
Where appropriate, closures may be used as an alternative to creating new functions to pass as callbacks. For example:
Closures must not be passed as filter or action callbacks, as they cannot be removed by <code>remove_action()</code> / <code>remove_filter()</code> (see <ahref="https://core.trac.wordpress.org/ticket/46635">#46635</a> for a proposal to address this).
153
153
154
-
<h3>Multiline Function Calls</h3>
154
+
### Multiline Function Calls
155
155
156
156
When splitting a function call over multiple lines, each parameter must be on a separate line. Single line inline comments can take up their own line.
157
157
@@ -176,13 +176,13 @@ $a = foo(
176
176
);
177
177
```
178
178
179
-
<h3>Regular Expressions</h3>
179
+
### Regular Expressions
180
180
181
181
Perl compatible regular expressions (<ahref="http://php.net/pcre">PCRE</a>, <code>preg_</code> functions) should be used in preference to their POSIX counterparts. Never use the <code>/e</code> switch, use <code>preg_replace_callback</code> instead.
182
182
183
183
It's most convenient to use single-quoted strings for regular expressions since, contrary to double-quoted strings, they have only two metasequences: <code>\'</code> and <code>\\</code>.
184
184
185
-
<h3>Opening and Closing PHP Tags</h3>
185
+
### Opening and Closing PHP Tags
186
186
187
187
When embedding multi-line PHP snippets within an HTML block, the PHP open and close tags must be on a line by themselves.
188
188
@@ -217,7 +217,7 @@ if ( $a === $b ) { ?>
217
217
<?php }
218
218
```
219
219
220
-
<h3>No Shorthand PHP Tags</h3>
220
+
### No Shorthand PHP Tags
221
221
222
222
<strong>Important:</strong> Never use shorthand PHP start tags. Always use full PHP tags.
223
223
@@ -235,11 +235,11 @@ Incorrect:
235
235
<?= $var ?>
236
236
```
237
237
238
-
<h3>Remove Trailing Spaces</h3>
238
+
### Remove Trailing Spaces
239
239
240
240
Remove trailing whitespace at the end of each line of code. Omitting the closing PHP tag at the end of a file is preferred. If you use the tag, make sure you remove trailing whitespace.
241
241
242
-
<h3>Space Usage</h3>
242
+
### Space Usage
243
243
244
244
Always put spaces after commas, and on both sides of logical, comparison, string and assignment operators.
When formatting SQL statements you may break it into several lines and indent if it is sufficiently complex to warrant it. Most statements work well as one line though. Always capitalize the SQL parts of the statement like <code>UPDATE</code> or <code>WHERE</code>.
331
331
@@ -344,13 +344,13 @@ $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID
344
344
345
345
See <atitle="Data Validation"href="http://codex.wordpress.org/Data_Validation"target="_blank">Data Validation</a> in the Codex for more information.
346
346
347
-
<h3>Database Queries</h3>
347
+
### Database Queries
348
348
349
349
Avoid touching the database directly. If there is a defined function that can get the data you need, use it. Database abstraction (using functions instead of queries) helps keep your code forward-compatible and, in cases where results are cached in memory, it can be many times faster.
350
350
351
351
If you must touch the database, get in touch with some developers by posting a message to the <atitle="wp-hackers mailing list"href="http://codex.wordpress.org/Mailing_Lists#Hackers"target="_blank">wp-hackers mailing list</a>. They may want to consider creating a function for the next WordPress version to cover the functionality you wanted.
352
352
353
-
<h3>Naming Conventions</h3>
353
+
### Naming Conventions
354
354
355
355
Use lowercase letters in variable, action/filter, and function names (never <code>camelCase</code>). Separate words via underscores. Don't abbreviate variable names unnecessarily; let the code be unambiguous and self-documenting.
356
356
@@ -391,7 +391,7 @@ Files containing template tags in <code>wp-includes</code> should have <code>-te
391
391
general-template.php
392
392
```
393
393
394
-
<h3>Only one object structure (class/interface/trait) should be declared per file</h3>
394
+
### Only one object structure (class/interface/trait) should be declared per file
395
395
396
396
For instance, if we have a file called `class-example-class.php` it can only contain one class in that file.
397
397
@@ -414,7 +414,7 @@ class Example_Class { [...] }
414
414
class Example_Class_Extended { [...] }
415
415
```
416
416
417
-
<h3>Self-Explanatory Flag Values for Function Arguments</h3>
417
+
### Self-Explanatory Flag Values for Function Arguments
418
418
419
419
Prefer string values to just <code>true</code> and <code>false</code> when calling functions.
420
420
@@ -450,7 +450,7 @@ function eat( $what, $args ) {
Where possible, dynamic values in tag names should also be as succinct and to the point as possible. <code>$user_id</code> is much more self-documenting than, say, <code>$this->id</code>.
466
466
467
-
<h3>Ternary Operator</h3>
467
+
### Ternary Operator
468
468
469
469
<atitle="Ternary"href="http://en.wikipedia.org/wiki/Ternary_operation"target="_blank">Ternary</a> operators are fine, but always have them test if the statement is true, not false. Otherwise, it just gets confusing. (An exception would be using <code>! empty()</code>, as testing for false here is generally more intuitive.)
// (if field is not empty ) ? (do this) : (else, do this);
479
479
```
480
480
481
-
<h3>Yoda Conditions</h3>
481
+
### Yoda Conditions
482
482
483
483
```php
484
484
if ( true === $the_force ) {
@@ -494,7 +494,7 @@ A little bizarre, it is, to read. Get used to it, you will.
494
494
495
495
This applies to ==, !=, ===, and !==. Yoda conditions for <, >, <= or >= are significantly more difficult to read and are best avoided.
496
496
497
-
<h3>Clever Code</h3>
497
+
### Clever Code
498
498
499
499
In general, readability is more important than cleverness or brevity.
500
500
@@ -570,27 +570,27 @@ The <code>goto</code> statement must never be used.
570
570
571
571
The <code>eval()</code> construct is <em>very dangerous</em>, and is impossible to secure. Additionally, the <code>create_function()</code> function, which internally performs an <code>eval()</code>, is deprecated in PHP 7.2. Both of these must not be used.
572
572
573
-
<h3>Error Control Operator <code>@</code></h3>
573
+
### Error Control Operator <code>@</code>
574
574
575
575
As noted in the <ahref="http://www.php.net//manual/en/language.operators.errorcontrol.php">PHP docs</a>:
576
576
<blockquote>PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.</blockquote>
577
577
While this operator does exist in Core, it is often used lazily instead of doing proper error checking. Its use is highly discouraged, as even the PHP docs also state:
578
578
<blockquote>Warning: Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.</blockquote>
579
579
580
-
<h3>Don't <code>extract()</code></h3>
580
+
### Don't <code>extract()</code>
581
581
582
582
Per <atitle="Remove all, or at least most, uses of extract() within WordPress"href="https://core.trac.wordpress.org/ticket/22400">#22400</a>:
583
583
<blockquote><code>extract()</code> is a terrible function that makes code harder to debug and harder to understand. We should discourage it's [sic] use and remove all of our uses of it.
584
584
585
585
Joseph Scott has <aclass="ext-link"href="https://blog.josephscott.org/2009/02/05/i-dont-like-phps-extract-function/">a good write-up of why it's bad</a>.</blockquote>
<li>November 13, 2013: <ahref="http://make.wordpress.org/core/2013/11/13/proposed-coding-standards-change-always-require-braces/">Braces should always be used, even when they are optional</a></li>
0 commit comments