VS Code highlighted all my wordpress function name
Asked Answered
N

8

106

I am using PHP Intelephense(version 1.3.7) which is the latest version and my VS Code is up to date There was no problem before but a few days ago, it keeps on highlighted all my wordpress function name I tried to downgrade my PHP Intelephense but the situation still exist.. Any help will be appreciate

enter image description here

Nea answered 24/1, 2020 at 5:33 Comment(0)
B
172

You have to add "wordpress" to the intelephense.stubs array setting in your VS Code settings file. This array should probably also have many other stubs listed as well. For example: ("wordpress" is at the bottom)

//  VS Code settings.json
{
    // ... other settings here ...
    "intelephense.files.maxSize": 3000000,
    "intelephense.stubs": [
        "apache",
        "bcmath",
        "bz2",
        "calendar",
        "com_dotnet",
        "Core",
        "ctype",
        "curl",
        "date",
        "dba",
        "dom",
        "enchant",
        "exif",
        "FFI",
        "fileinfo",
        "filter",
        "fpm",
        "ftp",
        "gd",
        "gettext",
        "gmp",
        "hash",
        "iconv",
        "imap",
        "intl",
        "json",
        "ldap",
        "libxml",
        "mbstring",
        "meta",
        "mysqli",
        "oci8",
        "odbc",
        "openssl",
        "pcntl",
        "pcre",
        "PDO",
        "pdo_ibm",
        "pdo_mysql",
        "pdo_pgsql",
        "pdo_sqlite",
        "pgsql",
        "Phar",
        "posix",
        "pspell",
        "readline",
        "Reflection",
        "session",
        "shmop",
        "SimpleXML",
        "snmp",
        "soap",
        "sockets",
        "sodium",
        "SPL",
        "sqlite3",
        "standard",
        "superglobals",
        "sysvmsg",
        "sysvsem",
        "sysvshm",
        "tidy",
        "tokenizer",
        "xml",
        "xmlreader",
        "xmlrpc",
        "xmlwriter",
        "xsl",
        "Zend OPcache",
        "zip",
        "zlib",
        "wordpress"
    ]
}

You can also take a look at these github issues:

Basque answered 21/2, 2020 at 4:6 Comment(4)
This is great for WP functions/clasess support itself, if you are however still get some red highlighted functions - they may be from a plugin - so you'd need to stub or add the plugin code to the project.Heliotropism
Thanks so much for this answer, I was banging my head against the wall trying to figure this out. In the VS Code settings I found the intelephense stubs by just searching on "stubs" and was able to add "wordpress" to the bottom of the list.Evangelical
this makes my life easier, not only wordpress but other functions are understood by vscode with thisLoar
If you don't add the list drwatsoncode provided, you will slowly grow to that list as you use various php functions and the plugin marks them undefined. Feels odd for these to be off by defaultConsidered
M
150

The quick fix for this is simply to add WordPress to the Intelephense: Stubs list.

1: Press (Ctrl+Shift+X) to the extensions bar on the left and search for PHP Intelephense.

2: Click the settings icon of the extension and choose Extension Settings.

screenshot of extensions bar

3: Scroll down to the bottom to see the list of Stubs.

4: Click on Add Item and choose wordpress from the list.

screenshot of extensions settings menu

If the changes are not affected, try to close and reopen Vscode.

This will solve the issue with built-in WordPress Functions, However, it will not recognize any functions from installed plugins.

Moravia answered 13/12, 2020 at 12:30 Comment(2)
Thank you! Screenshot really helps.Withdrawn
Thank you for the full procedure. But I have the problem even wordpress is in the list ...Submit
H
6

Maybe I'm missing something, but I never have this problem, and I think the reason is just that I open my whole WordPress site directory in VS Code at once.

Are you opening only your theme directory?

If so it makes sense that these functions are missing since they aren't in the project.

Open the whole site and this should go away. You'll also get all your theme and plugin functions imported correctly.

Hysterectomy answered 27/6, 2021 at 23:7 Comment(1)
after doing all of the above. this was the answerBedim
V
2

I found the perfect solution that actually includes all Wordpress's definitions, so the IntelliSense / Interpreter for PHP will recognize all Wordpress's functions, while developing a plugin or anything related to Wordpress.

  1. Git Clone WordPress source code locally to wordpress - Later on you'll need the absolute path to the wordpress directory
    git clone --depth 1 --single-branch [email protected]:WordPress/WordPress.git wordpress
    
  2. VSCode > Install the PHP Intelephense
  3. VSCode > Edit Settings > I usually include a .vscode/settings.json in my projects; here's how you can :
    {
      "editor.formatOnSave": true,
      "intelephense.environment.includePaths": ["/absolute/path/to/cloned/wordpress"]
    }
    
  4. VSCode shows WordPress's definitions as it should 🥳

Here's a demo of how it works - https://youtu.be/yLgd5OKRXxM

Opinion: I prefer cloning WordPress locally in case I need to debug something during my application development, I'm not sure the other alternatives have this capability.

P.S: Yes, I got that excited about the solution, so I recorded a video with cinematic music :)

Violaceous answered 28/1 at 21:8 Comment(0)
A
1

If you load only child theme folder in vs code than it give you highlight on wordpress function. Instead of this, load whole WordPress folder in vs code. It's works for me. Thanks.

Almoner answered 24/8, 2023 at 19:34 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Auspicate
P
1

I solved this as follows:

Open Your settings.json File: Ctrl + ,

Click on the {} icon in the top right corner to view the JSON settings

Locate the php.stubs Setting. If it's not available, add it.

Add "wordpress" to the Array

Your php.stubs setting should now look something like this:

"php.stubs": [
    "wordpress",
    "*"
]

Save Your Changes

Pleach answered 23/1 at 9:42 Comment(0)
D
0

you just need to add WordPress item to the Intelephense: Stubs from the extension setting.

enter image description here

Dealate answered 5/5, 2022 at 14:20 Comment(1)
Duplicate solution with less detail than https://mcmap.net/q/203459/-vs-code-highlighted-all-my-wordpress-function-name from over a year priorLamoureux
D
-1

Adam allalou and drwatsoncode are right and they work but just a reminder, to be sure that the changes are effected, don't forget to close and open your VsCode editor again.

Dimmer answered 9/9, 2021 at 13:27 Comment(1)
As well as not actually being an answer, this does not reflect my experience.Lamoureux

© 2022 - 2024 — McMap. All rights reserved.