Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WordPress Coding Standards & Best Practices

WordPress Coding Standards & Best Practices

Shawn Hooper

June 16, 2018
Tweet

More Decks by Shawn Hooper

Other Decks in Technology

Transcript

  1. Coding Best Practices Tips & Tricks for Writing Clean Code

    in WordPress Blog - shawnhooper.ca
 Twitter - @shawnhooper
  2. Director of IT at Actionable.
 WordPress Developer. Happy to be

    visiting here from Ottawa. Have been a web developer since 1998, and using WordPress since 2009. shawnhooper.ca Hi, I’m Shawn! Blog - shawnhooper.ca
 Twitter - @shawnhooper
  3. Why Do We Write Clean Code? • It’s easier to

    read • It reduces bugs • It requires less documentation • It reduces technical debt • Be nice to your teammates (code with empathy!) Blog - shawnhooper.ca
 Twitter - @shawnhooper
  4. Why Do We Write Clean Code? Always code as if

    the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
 
 - John Woods Blog - shawnhooper.ca
 Twitter - @shawnhooper
  5. All new or updated code released in WordPress must conform

    with the WCAG 2.0 guidelines at level AA. Accessibility Blog - shawnhooper.ca
 Twitter - @shawnhooper
  6. • Proper Heading Structure • Semantic Markup • wp.a11y.speak() •

    Images & Icons. ALT for Images, <title> in SVG. • Labels mandatory, not required to be visible .screen-reader-text class to hide labels. Accessibility Blog - shawnhooper.ca
 Twitter - @shawnhooper
  7. While we’re here… Add a comma after the last item

    in your array declaration Blog - shawnhooper.ca
 Twitter - @shawnhooper
  8. Braces Use braces instead of single line control structures. Blog

    - shawnhooper.ca
 Twitter - @shawnhooper
  9. Optional Close PHP Tag You don’t need a ?> tag

    at the end of your file. If you, make sure there is no white space after it. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  10. SQL If you have to write SQL Statements, capitalize SQL

    keywords like SELECT, FROM, WHERE, ORDER BY. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  11. SQL If you have to write SQL Statements, capitalize SQL

    keywords like SELECT, FROM, WHERE, ORDER BY. In most cases though, you should use the functions provided by the WPDB Class. or use 
 $wpdb->prepare( $sql, $arg1, $arg2,… ); Blog - shawnhooper.ca
 Twitter - @shawnhooper
  12. There are two hard things in computer science: cache invalidation,

    naming things, and off-by-one errors.
 
 - Phil Karlton Blog - shawnhooper.ca
 Twitter - @shawnhooper
  13. Naming Things function names should be lower case with words

    separated by underscores. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  14. Naming Things Class names should be uppercase with words separated

    by underscores. Acronyms should be uppercase. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  15. Naming Things Constants should be uppercase with words separated by

    underscores. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  16. Naming Things Filenames should be in lowercase, separated by hyphens.

    Classes should be prepended by “class-“ and be named with the name of the class. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  17. Yoda Conditions Variables on the right, constants and literals on

    the left. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  18. Ternary Operators Test that the statement is true, not false.


    
 ! empty() is allowed. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  19. Don’t Extract Don’t use the PHP extract() function to create

    variables from an array. It’s hard to debug. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  20. Naming Conventions Variable and function names should be full words,

    using camel case with a lowercase first letter.
 
 myVariable = ‘value’; Blog - shawnhooper.ca
 Twitter - @shawnhooper
  21. Naming Conventions Constructors intended for use with new should use

    UpperCamelCase:
 
 function MyConstructor( ) …. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  22. Validated All HTML pages should be verified against the W3C

    validator to ensure that the markup is well formed. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  23. Indentation Indent PHP blocks to match HTML. Ident to match

    logical structure. Use Tabs. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  24. Single Purpose Methods Your methods should do one thing, and

    only that one thing. If not:
 
 1. It reduces where it can be used
 2. It becomes harder to test Blog - shawnhooper.ca
 Twitter - @shawnhooper
  25. Keep Methods Short If your method is longer than 20

    lines of code, you can probably split it up into smaller pieces. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  26. Automation There are tools available to help you keep your

    code clean. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  27. PHP CodeSniffer Checks that your code validates to a specified

    PHP Standard! Blog - shawnhooper.ca
 Twitter - @shawnhooper
  28. JSHint Checks that your JavaScript validates to the standard. Run

    with Grunt or Gulp Blog - shawnhooper.ca
 Twitter - @shawnhooper
  29. PHPUnit Unit Testing Framework to perform dynamic tests on your

    PHP Code. Blog - shawnhooper.ca
 Twitter - @shawnhooper
  30. Selenium Used to test your web application in a real

    browser. https://www.shawnhooper.ca/2017/07/selenium-ui-testing- wpcampus/ Blog - shawnhooper.ca
 Twitter - @shawnhooper
  31. Tenon An automated testing tool for web accessibility. Blog -

    shawnhooper.ca
 Twitter - @shawnhooper
  32. Code Reviews 2 sets of eyes are better than one!


    
 Can be done remotely.
 
 Can be baked into pull requests.
 
 Don’t take any criticism personally. Blog - shawnhooper.ca
 Twitter - @shawnhooper