WordPress-specific coding assistance
PhpStorm provides full coding assistance for developing WordPress including WordPress-aware code completion, search for hook registration functions and functions specified as hook registration parameters, navigation between hook registrations and the hook invocations, callbacks, the possibility to configure the coding style in accordance with the WordPress code style, viewing the official WordPress documentation from the PhpStorm editor, and so on.
Adding the WordPress to a project
To take advantage of coding assistance provided by PhpStorm, the WordPress installation you are working with should be configured in the project as an external library. As a result, the WordPress core file will be involved in indexing, which is the basis for resolving references and providing code completion, navigation, search, and so on.
If the wp-content folder is outside the WordPress installation, it needs to be added either as an include path or as a content root.
Configure the WordPress installation as a project include path
To add WordPress to your project as an external library, you need to add it to the list of include paths. Learn more in Configure include paths.
In the Settings/Preferences dialog Ctrl+Alt+S, navigate to .
On the PHP page that opens, add the path to the WordPress installation folder to the Include Paths list: click the button and specify the path to the installation folder in the dialog that opens.
Add the wp-content folder to the project
If the wp-content folder is located outside the WordPress installation, you need to add it to the project individually, apart from the WordPress core files. This can be done in two ways:
To have the wp-content folder involved in indexing without putting it under the project version control, add it to the list of include paths:
In the Settings/Preferences dialog Ctrl+Alt+S, navigate to .
On the PHP page that opens, add the path to the wp-content folder to the Include Paths list: click the button and specify the path to the folder in the dialog that opens.
To have the wp-content folder involved in indexing and put it under the project version control, add it as a content root:
In the Settings/Preferences dialog Ctrl+Alt+S, navigate to Directories.
On the Directories page that opens, click the Add Content Root button .
In the dialog that opens, locate the wp-content directory and click OK.
Learn more about configuring content roots in Configuring content roots. For information about adding files and folders to version control, see Enable version control.
Configuring WordPress code style
In PhpStorm, you can use the WordPress Code Style in accordance with the WordPress coding standards. Learn more in Configuring code style.
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:
In the Settings/Preferences dialog Ctrl+Alt+S, go to .
On the Code Style. PHP page that opens, click the Set from... link.
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
Inside composer.json, add the squizlabs/php_codesniffer dependency record to the
require
orrequire-dev
section. Press Ctrl+Space to get code completion for the package name and version.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
Inside composer.json, add the wp-coding-standards/wpcs dependency record to the
require
orrequire-dev
section. Press Ctrl+Space to get code completion for the package name and version.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 page of the Settings/Preferences dialog Ctrl+Alt+S. See Configure PHP_CodeSniffer as a PhpStorm inspection for details.
Hooks support
PhpStorm indexes all hooks declared in the WordPress core and in the included plugins. This makes the basis for coding assistance when working with hooks.
Completion for parameters of the Action and Filter functions
Hook names are available in code completion of standard parameters for action and filter functions add_action()
and add_filter()
.
Invoke completion for a parameter
Start typing the declaration of an
add_action()
oradd_filter()
function.Press Ctrl+Space and choose the relevant parameter from the list.
Navigation from a hook registration to the hook invocation
You can navigate from a hook registration (add_action()
or add_filter()
function) to the hook invocation.
Callbacks from a hook registration
You can navigate to the declaration of the function or method specified as the second parameter of a hook registration (add_action()
or add_filter()
function).
With the Ctrl key pressed, hover the cursor over the parameter of interest. PhpStorm displays a popup information message with the definition of the function or method specified as this parameter:
The parameter turns into a link. When you click this link, PhpStorm opens the file where the function or method was declared and positions the cursor and declaration.
Navigating to hook invocations
Learn more about search and navigation in Find and replace text in a file and Source code navigation.
Search for a hook invocation and navigate to it
Do one of the following:
Use the Navigate to Symbol functionality:
Choose
from the main menu.In the Enter symbol name popup that appears, start typing the hook name in the search field. If necessary, select the Include non-project items checkbox. The contents of the list below the search field change as you type.
Click the relevant hook from the list. PhpStorm opens the file where it is invoked and positions the cursor at the invocation.
Use the Search Everywhere functionality:
Press Shift twice.
start typing the hook name in the search field. If necessary, select the Include non-project items checkbox. The contents of the list below the search field change as you type.
Click the relevant hook from the list. PhpStorm opens the file where it is invoked and positions the cursor at the invocation.
Searching for hook registration functions
You can search for occurrences of a hook registration function and for occurrences of a function or method specified as the second parameter of a hook registration.
Learn more in Search for usages
List usages for a hook registration function
Position the caret at the name of the desired hook registration function and press Alt+F7 or choose
from the main menu.From the popup list that shows the available options in terms of hook/function name, choose the relevant option. The Find tool window opens with a new tab showing all the detected occurrences of the selected function.
Explore the listed occurrences. To navigate to the relevant one, click it. PhpStorm opens the file with the selected function or method and positions the cursor at it.
List occurrences of a function or method specified as the second parameter of a hook registration
Position the caret at the parameter of interest and press Alt+F7 or choose Find tool window opens with a new tab showing all the detected occurrences of the function or method specified as the parameter on which the search was invoked.
from the main menu. TheExplore the listed occurrences. To navigate to the relevant one, click it. PhpStorm opens the file with the selected function or method and positions the cursor at it.
Viewing the official WordPress documentation from PhpStorm
You can view the official WordPress Documentation at http://wordpress.org/ right from the PhpStorm editor.
Select the text you are interested in.
From the context menu of the selection, choose Search on WordPress.org.
PhpStorm opens the page with the relevant documentation in the default browser.