Skip to main content

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

What's Next