Write accurate PHPDoc styled code comments.
Write real Classes or Namespaced functions.
PHP Namespaces have been available since 2009. Namespaces are not a new concept. We use them.
Our namespace is:
- If your Class isn’t an Object like
\WP_Queryetc., 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.
Class methods are
Rules of thumb:
getFooBar()is when you are getting something associated with an object – it implies the set is already defined.
loadFooBar()is when you are loading from an external source, like a file or a database.
hasFooBar()is when a method returns a boolean value.
createFooBar()is when a method creates new files or assets.
public function foo(), just write
function foo(). Public is implied.
Order of methods in a class:
Prefix WP Post meta keys with
Prefix WP User meta keys with
Prefix WP Option names with
themes-root/ are exempt from the above rules, but should still make an effort to follow them.
For everything else not mentioned we respect the WordPress coding standards.
Validating with PHP Code Sniffer
From the Pressbooks plugin directory:
composer install --dev