Undefined WordPress Functions in PHPStorm

In this guide, you will learn how to get PHPStorm to recognize undefined WordPress functions while developing a WordPress plugin. In addition, we discuss some background on why this might happen.

Table of Contents


Introduction

PHPStorm is a PHP IDE from Jetbrains. It is a great choice for PHP development, including WordPress Plugin development. This IDE offers several ways to structure and execute a WordPress plugin project. Depending on how you structure your plugin, you may run into an issue where PHPStorm gives you a warning that WordPress specific functions are undefined. You can see an example of this warning in the image below:

Screenshot of PHPStorm showing an undefined PHP function

Project Structure

We’re going to assume that your project uses the approach described in the guide on using Docker Compose for local WordPress development. The diagram below shows the general structure:

Screenshot of a diagram of the structure of our WordPress Plugin

There are three main components in our project.

  1. First, a top-level git repository, shown in blue, that contains our docker-compose.yml configuration file.
  2. Second, mounted docker volumes, shown in gray, that contain the data used by Docker Compose. Since PHPStorm requires use to define the path to our WordPress installation, we must copy the contents of /var/www/html from the WordPress container to our local filesystem.
  3. Lastly, Git submodules, shown in green, for any additional plugins.

Why are WordPress Functions Undefined?

PHPStorm is clever enough to understand git submodules. This means that you can work on different git projects within a single PHPStorm workspace. However, it is not yet smart enough to include function definitions from parent modules.

Based on the diagram shown above, PHPStorm knows that our-plugin is a part of wordpress but not the other way around. Since all of the WordPress function definitions are stored in wordpress/html when we try to use them in PHP files located inside of our-plugin, we get an undefined function warning.

More generally, WordPress is an “External Library” to our new plugin. Since WordPress plugins don’t have a first-class concept of dependency management, there is no great way to signal to PHPStorm that this relationship exists. For this reason, we can resolve this issue by manually adding WordPress as an external library to our project.

How To Manually Add an External Library in PHPStorm

  1. Open the Project Settings Window

    You can use the Alt+Ctrl+S shortcut, or navigate to File -> Settings

    Screenshot of the PHP Settings screen in PHPStorm

  2. Navigate to Languages & Frameworks -> PHP

  3. Add a new Path

    Select Include Path and then add a new path by selecting the “plus” (+) symbol on the right side of this window.

  4. Add WordPress Location

    In our example, WordPress files are located in the html folder.

    Screenshot of the Path Dialog in PHPStorm

  5. Save Your Changes

    Select the Apply button and then select OK to save your changes.

    You should now see the full function definitions of any WordPress function that you use.

    Screenshot of a defined PHP function in PHPStorm

Conclusion

In summary, you’ve learned how to resolve WordPress functions appearing as undefined in PHPStorm by adding WordPress as an External Library.


You can add a comment via Email. Or use the embedded discourse form shown below: