In a simple word, Coding Standards are is list of rules, specific guidelines and best practices which ascertain a programming style, methods and other procedures for a programming language.
Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices, and methods for each aspect of a program written in that language. - Wikipedia
Every software development organization want their programmer to maintain a specific guideline and rules to code for best eco-system.
WordPress is a very famous CMS build in PHP. WordPress is the most widely used popular CMS in the world’s of internet. Almost 30% of all the websites around the world are made of WordPress. There is comprehensive opportunities to create jobs in the WordPress marketplace around the world.
There is a lot of sections in WordPress website development. Most widely popular is Plugins and Themes development. Anyone can create their own plugin and theme with their PHP experience and PHP coding styles. But WordPress has it’s own Coding Standards for it’s eco-system & to make popular to the programmers and to maintain same coding styles.
It doesn’t matter if you maintain the Coding standards or not. Your plugin and theme will still works without it. But if you work for a good company or for a good client you should maintain coding standards for future development. So, anyone work after you will be able to understand, extend or modify your code easily. Then they can give good feedback and will be help you to get good impression in your career.
How to apply WordPress Coding Standards?
Well, it’s really very easy to use coding standard on your plugin or theme. If you have knowledge in Composer you are ready to go! If you don’t know what Composer is then you can read about it around the internet.
So, let’s assume you have already installed Composer in your computer.
First we need to open CMD panel if you are on Windows, for Mac you have Terminal. I am using Windows and installed git bash to open the CMD window easily from any folder around my project. If you want don’t know about git bash, you can also read about it from internet. There is lot of resources about it. If you don’t want to use git bash then you can use default CMD panel from your Windows. Check the following screenshots..
First go to the path of your project folder, select it
Then type cmd then hit enter.
It will open CMD panel on the current directory.
We are all set. Let’s create our composer.json file.
Type the following line in your CMD panel:
It will show following setup configuration, just setup your config or keep default settings by hitting enter button from your keyboard.
Once you are done setting up your configuration you will be able to see your composer.json file created in your project folder. It will be looks like as bellow screenshot
Now install the composer by running following code in CMD
Now we need to install WordPress Coding Standard dependency package. Enter the following code in your CMD panel
composer require --dev wp-coding-standards/wpcs
Once the installation done it will look as follow
We need to setup package path from our vendor folder if we want to add it manually. But there is another easy way to add the Coding Standards package path easily. For that we need another PHP package. Run following code in CMD panel again ..
composer require --dev dealerdirect/phpcodesniffer-composer-installer
After successful installation it will be look like as bellow..
Now let’s test wpcs. Run following code
bash ./vendor/bin/phpcs -i
it will show the installed Coding Standards. As I have installed some other Coding Standards dependency packages I am getting more Coding Standards packages name
If you see these we are all set. Now we can test our coding with Coding Standards. Let’s see an example. I want to test a folder and a file outside of that folder to check if there is any Coding Standards error or not. I am running following code -
bash ./vendor/bin/phpcs --standard=WordPress ./core ./woocommerce-altcoin-payment.php
After running the above code, I see there is error on my coding.
Now I want to fix these automatically. So, I’m running again -
bash ./vendor/bin/phpcbf --standard=WordPress ./core ./woocommerce-altcoin-payment.php
Some issues has been fixed automatically.
But some still not fixed. These error need to look manually and need to fix.
That’s all! We have setup and tested WordPress Coding Standards in our project.
Read more about WordPress Coding Standards - https://make.wordpress.org/core/handbook/best-practices/coding-standards/