IntelliJ IDEA 2023.1 Help

WordPress-specific coding assistance

IntelliJ IDEA 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 IntelliJ IDEA editor, and so on.

Adding the WordPress to a project

To take advantage of coding assistance provided by IntelliJ IDEA, 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.

  1. In the Settings dialog (Control+Alt+S), navigate to Languages & Frameworks | PHP.

  2. On the PHP reference page that opens, add the path to the WordPress installation folder to the Include Paths list: click the the Add button 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:

    1. In the Settings dialog (Control+Alt+S), navigate to Languages & Frameworks | PHP.

    2. On the PHP page that opens, add the path to the wp-content folder to the Include Paths list: click the the Add button 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:

    1. Open the Project Structure dialog (e.g. Control+Alt+Shift+S).

    2. In the left-hand pane, select Modules.

    3. In the pane to the right, select the necessary module.

    4. In the right-hand part of the dialog, select the Sources tab.

    5. Click Add Content Root.

    6. In the dialog that opens, locate the wp-content directory and click OK.

    Learn more about configuring content roots in Content roots. For information about adding files and folders to version control, see Enable version control.

Configuring WordPress code style

In IntelliJ IDEA, 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, IntelliJ IDEA 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 (Control+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 IntelliJ IDEA, 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 section. Press Control+Space to get code completion for the package name and version.

  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, IntelliJ IDEA 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 section. Press Control+Space to get code completion for the package name and version.

  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, IntelliJ IDEA 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 (Control+Alt+S). See Enable PHP_CodeSniffer as an IntelliJ IDEA inspection for details.

Hooks support

IntelliJ IDEA 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

  1. Start typing the declaration of an add_action() or add_filter() function.

  2. Press Control+Space and choose the relevant parameter from the list.

    ps_wp_hooks_completion.png

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 Control key pressed, hover the cursor over the parameter of interest. IntelliJ IDEA displays a popup information message with the definition of the function or method specified as this parameter:

    ps_wp_hooks_callback.png

    The parameter turns into a link. When you click this link, IntelliJ IDEA opens the file where the function or method was declared and positions the cursor and declaration.

Learn more about search and navigation in Search for a target within 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:

    1. Choose Navigate | Symbol from the main menu.

    2. 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.

      Navigating to symbol
    3. Click the relevant hook from the list. IntelliJ IDEA opens the file where it is invoked and positions the cursor at the invocation.

  • Use the Search Everywhere functionality:

    1. Press Shift twice.

    2. 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.

    3. Click the relevant hook from the list. IntelliJ IDEA 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

  1. Position the caret at the name of the desired hook registration function and press Alt+F7 or choose Edit | Find | Find Usages from the main menu.

  2. 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.

  3. Explore the listed occurrences. To navigate to the relevant one, click it. IntelliJ IDEA 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

  1. Position the caret at the parameter of interest and press Alt+F7 or choose Edit | Find | Find Usages from the main menu. The 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.

  2. Explore the listed occurrences. To navigate to the relevant one, click it. IntelliJ IDEA opens the file with the selected function or method and positions the cursor at it.

Viewing the official WordPress documentation from IntelliJ IDEA

You can view the official WordPress Documentation at http://wordpress.org/ right from the IntelliJ IDEA editor.

  1. Select the text you are interested in.

  2. From the context menu of the selection, choose Search on WordPress.org.

    IntelliJ IDEA opens the page with the relevant documentation in the default browser.

Last modified: 21 June 2023