How to make prettier (php) to work with VS Code

5.1k Views Asked by At

I have prettier installed on VS code but it does not seem to have any functionality. I installed Prettier - Code formatter (6.4.0) through VS code extensions, I am using the latest version of VScode and there are no updates available.

Both the format on save and cmd-shift-p no nothing for .php files but works perfectly for .js files.

My VS code settings.json file look like this:

{
"editor.codeActionsOnSave": null,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}

This is the only VS code extension I have installed, so it is not a compatibility issue with another VS code extension, however I did also install the plugin-php formatter globally using:

npm install --global prettier @prettier/plugin-php
2

There are 2 best solutions below

0
On

Try installing the PHP formatter locally, like this:

npm install --save-dev prettier @prettier/plugin-php
0
On

From the official doc here: https://github.com/prettier/plugin-php for Visual Studio Code: add php to document selectors in vscode settings (settings.json)

 "prettier.documentSelectors": [
    "**/*.{js,jsx,ts,tsx,vue,html,css,scss,less,json,md,mdx,graphql,yaml,yml,php}"
  ]

and declare the php parser in prettier config file (.prettierrc) https://prettier.io/docs/en/configuration

{
    "parser": "php",
    "plugins": ["@prettier/plugin-php"]
}