development

Neovim Plugin for WordPress and WooCommerce

wordpress woocommerce open-source neovim
Mile Rosu
Mile Rosu

Vim has been the editor of choice for our team since day one and has remained our loyal companion in writing code to day, be it for CakePHP, WordPress, Django or Golang projects. Working on the code using Vim feels like using the highest quality hand tools instead of the common, noisy electrical power tools. In our opinion, there’s little need to have a lot of bells and whistles but miss instead the main point of reducing distraction and noise around the coding work.

Getting to use Neovim, with its extensible approach has allowed us to keep our tool as “sharp” and as extensible as needed. Not to say that the vision of the Neovim team is something we adhere to completely. The routines we use to adjust our Neovim for best use with WordPress and WooCommerce have crystalized into a simple plugin for Neovim that we’re glad to share.

The plugin sets the tabs according the WordPress coding standards and is integrating the Intelephense Language server protocol, as well as preparing Neovim for use with the phpcs and phpcbf - PHP Code Sniffer and PHP Code Beautifier and Fixer by using the none-ls.nvim language server.

The plugin requires that you have Intelephense installed in your environment. Also, you need to make sure you have PHP Code Sniffer installed with the WordPress Coding Standards.

If you are using Mason, you need to disable it for phpcs.

require("mason-null-ls").setup {
    automatic_installation = {
        exclude = { "phpcs", "phpcbf" }
    }
}

Then, install phpcs globally with support for WordPress Coding Standards.

composer global require "wp-coding-standards/wpcs"

Now you are ready to install the actual plugin with your favorite Neovim package manager - either Lazy

{
  "bitpoke/wordpress.nvim",
}

or Packer with a prepended use:

use {
  "bitpoke/wordpress.nvim",
}  

Once installed, the plugin still needs you to add the configuration lines:

local wp = require('wordpress')
local lspconfig = require('lspconfig')
local null_ls = require('null-ls')

-- setup Intelephense for PHP, WordPress and WooCommerce development
lspconfig.intelephense.setup(wp.intelephense)

null_ls.setup({
    ...,
    sources = {
        ...,
        null_ls.builtins.diagnostics.phpcs.with(wp.null_ls_phpcs),
        null_ls.builtins.formatting.phpcbf.with(wp.null_ls_phpcs),
    },
})

There is a known issue that we are aware of, calling vim.lsp.buf.format() formats using both phpcbf and intelephense tools. vim.lsp.buf.format() uses all available formatters for the current file type. To avoid this, you can use the following command to format the current buffer:

vim.lsp.buf.format({ filter = require('wordpress').null_ls_formatter })

Looking forward to hear from you about how you would make the plugin better in the issues section of the Bitpoke WordPress Neovim plugin Github repo.

Work with us! Schedule a free consultation now
Next article Affordable WordPress and WooCommerce loadtesting...