On Github davetgreen / wp-coding-standards-talk
OCD so I love clear structures and standards.
Work on more team projects than ever.
Spending a lot more time writing plugins.
Trying to increase my contributions to core.
Make Do work with a lot of other agencies.
Have used PSR2 standards in the past.
Switched for PHP coding in September 2015.
In the process of switching for Javascript.
All new or updated code released in WordPress must conform with the WCAG 2.0 guidelines at level AA.
Core Handbookif ( 'literal' == $variable ) { if ( true == $variable ) { if ( function() == $variable ) {
$variable = 'wpcs'; if ( $variable == 'wpcs' ) { $result = 'Huzzah!'; }
Here we're checking to see if the variable is equal to the string literal. In this case the expression resolves as truthy, and we get the desired result.
$variable = 'ooops!'; // Ooops, we lost an equals! if ( $variable = 'wpcs' ) { $result = 'Huzzah!'; }
Ommitting the second comparison operator (equals) means that this expression silently resolves as truthy, and is a bug that may take you several minutes to fix!
$variable = 'ooops!'; if ( 'wpcs' = $variable ) { $result = 'Huzzah!'; }
By reversing the expression, an ommission of the second comparison operator (equals) will produce an error that you can fix immediately. Huzzah!
Parse error: syntax error, unexpected '=' in /example.php on line 5
Yoda Conditions should only be used with the following comparison operators: ==, !=, ===, and !==.
/** * A description of what purpose this * file serves in the theme/plugin. * * @package Example_Theme */
/** * Describe the function's purpose. * @param int $id Post ID. * @param array $args Arguments. * @return string Some text. */ function do_something( $id, $args ) {
Here we check to see if the input variable is a string as expected, before adding it to an array.
$things = array(); if ( isset( $_POST['foo'] ) { $foo = $_POST['foo']; if ( 'string' === gettype( $foo ) ) { $things[] = $foo; } }
Here we validate then sanitize an input variable before using it to update post meta.
if ( isset( $_POST['foo'] ) ) { $foo = sanitize_text_field( $_POST['foo'] ); update_post_meta( $id, 'foo_meta', $foo ); }
Here we output a validated and sanitized input variable, making sure it has been properly escaped.
if ( isset( $_POST['foo'] ) { $foo = sanitize_text_field( $_POST['foo'] ); echo '<h1>' . esc_html( $foo ) . '</h1>'; }
(Caching and performance optimisation FTW!)
Lets get all posts with an ID less than 99.
global $wpdb; $posts = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE `id` < 99", OBJECT );
Pass an array of IDs into a new WP_Query and voila!
$ids = range(1, 98); $args = array( 'post__in' => $ids, ); $my_query = new WP_Query( $args );
Install PHPCS on your machine using PEAR or Composer. Homebrew is an alternative for Mac OSX users.
Add the path to the phpcs command to your system PATH, transforming it into a global installation.
Open your Bash profile, append the path to phpcs, save the file, reload it and then echo the PATH to test it.
nano ~/.bash_profile
export PATH=$PATH:/usr/local/bin/phpcs
CRTL+X
source ~/.bash_profile
echo $PATH
Download the WPCS rules for PHPCS to your machine using Git, Pear, Composer or the old fashioned way.
Place them in a suitable directory and make a note of the location. Mine are in: /Users/davetgreen/.wpcs
Or any similar software that either has a PHP Code Sniffer plugin available or comes with built-in support.
$ phpcs --standard=WordPress test.php
// A quick example if($force == "with you") { $victorious = you_will($be); }
Inline comments must end in full stops.
Expected 1 space after IF keyword.
No space after opening parenthesis is prohibited.
Use Yoda Condition checks, you must.
String "with you" does not require double quotes.
No space before closing parenthesis is prohibited.
Line indented correctly; expected 1 tabs, found 0.
Expecting one spaces before closing bracket: 0 found.
// A quick example if($force == "with you") { $victorious = you_will($be); }
// A quick example. if ( 'with you' == $force ) { $victorious = you_will( $be ); }
Don't try to switch for all languages/standards at once.
Gradually increase the amount of rules you comply with.
Mistakes are going to be made, it's part of the process.
Get help with the initial set-up if needed: it's worth it.
Productivity is key, don't let standards get in the way.
Encourage other WordPress developers to switch!
Code like you've never coded before!
Core Handbook: Coding Standards https://make.wordpress.org/core/handbook/best-practices/coding-standards
Validating Sanitizing and Escaping User Data https://codex.wordpress.org/Validating_Sanitizing_and_Escaping_User_Data
GitHub: PHP_CodeSniffer https://github.com/squizlabs/PHP_CodeSniffer#installation
GitHub: WordPress Coding Standards https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards