Using phpactor as a language server for WordPress development

Published by on .

This post shows how to use phpactor as a language server for WordPress development. Phpactor is a strong tool for PHP development, and it works with editors and IDEs that support the Language Server Protocol (LSP).

Phpactor works well with modern PHP code when the project either follows PSR-4 or uses Composer for autoloading. WordPress does not use either approach, so phpactor can have trouble discovering symbols from WordPress core by default.

Configuring phpactor to include WordPress core files

You can solve this by setting the indexer.include_patterns configuration option to include WordPress core files. Add the following to your .phpactor.json configuration file:

{
    "indexer.include_patterns": [
        "wp-includes/**/*.php",
        "wp-admin/**/*.php",
        "wp-content/plugins/**/*.php",
        "wp-content/themes/**/*.php"
    ]
}

I also exclude WordPress core and third-party plugins from phpactor’s diagnostics through the language_server.diagnostic_exclude_paths configuration setting.

{
  "language_server.diagnostic_exclude_paths": [
    "wp-includes/**/*",
    "wp-admin/**/*",
    "wp-content/plugins/woocommerce/**/*"
  ]
}

With these two configuration options in place, phpactor can discover symbols from WordPress core and provide accurate diagnostics for your own code while ignoring issues in WordPress core or third-party plugins.

Using WordPress stubs

Alternatively, you can use stubs to give phpactor the information it needs about WordPress core functions and classes.

composer require --dev php-stubs/wordpress-stubs

This installs the php-stubs/wordpress-stubs package. You can then configure phpactor to use these stubs by adding the following to your .phpactor.json configuration file:

{
    "worse_reflection.additive_stubs": [
        "vendor/php-stubs/wordpress-stubs/wordpress-stubs.php"
    ]
}

Both approaches make phpactor more useful in WordPress projects. Indexing core files gives phpactor more project context, while stubs provide a lightweight way to expose WordPress functions and classes to the language server.