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
+46-43Lines changed: 46 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,8 +15,8 @@ If you want to automatically check your code against this standard, you can use
15
15
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:
echo "<ahref='{$escaped_link}'>text with a ' single quote</a>";
20
20
```
21
21
22
22
Text that goes into HTML or XML attributes should be escaped so that single or double quotes do not end the attribute value and invalidate the HTML, causing a security issue. See [Data Validation](https://developer.wordpress.org/plugins/security/data-validation/) in the Plugin Handbook for further details.
@@ -119,7 +119,7 @@ Note that requiring the use of braces means that _single-statement inline contro
@@ -246,12 +248,12 @@ Remove trailing whitespace at the end of each line. Omitting the closing PHP tag
246
248
Always put spaces after commas, and on both sides of logical, comparison, string and assignment operators.
247
249
248
250
```php
249
-
x === 23
250
-
foo && bar
251
-
! foo
252
-
array( 1, 2, 3 )
253
-
$baz . '-5'
254
-
$term .= 'X'
251
+
SOME_CONST === 23;
252
+
foo() && bar();
253
+
! $foo;
254
+
array( 1, 2, 3 );
255
+
$baz . '-5';
256
+
$term .= 'X';
255
257
```
256
258
257
259
Put spaces on both sides of the opening and closing parentheses of control structure blocks.
@@ -292,22 +294,22 @@ $foo = (bool) $bar;
292
294
When referring to array items, only include a space around the index if it is a variable, for example:
293
295
294
296
```php
295
-
$x = $foo['bar']; // correct
296
-
$x = $foo[ 'bar' ]; // incorrect
297
+
$x = $foo['bar']; // Correct.
298
+
$x = $foo[ 'bar' ]; // Incorrect.
297
299
298
-
$x = $foo[0]; // correct
299
-
$x = $foo[ 0 ]; // incorrect
300
+
$x = $foo[0]; // Correct.
301
+
$x = $foo[ 0 ]; // Incorrect.
300
302
301
-
$x = $foo[ $bar ]; // correct
302
-
$x = $foo[$bar]; // incorrect
303
+
$x = $foo[ $bar ]; // Correct.
304
+
$x = $foo[$bar]; // Incorrect.
303
305
```
304
306
305
307
In a `switch` block, there must be no space between the `case` condition and the colon.
306
308
307
309
```php
308
310
switch ( $foo ) {
309
-
case 'bar': // correct
310
-
case 'ba' : // incorrect
311
+
case 'bar': // Correct.
312
+
case 'ba' : // Incorrect.
311
313
}
312
314
```
313
315
@@ -336,8 +338,8 @@ Functions that update the database should expect their parameters to lack SQL sl
336
338
`$wpdb->prepare()` is a method that handles escaping, quoting, and int-casting for SQL queries. It uses a subset of the `sprintf()` style of formatting. Example :
337
339
338
340
```php
339
-
$var = "dangerous'"; // raw data that may or may not need to be escaped
340
-
$id = some_foo_number(); // data we expect to be an integer, but we're not certain
341
+
$var = "dangerous'"; // Raw data that may or may not need to be escaped.
342
+
$id = some_foo_number(); // Data we expect to be an integer, but we're not certain.
341
343
342
344
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) );
343
345
```
@@ -421,20 +423,20 @@ class Example_Class_Extended { [...] }
421
423
Prefer string values to just `true` and `false` when calling functions.
422
424
423
425
```php
424
-
// Incorrect
426
+
// Incorrect.
425
427
function eat( $what, $slowly = true ) {
426
428
...
427
429
}
428
430
eat( 'mushrooms' );
429
-
eat( 'mushrooms', true ); // what does true mean?
430
-
eat( 'dogfood', false ); // what does false mean? The opposite of true?
431
+
eat( 'mushrooms', true ); // What does true mean?
432
+
eat( 'dogfood', false ); // What does false mean? The opposite of true?
431
433
```
432
434
433
435
PHP only supports named arguments as of PHP 8.0. However, as WordPress currently still supports older PHP versions, we cannot yet use those.
434
436
Without named arguments, the values of the flags are meaningless, and each time we come across a function call like the examples above, we have to search for the function definition. The code can be made more readable by using descriptive string values, instead of booleans.
Be careful when using this pattern, as it can lead to "Undefined array index" notices if input isn't validated before use. Use this pattern only where it makes sense (i.e. multiple possible arguments), not just for the sake of it.
458
+
456
459
## Interpolation for Naming Dynamic Hooks
457
460
458
461
Dynamic hooks should be named using interpolation rather than concatenation for readability and discoverability purposes.
@@ -518,16 +521,16 @@ Unless absolutely necessary, loose comparisons should not be used, as their beha
0 commit comments