Coding Standards

Validating with PHP Code Sniffer

Instead of reading any of this why not just let the computer nag you? From the Pressbooks plugin directory:

  1. composer install
  2. composer standards

Bonus: You can sometimes automatically fix errors by running:

vendor/bin/phpcbf --standard=phpcs.ruleset.xml /path/to/your/file

Linting Javascript

We use eslint for scripts & styles & ES6 standards. To lint your Javascript and SCSS assets, run npm run lint.

Pressbooks Coding Standards (Mandatory)

We enforce Human Made Coding Standards with the following small tweaks.

  • Use camelCase for class methods & properties, UPPERCASE for class constants, snake_case everywhere else.
  • PHP Sessions are allowed.

Write Classes or Namespaced functions, stay out of global space!

PHP Namespaces have been available since 2009. Namespaces are not a new concept. We use them.

Our namespace is: Pressbooks

  • If your Class isn’t an Object like WP_User, WP_Dependencies, WP_Query etc., write a library of functions.
  • If your Class is a bunch of Static methods and nothing else, write a library of functions.
  • Afraid of function name collisions? See Namespaces.

Pressbooks Coding Recommendations (Optional)

Write accurate PHPDoc styled code comments.

Prefix action and filter hook names with pb_.

Prefix WP Post meta keys with pb_.

Prefix WP User meta keys with pb_.

Prefix WP Option names with pressbooks_.

Theme files are exempt from the above rules, but should still make an effort to follow them.

Edit this post on GitHub.