PhpStorm 2024.1 Help

WordPress code style

Configuring WordPress code style

In PhpStorm, you can use the WordPress Code Style in accordance with the WordPress coding standards. Learn more in Code style schemes.

Upon activating of the WordPress integration, PhpStorm displays a popup offering you to set the WordPress code style. Click the Set it link in the popup and set the code style on the Code Style. PHP page that opens.

To set the WordPress code style manually:

  1. In the Settings dialog (Ctrl+Alt+S) , go to Editor | Code Style | PHP.

  2. On the Code Style. PHP page that opens, click the Set from... link.

  3. From the context menu, choose Predefined Style, then choose WordPress.

Checking code against the WordPress coding standards

With PhpStorm, you can use the PHP_CodeSniffer tool, which detects coding standard issues, in combination with WordPress Coding Standards, which provides a set of WordPress-specific standards to PHP_CodeSniffer. This will ensure that your code is clean, consistent, and free of some common errors.

To get started, install PHP_CodeSniffer using any technique described in Install and configure PHP_CodeSniffer. Probably the easiest way is to install it with Composer.

Install PHP_CodeSniffer

  1. Inside composer.json, add the squizlabs/php_codesniffer dependency record to the require or require-dev key. To get code completion for the package name and version, press Ctrl+Space.

  2. Do one of the following:

    • Click the Install shortcut link on top of the editor panel.

    • If the Non-installed Composer packages inspection is enabled, PhpStorm will highlight the declared dependencies that are not currently installed. Press Alt+Enter and select whether you want to install a specific dependency or all dependencies at once.

Next, install WordPress Coding Standards, which will provide WordPress-specific standards to PHP_CodeSniffer.

Install WordPress Coding Standards

  1. Inside composer.json, add the wp-coding-standards/wpcs dependency record to the require or require-dev key. To get code completion for the package name and version, press Ctrl+Space.

  2. Do one of the following:

    • Click the Install shortcut link on top of the editor panel.

    • If the Non-installed Composer packages inspection is enabled, PhpStorm will highlight the declared dependencies that are not currently installed. Press Alt+Enter and select whether you want to install a specific dependency or all dependencies at once.

The WordPress Coding Standards package will be installed, and the corresponding WordPress standard will be selected for the PHP_CodeSniffer validation inspection automatically. If necessary, you can further customize the inspection on the Editor | Inspections page of the Settings dialog (Ctrl+Alt+S) . For more information, refer to Enable PHP_CodeSniffer as a PhpStorm inspection.

Last modified: 04 April 2024