Skip to content

Commit 015ed70

Browse files
authored
Merge pull request #92 from WordPress/php-fix-code-snippets
Fix the code examples
2 parents 57ae45b + ea85a03 commit 015ed70

File tree

1 file changed

+46
-43
lines changed
  • wordpress-coding-standards

1 file changed

+46
-43
lines changed

wordpress-coding-standards/php.md

Lines changed: 46 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ If you want to automatically check your code against this standard, you can use
1515
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:
1616

1717
```php
18-
echo '<a href="/static/link" title="Yeah yeah!">Link name</a>';
19-
echo "<a href='$link' title='$linktitle'>$linkname</a>";
18+
echo '<a href="/static/link" class="button button-primary">Link name</a>';
19+
echo "<a href='{$escaped_link}'>text with a ' single quote</a>";
2020
```
2121

2222
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
119119
<?php if ( have_posts() ) : ?>
120120
<div class="hfeed">
121121
<?php while ( have_posts() ) : the_post(); ?>
122-
<article id="post-<?php the_ID() ?>" class="<?php post_class() ?>">
122+
<article id="<?php echo esc_attr( 'post-' . get_the_ID() ); ?>" class="<?php echo esc_attr( get_post_class() ); ?>">
123123
<!-- ... -->
124124
</article>
125125
<?php endwhile; ?>
@@ -166,7 +166,7 @@ $bar = array(
166166
);
167167
$baz = sprintf(
168168
/* translators: %s: Friend's name */
169-
esc_html__( 'Hello, %s!', 'yourtextdomain' ),
169+
__( 'Hello, %s!', 'yourtextdomain' ),
170170
$friend_name
171171
);
172172

@@ -192,16 +192,18 @@ Correct (Multiline):
192192

193193
```php
194194
function foo() {
195-
?>
196-
<div>
197-
<?php
198-
echo bar(
199-
$baz,
200-
$bat
201-
);
202-
?>
203-
</div>
204-
<?php
195+
?>
196+
<div>
197+
<?php
198+
echo esc_html(
199+
bar(
200+
$baz,
201+
$bat
202+
)
203+
);
204+
?>
205+
</div>
206+
<?php
205207
}
206208
```
207209

@@ -227,14 +229,14 @@ Correct:
227229

228230
```php
229231
<?php ... ?>
230-
<?php echo $var; ?>
232+
<?php echo esc_html( $var ); ?>
231233
```
232234

233235
Incorrect:
234236

235237
```php
236238
<? ... ?>
237-
<?= $var ?>
239+
<?= esc_html( $var ) ?>
238240
```
239241

240242
## Remove Trailing Spaces
@@ -246,12 +248,12 @@ Remove trailing whitespace at the end of each line. Omitting the closing PHP tag
246248
Always put spaces after commas, and on both sides of logical, comparison, string and assignment operators.
247249

248250
```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';
255257
```
256258

257259
Put spaces on both sides of the opening and closing parentheses of control structure blocks.
@@ -292,22 +294,22 @@ $foo = (bool) $bar;
292294
When referring to array items, only include a space around the index if it is a variable, for example:
293295

294296
```php
295-
$x = $foo['bar']; // correct
296-
$x = $foo[ 'bar' ]; // incorrect
297+
$x = $foo['bar']; // Correct.
298+
$x = $foo[ 'bar' ]; // Incorrect.
297299

298-
$x = $foo[0]; // correct
299-
$x = $foo[ 0 ]; // incorrect
300+
$x = $foo[0]; // Correct.
301+
$x = $foo[ 0 ]; // Incorrect.
300302

301-
$x = $foo[ $bar ]; // correct
302-
$x = $foo[$bar]; // incorrect
303+
$x = $foo[ $bar ]; // Correct.
304+
$x = $foo[$bar]; // Incorrect.
303305
```
304306

305307
In a `switch` block, there must be no space between the `case` condition and the colon.
306308

307309
```php
308310
switch ( $foo ) {
309-
case 'bar': // correct
310-
case 'ba' : // incorrect
311+
case 'bar': // Correct.
312+
case 'ba' : // Incorrect.
311313
}
312314
```
313315

@@ -336,8 +338,8 @@ Functions that update the database should expect their parameters to lack SQL sl
336338
`$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 :
337339

338340
```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.
341343

342344
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_title = %s WHERE ID = %d", $var, $id ) );
343345
```
@@ -421,20 +423,20 @@ class Example_Class_Extended { [...] }
421423
Prefer string values to just `true` and `false` when calling functions.
422424

423425
```php
424-
// Incorrect
426+
// Incorrect.
425427
function eat( $what, $slowly = true ) {
426428
...
427429
}
428430
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?
431433
```
432434

433435
PHP only supports named arguments as of PHP 8.0. However, as WordPress currently still supports older PHP versions, we cannot yet use those.
434436
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.
435437

436438
```php
437-
// Correct
439+
// Correct.
438440
function eat( $what, $speed = 'slowly' ) {
439441
...
440442
}
@@ -446,13 +448,14 @@ eat( 'dogfood', 'quickly' );
446448
When more words are needed to describe the function parameters, an `$args` array may be a better pattern.
447449

448450
```php
449-
// Even Better
450451
function eat( $what, $args ) {
451452
...
452453
}
453454
eat ( 'noodles', array( 'speed' => 'moderate' ) );
454455
```
455456

457+
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+
456459
## Interpolation for Naming Dynamic Hooks
457460

458461
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
518521
Correct:
519522

520523
```php
521-
if ( 0 === strpos( 'WordPress', 'foo' ) ) {
522-
echo __( 'Yay WordPress!' );
524+
if ( 0 === strpos( $text, 'WordPress' ) ) {
525+
echo esc_html__( 'Yay WordPress!', 'textdomain' );
523526
}
524527
```
525528

526529
Incorrect:
527530

528531
```php
529532
if ( 0 == strpos( 'WordPress', 'foo' ) ) {
530-
echo __( 'Yay WordPress!' );
533+
echo esc_html__( 'Yay WordPress!', 'textdomain' );
531534
}
532535
```
533536

@@ -538,15 +541,15 @@ Correct:
538541
```php
539542
$data = $wpdb->get_var( '...' );
540543
if ( $data ) {
541-
// Use $data
544+
// Use $data.
542545
}
543546
```
544547

545548
Incorrect:
546549

547550
```php
548551
if ( $data = $wpdb->get_var( '...' ) ) {
549-
// Use $data
552+
// Use $data.
550553
}
551554
```
552555

@@ -556,7 +559,7 @@ In a `switch` statement, it's okay to have multiple empty cases fall through to
556559
switch ( $foo ) {
557560
case 'bar': // Correct, an empty case can fall through without comment.
558561
case 'baz':
559-
echo $foo; // Incorrect, a case with a block must break, return, or have a comment.
562+
echo esc_html( $foo ); // Incorrect, a case with a block must break, return, or have a comment.
560563
case 'cat':
561564
echo 'mouse';
562565
break; // Correct, a case with a break does not require a comment.

0 commit comments

Comments
 (0)