Preferred code style for WordPress themes and plugins?


#1

Hi folks! I was wondering what was the general consensus (or if there is one at all) regarding code style for WordPress themes and plugins. This is a question for both fellow developers and customers. The way I see it now, there are two options (note that these are just my personal opinions):

1. The WordPress guidelines:

https://make.wordpress.org/core/handbook/coding-standards/php/

Pros:

  • Plugin and theme code will use the same style as with the WordPress core. so there will be more consistency.
Cons:
  • A bit outdated. Not used by any other modern projects or frameworks.
  • Lots of spaces everywhere. Hard to apply without a code-formatter (I'm using PhpStorm).

2. The PHP-FIG PSR-x guidelines:

http://www.php-fig.org/psr/psr-1/ (there are also PSR-0, PSR-2, PSR-3, and PSR-4)

Pros:

  • A more modern approach.
  • Used by newer frameworks like Symfony2, Laravel, etc.
  • Voting members from lots of big projects: http://www.php-fig.org/#voting-members

Cons:

  • Very much different from the WordPress guidelines.

So, what’s your opinion on this? Do you prefer the WordPress or the PSR-x style? Or something else?

Cheers,

Liviu


#2

We use a standard that is very similar to PSR-1, with a few exceptions:

  • We don't use PSR-0, as our namespaces are not related to the paths where the classes are located.
  • We don't use lowerCamelCase. I always found it horrible to look at, anyway. Depending on the project and the standard used in it, we either use CamelCase, or lowercase_with_underscore for everything.
  • We always put opening curly braces on the same line as statements or class declarations. It doesn't make much of a difference when reading the code, it's just a habit.

At the end of the day, it should be up to you to use a coding standard that is productive for you and your team. Of course, if you contribute to OS projects, you should try to be consistent with their style, but, for your own products, you are free to choose.