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
- Project Structure
- Why are WordPress Functions Undefined?
- How To Manually Add an External Library in PHPStorm
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:
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:
There are three main components in our project.
- First, a top-level git repository, shown in blue, that contains our
- 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/htmlfrom the WordPress container to our local filesystem.
- 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
Open the Project Settings Window
You can use the Alt+Ctrl+S shortcut, or navigate to File -> Settings
Navigate to Languages & Frameworks -> PHP
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.
Add WordPress Location
In our example, WordPress files are located in the
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.
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: