PHP and WordPress Setup
Required Tools
# LSP server: Intelephense (better PHP LSP)
npm install -g intelephense
# Formatters
composer global require friendsofphp/php-cs-fixer
# Linters
composer global require squizlabs/php_codesniffer
# Mason
:MasonInstall intelephense phpcs php-cs-fixer
Intelephense Configuration
lspconfig.intelephense.setup({
on_attach = on_attach,
capabilities = capabilities,
settings = {
intelephense = {
files = {
maxSize = 5000000,
},
environment = {
phpVersion = "8.2", -- your PHP version
},
diagnostics = {
enable = true,
undefinedClassConstants = true,
undefinedConstants = true,
undefinedFunctions = true,
undefinedMethods = true,
undefinedProperties = true,
undefinedSymbols = true,
undefinedVariables = true,
unusedSymbols = false,
},
format = {
enable = true,
},
stubs = {
-- Enable WordPress stubs for autocompletion
"wordpress",
"apache", "bcmath", "bz2", "calendar",
"Core", "curl", "date", "dom", "exif",
"fileinfo", "filter", "ftp", "gd",
"gettext", "json", "mbstring", "mcrypt",
"memcache", "memcached", "mysql", "mysqli",
"openssl", "pcre", "PDO", "pdo_mysql",
"readline", "Reflection", "session",
"simplexml", "soap", "SPL", "sqlite3",
"standard", "tokenizer", "xml", "xmlreader",
"xmlwriter", "zip", "zlib",
},
},
},
})
WordPress Stubs
Install WordPress stubs for proper IDE completion:
composer require --dev php-stubs/wordpress-stubs
PHP CS Fixer Configuration
.php-cs-fixer.php
<?php
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('node_modules')
->exclude('vendor');
return (new PhpCsFixer\Config())
->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => true,
'no_unused_imports' => true,
'trailing_comma_in_multiline' => true,
'single_quote' => true,
])
->setFinder($finder);
WordPress Coding Standards (PHPCS)
# Install WordPress standards
composer global require wp-coding-standards/wpcs
phpcs --config-set installed_paths ~/.composer/vendor/wp-coding-standards/wpcs
none-ls Sources for PHP
null_ls.setup({
sources = {
null_ls.builtins.formatting.phpcsfixer.with({
extra_args = { "--config=.php-cs-fixer.php" },
}),
null_ls.builtins.diagnostics.phpcs.with({
extra_args = { "--standard=WordPress" }, -- or PSR12
}),
},
})
WP-CLI Integration
Run WP-CLI commands from Neovim:
lua/config/keymaps.lua
vim.keymap.set("n", "<leader>wp", function()
local cmd = vim.fn.input("WP-CLI: wp ")
if cmd ~= "" then
vim.cmd("split | terminal wp " .. cmd)
end
end, { desc = "Run WP-CLI command" })
PHP File Structure Navigation
With Treesitter + nvim-treesitter-textobjects:
vaf → select entire PHP function
dac → delete entire class
]f → jump to next function
[c → jump to previous class
In LazyVim
LazyVim Context
LazyVim provides a lang.php extra that instantly configures PHP LSP (Intelephense or PHPactor), formatting, and linting. It leverages conform.nvim and vim-lint for modern standard practices without writing extensive configurations manually.
| Aspect | Vanilla Neovim | LazyVim |
|---|---|---|
| LSP Setup | Manual intelephense configuration | Enable via :LazyExtras → lang.php |
| Formatting | Custom php-cs-fixer in | |
| ull-ls | Pre-configured via conform.nvim (via the extra) | |
| Linting | Custom phpcs in | |
| ull-ls | Pre-configured via | |
| vim-lint | ||
| WP Stubs | Requires manual LSP stubs array config | Needs slight manual override for intelephense settings if strictly using WP |
Enabling PHP in LazyVim
- Open Neovim.
- Run :LazyExtras.
- Enable lang.php.
By default, the lang.php extra uses intelephense and sets up php-cs-fixer for formatting.
Adding WordPress Stubs in LazyVim
Since WordPress isn't a standard part of vanilla PHP development, you will need to append the wordpress stub to Intelephense inside LazyVim. You can do this by extending the LSP configuration in your lua/plugins/lsp.lua file:
lua/plugins/lsp.lua
return {
{
"neovim/nvim-lspconfig",
opts = {
servers = {
intelephense = {
settings = {
intelephense = {
stubs = {
"wordpress",
"apache", "bcmath", "bz2", "calendar", "Core", "curl", "date", "dom", "exif", "fileinfo", "filter", "ftp", "gd", "gettext", "json", "mbstring", "mcrypt", "memcache", "memcached", "mysql", "mysqli", "openssl", "pcre", "PDO", "pdo_mysql", "readline", "Reflection", "session", "simplexml", "soap", "SPL", "sqlite3", "standard", "tokenizer", "xml", "xmlreader", "xmlwriter", "zip", "zlib",
},
},
},
},
},
},
},
}