Search
Close this search box.
Search

How to Format PHP code to WordPress Coding Standards in NetBeans

Listen to this article

talk-is-cheap-coding-standardsAll developers have a unique coding style, what makes the code uniform is the standards they follow.

Coding standards are rules which make the code readable for developers working on the same application, whether they are your workmates or add-on developers.

I wouldn’t say that coding standards are more important when you’re working with open source. Coding standards are always important. But I’d surely say, mistakes in your code can become more apparent when the source code is openly available to others. 😀

For example, when you write a plugin or theme for WordPress, there are high chances someone from the community will take a look at your code. Or you, yourself might someday open someone else’s code to customize it. Clean code, adhering to the coding standards makes it easy to understand and modify.

It’s not that difficult to stick to WordPress coding standards. In fact, if you follow them, coding according to the standards just becomes a habit. But there are some of us, some newbies, or others used to developing for other CMSs who might find writing code as per WordPress coding standards difficult.

andrew-munro-coding-standards
Click for more thoughts from Andrew

Not to worry!

An easy solution, is to add these standards into your IDE. So that a few gaffes are caught before the code review stage. In this article, I’ll show you how you can configure NetBeans so that your PHP code adheres to WordPress coding standards.

Note: We are going to configure NetBeans on an Ubuntu system but similar steps can be followed for any other Linux based OS.

[space]

Formatting PHP code to WordPress Coding Standards in NetBeans

If you haven’t installed PHP, fire the below commands:

sudo apt-get update
sudo apt-get install php5

#1 Install PHP_CodeSniffer

First, we will need to install PHP_CodeSniffer and PHP_CodeSniffer for WordPress. Open your terminal and fire a command:

sudo pear install PHP_CodeSniffer

Side note: You’ll need Pear installed for this. If pear is not installed, you’ll be able to install it using the below commands:

wget http://pear.php.net/go-pear.phar
php go-pear.phar

And then use the above mentioned command to install PHP_CodeSniffer.

To verify if PHP_CodeSniffer is installed or not use the command phpcs -i

Once PHP_CodeSniffer is installed, we will need to install WordPress coding standards for CodeSniffer. The procedure for the same can be found using this link.

#2 Install PHP Tidy

While we’re at it, let’s install a script WP PHP Tidy. This script will help us to format our code automatically as much as possible. To install this script, fire the below commands in your terminal:

cd ~/Documents
wget https://github.com/scribu/wp-phptidy/archive/master.zip
unzip master.zip
cd wp-phptidy-master
chmod +x wp-phptidy.php
sudo mv wp-phptidy.php /usr/local/bin/wp-phptidy

Now that we have PHP_CodeSniffer and WP PHP Tidy installed, we will configure NetBeans. (Finally!!)

#3 Configure NetBeans

For this step, I have compiled the settings in one zip file. Download the file from here and open NetBeans.

  • Go to Tools -> Options -> Import and select this netbeans_settings.zip file which you just downloaded. Click ‘Ok’. It will import the settings and restart NetBeans.
  • This will configure the formatting options in NetBeans and CodeSniffer option in ‘Code Analysis’ section (found under Tools -> Options -> PHP)

We have all the tools ready now. Let’s move to the execution stage. (We’re almost there. Hang on!)

Create a new project or choose an existing project in NetBeans. Create or edit a file and start writing some PHP code. Once you have written the code, go to ‘Source’ and click ‘Format’. This will format the code as we set under ‘Formatting’ Settings in NetBeans. Once this is done, Save the file.

Now fire your terminal and head to the NetBeans project directory and fire the below commands:

sudo phpcbf --standard=WordPress-Core your-file.php
sudo wp-phptidy replace your-file.php

Now you should be able to tackle most WordPress Coding Standard violations automatically. However, there will still be a few issues, which you’d have to solve manually. 

To identify such issues go to ‘Format -> Inspect’. Select ‘Code Sniffer’ in the configuration. Verify that the ‘Scope’ is defined to your current file and hit ‘Inspect’. It will display all the problems which make your code incompatible with WordPress coding standards. These problems you will have to solve manually. Once those problems are solved, your code should be compatible with WordPress Coding Standards.

Congratulations!

mike-jolley-coding-standards
Click for more from Mike Jolley

Sure, you should keep an eye open in the code review phase too!

Well, that’s all for me for now! If you have any doubts or have a better solution, please feel free to add your thoughts in comment section below! 🙂

Sumit Pore

Sumit Pore

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

WordPress Development

Custom WordPress Solutions, for You

LearnDash Development

Custom LearnDash Solutions, for You

WooCommerce Development

Custom WooCommerce Solutions, for You

WordPress Plugins

Scale your WordPress Business

WooCommerce Plugins

Scale your WooCommerce Business

LearnDash Plugins

Scale your LearnDash Business

Label

Title

Subtext

Label

Title

Subtext

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads