Skip to content

How to install Prettier PHP Plugin in Visual Studio Code #1721

Open

Description

@alexbalak21

Hello,

I am using Visual Studio Code for editing .php files.
How can I set up Prettier plugin to format php in vs code.
I tried to follow README.md but, I am not able to activate the code formatting on php.

Thank you for your help.

Activity

  1. czosel commented on May 6, 2021

    @czosel
    Collaborator

    Hi @alexbalak21,
    can you describe the steps you took in detail? As far as I know the instructions in the Readme should still be valid.

  2. NateOs commented on May 11, 2021

    @NateOs

    I have the same issue, happen to be working on a large PHP codebase and the formatting is killing me, any help adding the plugin to already installed prettier in vscode?

  3. alexbalak21 commented on May 13, 2021

    @alexbalak21
    Author

    I use Prettier to format on save on Vs Code.
    When I use it on php files it is not working.
    I installed prettier on node globally. When I use: "prettier path/to/file.php --write" it works and formats the file.

    How do I add formatting php files on save ? (with Prettier PHP Plugin)

    I want it to format my php files on save automatically.

  4. czosel commented on May 13, 2021

    @czosel
    Collaborator

    For me, the plugin works as expected (tested using VScode 1.56.1 with the prettier extension v6.4.0). Try the following steps:

    1. Install the prettier extension for VScode (https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
    2. Install prettier and @prettier/plugin-php locally in the root your workspace: npm install --save-dev prettier @prettier/plugin-php or yarn add --dev prettier @prettier/plugin-php
    3. The prettier extension should ask if you want to run the local copy of prettier including all installed plugins:
      Screenshot from 2021-05-13 18-35-58
      If the dialog doesn't appear, try opening the package.json file.
    4. Run "Format Document" (or enable "format on save" and save the file)
  5. NateOs commented on May 15, 2021

    @NateOs

    Thanks, @czosel, I succeeded in installing the PHP plugin but now it doesn't actually format code but gives a response on the terminal that it has formatted the code but with no effect on the code that is recognisable as change

  6. czosel commented on May 15, 2021

    @czosel
    Collaborator

    Hi @NateOs, can you share the detailed steps on how to reproduce this?

  7. NateOs commented on May 19, 2021

    @NateOs

    Great, I have prettier v6.4.0 installed,
    I downloaded the latest version of the PHP-plugin as at the time and installed it by "npm install --save-dev prettier @prettier/plugin-PHP
    I got the prompt to allow and i "allowed everywhere"

    Before following your install process I had already tried and failed to install, therefore prettier will give an error message when I tried to format PHP, the error went away after installing your way, but now I don't get any actual formatting although prettier on save will tell me in the terminal that everything is ok, also I am not using any prettier config files, just the extension settings in vs code

  8. czosel commented on May 20, 2021

    @czosel
    Collaborator

    Are you sure the file you’re trying to format is valid PHP? Can you post a screenshot?

  9. robertandrews commented on Mar 6, 2022

    @robertandrews

    I installed it npm install --save-dev prettier @prettier/plugin-php but it doesn't do anything.
    When I choose "Format Document" from the Command Palette, VS Code pops up to say: "There is no formatter for 'plaintext' files installed."

    Screenshot 2022-03-06 at 08 41 36

    It then opens up the Extensions market at "category:formatters plaintext".

    This is for a doc that starts <?php and ends ?>, one that is picked up as PHP elsewhere...

    Screenshot 2022-03-06 at 08 42 50

    Help.

  10. czosel commented on Mar 6, 2022

    @czosel
    Collaborator

    Does the file you’re trying to format end with “.php”? Do you have the prettier extension for vscode installed?

  11. robertandrews commented on Mar 6, 2022

    @robertandrews

    Does the file you’re trying to format end with “.php”?

    Yes. test.php.

    Do you have the prettier extension for vscode installed?

    Yes.

  12. czosel commented on Mar 6, 2022

    @czosel
    Collaborator

    Hmm, I haven’t seen this before. Probably it would be best if you ask about this over at https://github.com/prettier/prettier-vscode

  13. UnderDogg commented on Mar 27, 2022

    @UnderDogg

    I'm a bit late to the party, but ....

    • open the vscode terminal, to see if there's any output
    • there's a developer console in vscode, it should show any js exceptions when the code will not format
  14. robertandrews commented on Mar 28, 2022

    @robertandrews

    I'm a bit late to the party, but ....

    • open the vscode terminal, to see if there's any output
    • there's a developer console in vscode, it should show any js exceptions when the code will not format

    @UnderDogg ... I couldn't remember if this plugin was installed (doesn't appear as an extension), so (re?)-ran sudo npm install --global prettier @prettier/plugin-php...

    In a PHP file, if I choose from the command palette "Format Document" (that's Prettier, right?), I still get: "There is no formatter for 'plaintext' files installed."

    I have resorted to installing other PHP formatters, which correctly format the document on save. I don't know why this one won't.

    Actually, I was just navigating to /usr/local/lib/node_modules/@prettier/plugin-php to delete the installed plugin and... it's not there...

    @czosel The docs say to run npm install --global prettier @prettier/plugin-php. This @prettier folder is not prettier. This command creates a second folder with the @ prefix. The plugin-php is never added to prettier.

    If I instead run sudo npm install --global prettier prettier/plugin-php, (no @), it still creates a folder with the @.

    Is that the way this should be working?

  15. UnderDogg commented on Mar 28, 2022

    @UnderDogg

    I wouldn't know how to answer this one.
    Focus on this error:
    There is no formatter for 'plaintext' files installed
    Check your settings, maybe .php is associated with plaintext?
    Check this piece:

    "files.associations": {},
    

    Or just find plaintext in your settings.json file.

  16. czosel commented on Mar 28, 2022

    @czosel
    Collaborator

    @robertandrews Yep, that's how it's supposed to be working. Inside the prettier program, all folders starting with @prettier/plugin- are searched and registered as plugins. You can verify if your installation is working by running prettier from the command line on a php file (something like yarn prettier test.php).

  17. jaxramus commented on Apr 22, 2022

    @jaxramus

    Installing locally and opening the package.json file worked for me. I didn't get a popup asking if I wanted to allow it everywhere though.

  18. webprogrammerio commented on Apr 25, 2022

    @webprogrammerio

    For me, the plugin works as expected (tested using VScode 1.56.1 with the prettier extension v6.4.0). Try the following steps:

    1. Install the prettier extension for VScode (https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode)
    2. Install prettier and @prettier/plugin-php locally in the root your workspace: npm install --save-dev prettier @prettier/plugin-php or yarn add --dev prettier @prettier/plugin-php

    Thank you. This worked for me.

  19. Gataquadrada commented on Aug 24, 2022

    @Gataquadrada

    Setting prettier.resolveGlobalModules to true and installing it globally (npm i -g prettier @prettier/plugin-php) fixed all my issues.

  20. Paul-Hebert commented on Jul 26, 2023

    @Paul-Hebert

    I was running into a similar issue this morning.

    In my case it was because my package.json and node_modules directories were not in the root directory I had open in VS Code (they were in a subdirectory.)

    I opened that subdirectory directly and Prettier started working correctly.

  21. Starlette commented on Apr 5, 2025

    @Starlette

    This is what worked for me:

    1. Install "PHP Intelephense" extension
    2. Inside your ".vscode/settings.json" file, add:
    "[php]": {
        "editor.defaultFormatter": "bmewburn.vscode-intelephense-client"
    }
    

    What your settings.json file should look like:

    {
      "editor.tabSize": 2,
      "editor.defaultFormatter": "esbenp.prettier-vscode",
      "editor.formatOnSave": true,
      "[php]": {
        "editor.defaultFormatter": "bmewburn.vscode-intelephense-client"
      }
    }
    

    Source: https://dev.to/ngobrolinit/prettier-for-php-file-formats-in-vscode-104o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions