Using PHP Standards in WordPress

by | Aug 7, 2022 | 0 comments

Using PHP Standards in WordPress

by | Aug 7, 2022 | Free Wordpress Plugins | 0 comments



WordPress is the dominant CMS in 2022
It’s a long lived platform
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?

Use PHP standards.

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

Why we started using PSR standards

Our commercial plugins were using a pimple container that’s not a standard
The CSS and Javascript made use of jquery and other libraries. Often messy.
These plugins have become hard to maintain and hard to fix bugs.
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.
A container class and overall code was confusing.

The results of the code:

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

Its was HARD

Answer is a better developer experience

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

How do we do this?
Use PSR Standards
The PSR4 Autoloader
The PSR5 Documentation standard.
Composer for Libraries

Materials

The WordPress Standard
The WordPress standard is a guideline for how to code your plugins.

WordPress was written for PHP 5

PHP 8.0 and higher is our current target for WordPress and Optimized Plugins

You can read more about this in these articles written by Josh Pollock and Leonardo Losoviz
Important Features of the WordPress Standard
Opening and Closing PHP Tags
No shorthand PHP Tags
Class Names should be separated by Underscores (Not PSR4 friendly)
Closures should not be used for Actions or Filters
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

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.

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
Using these standards we get easier to maintain and longer life of code.

I=================================

My Favourite WordPress tools:

Blocksy Theme

WP Stackable Blocks

Astra Theme

WP Rocket

WP Engine

Gravity forms

=================================
Learn more about MRK
=================================

Find my Website

Find me on Twitter

Find me on Github

Find me on WordPress

source