Using PHP Standards in WordPress

Home / Guides / Building a Plugin for WordPress / Using PHP Standards in WordPress
Lesson 1 of 3
6m read.
Advanced
6m video.

WordPress powers over 50% of the internet.

According to W3Techs, WordPress is currently used by 60.1% of all websites. This number is only going to continue to grow in the coming years, making it the dominant CMS in 2022. If you’re looking to create a plugin that is compliant with WordPress standards, PHP 8.0 and higher is the target to aim for.

PHP 5 was released in 2004 and WordPress was released in 2003. Over the past 15 years, PHP and WordPress have both evolved and changed to meet the needs of users. PHP 8.0 was released in December of 2018 and offers many new features and improvements that make it an ideal target for WordPress development.

How can you make your plugins more maintainable?

How can you ensure what you are looking at today will work into the future for PHP?

One of the main benefits of following PHP standards is that your code becomes more maintainable. This is because the standard is a set of guidelines that are followed by the majority of developers. By conforming to these standards, you make it easier for other developers to understand your code and work with it.

In addition, following PHP standards can help you avoid common errors and ensure that your code is compatible with the latest versions of PHP and WordPress.

Use PHP standards.

At MRK we have gone from various setups over the years to moving our code to standards.

PHP has a number of different standards that cover various aspects of coding. The most popular standard is the PHP Coding Standard, which was created and is maintained by the PHP Standards Group. This standard defines a set of guidelines for how to code PHP in a consistent and error-free way.

The PHP Coding Standard is based on the PSR-1 and PSR-2 standards, which were created by the PHP Framework Interoperability Group (PHP-FIG). These standards define a set of rules for writing PHP code that is both consistent and easy to read.

The WordPress standard is based on the PHP Coding Standard, and offers specific guidelines for coding WordPress plugins. By following these standards, you can ensure that your code is compatible with WordPress and meets the expectations of other developers.

Why we started using PSR standards

  1. Our commercial plugins were using a pimple container that’s not a standard
  2. The CSS and JavaScript made use of jQuery and other libraries. Often messy.
  3. These plugins have become hard to maintain and hard to fix bugs.
  4. Lack of comments and clear structure in these plugins means its hard to work out why something is broken and work fast for adding necessary features.
  5. A container class and overall code was confusing.

The results of the our old code method:

  • Hard to maintain
  • Hard to add developers to the team
  • Hard to add new features without breaking code

Its was HARD

The answer is a better developer experience by using PHP Standards.

Now that we are using these new methods we had a couple of actions points within our team.

  • Rewrite  our commercial plugins with standards
  • Improve developer experience
  • Have proven design patterns
  • Have a documentation standard.

How do we do reach these goals?

  1. Use PSR Standards
  2. The PSR4 Autoloader
  3. The PSR5 Documentation standard.
  4. Composer for Libraries

The WordPress Coding Standards

The WordPress coding standard is a guideline for how to code your plugins and themes. It can also cover other contributions to the WordPress Core.

WordPress was written for PHP 4 in 2003, this means the standards have changed a lot over the years. As PHP versions have changed so has WordPress and the way we are writing source code.

PHP 8.0 and higher is our current target for WordPress and Optimized Plugins in 2022. When you are writing code for WordPress its a good idea to check that it works on the support standard for WordPress.

One thing with WordPress is it does not have many interfaces or traits that are part of modern PHP. Even ternary operators are avoided in the code base.

Easy to read code is always chosen over fancy statements. This is great for somethings, however other areas, like formatting SQL statements is a long way behind other PHP frameworks like Laravel. Separate words for functions and classes are useful but in some cases are conflicting with the PHP standards.

This means there are currently no guidelines for PSR and WordPress.

Important Features of the WordPress Standard

  1. Opening and Closing PHP Tags
  2. No shorthand PHP Tags
  3. Class Names should be separated by Underscores (Not PSR4 friendly)
  4. Closures should not be used for Actions or Filters
  5. Remove trailing spaces from any block of code.
  6. How to name your function arguments

What is the WordPress community doing about updating standards?

Its an ongoing work but its been hard to get consensus.

The best thing about WordPress is its open source and flexible enough to implement what you want.

To learn more about this you can read these 2 great articles.

Don’t Follow WordPress Coding Standards

In this article Josh Pollock explores over why the PHP WordPress standard is outdated. The article is old but still worth reading.

Improving WordPress Code With Modern PHP

Improving WordPress Code With Modern PHP is possible and Leo breaks this down really well.

What is a PSR?

  • PSR’s are a coding standard for PHP
  • They are not directly implemented by WordPress
  • A PSR allows libraries to be used together

You can learn more about the PSR guidelines on the website PHP-FIG.

Example PSR’s

Our most used PSRs at MRK WP are:

PSR-4 – The autoloader

Benefits : 

  • Can load classes using composer.
  • Has namespaces.
  • Means you no longer need to require or include files in your directory structure.

You can learn more about the PSR-4 standard on the PHP-FIG website.

PSR-5 – Documentations standard

Benefits : 

  • Auto complete for docs in VS Code
  • Comments work with intellisense
  • Can be used to autocomplete documentation

PSR-5 is a draft standard.

Outcomes

By using these standards we get a core style of code.

Most of our plugins are easy to understand and have become easy for us to onboard new WordPress Developers especially in the PHP space.

Using these standards we get easier to maintain and longer life of code.

Overall its taken a lot of effort to migrate to a new standard but over time, it will save us a significant amount of money from reducing code rewrite and increasing code reuse.

Training has also been reduced because we have more resources to leverage for our team.