rtCamp notes, day 20 of undefined

Coding standards

Some quick notes for the WordPress Coding standards (PHP)

  • php opening and closing tags must be on a line by themselves.
  • don’t use short php tags (<? ?>)
  • If our code is not processing any variable or information, use single quotes, else use double quotes.
  • We can also do like below

Instead of this

echo "This is a test string with $number number";

We can do

echo 'This is a test string with {$number} number';
  • Use lowercase in variables, actions & filter, function names
  • DON’T USE CAMEL CASE
  • Prepend class- to class file names and format should be class-{class_name}.php
  • USE SPACE -> USE SPACE TO MAKE YOUR LOGICS STAND OUT
  • For e.g.
function( $param1, $param2 );

if( ! empty( $string ) ) {

}
  • For array use space only if its a variable in index.

Indentation

  • Code must look like it’s together, For e.g.

Look at the extra space added to make it match with the indentation.

Braces

  • Braces should be used even if they are not required. i.e.
  • Inline control structure is not allowed.

Array

  • Describe array with the long array structure i.e. array()
  • Better readability and better support

  • Main thing, do validation and sanitization everywhere, never trust user input.

Leave a Reply

Your email address will not be published. Required fields are marked *