php-stubs/wordpress-stubs
WordPress core stubs for static analysis and IDE autocompletion (functions, classes, interfaces; no globals). Generated from johnpbloch/wordpress-core. Works with PHPStan (via phpstan-wordpress) and Psalm stubs config. Requires PHP 7.4/8.0.
Install the stubs in your Laravel project (or WordPress plugin/theme):
composer require --dev php-stubs/wordpress-stubs
Place this in dev-dependencies to avoid bloating production builds.
Configure PHPStan (if using it):
composer require --dev szepeviktor/phpstan-wordpress
phpstan.neon:
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon
First Use Case: Run PHPStan on a WordPress plugin file:
./vendor/bin/phpstan analyse app/Plugins/MyPlugin/Classes/MyClass.php
Expect warnings about undefined WordPress functions/classes (e.g., wp_insert_post()) to disappear.
.intelephense.config.js:
module.exports = {
stubs: ['vendor/php-stubs/wordpress-stubs/wordpress-stubs.php'],
};
Settings > PHP > Include Paths and add the stubs file./app
/Plugins
/MyPlugin
/Classes
MyCustomPostType.php <-- Target for static analysis
my-plugin.php <-- WordPress plugin bootstrap
phpstan.neon):
parameters:
level: 8
paths:
- app/Plugins
excludePaths:
- vendor
- node_modules
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon
MyCustomPostType::create())../vendor/bin/phpstan analyse --memory-limit=1G app/Plugins
@param tags for wp_insert_post()).// app/Services/WPPostService.php
namespace App\Services;
use WP_Post;
class WPPostService {
public function createPost(array $data): WP_Post {
$post_id = wp_insert_post($data); // No IDE warnings now!
return get_post($post_id);
}
}
wp_insert_post() (int).$data (e.g., array{title: string, content: string}).psalm.xml Configuration:<projectFiles>
<directory name="app/Plugins" />
<exclude-name>*.blade.php</exclude-name>
</projectFiles>
<stubs>
<file name="vendor/php-stubs/wordpress-stubs/wordpress-stubs.php" />
</stubs>
<projectFiles> to avoid conflicts.Add to .github/workflows/phpstan.yml:
name: PHPStan
on: [push, pull_request]
jobs:
phpstan:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- run: composer install --dev
- run: ./vendor/bin/phpstan analyse --memory-limit=1G app/Plugins
Globals Are Excluded:
$wpdb, $post, or $wp_query.@var annotations.
/** @var \wpdb $wpdb */
global $wpdb;
Private Methods Missing:
WP_Query::privateMethod()).Version Mismatches:
IDE Lag:
wordpress-stubs.php) can slow down IDEs like PhpStorm..phpstorm.meta.php to exclude the stubs file from indexing:
return [
'exclude' => [
'vendor/php-stubs/wordpress-stubs/wordpress-stubs.php',
],
];
False Positives:
wp_insert_post() as "undefined," ensure:
composer.json).szepeviktor/phpstan-wordpress) is installed../vendor/bin/phpstan analyse --debug app/Plugins/MyPlugin.php
Custom WordPress Functions:
my_custom_function()), add PHPDoc blocks:
/**
* @param string $arg
* @return int
*/
function my_custom_function(string $arg): int { ... }
Psalm-Specific Issues:
WP_Post," ensure:
<stubs>.<projectFiles>.Custom Stubs:
// app/Stubs/MyPluginStubs.php
namespace App\Stubs;
/**
* @method static \App\Plugins\MyPlugin\MyClass create()
*/
class MyPluginStubs {}
includes:
- vendor/szepeviktor/phpstan-wordpress/extension.neon
- app/Stubs/MyPluginStubs.php
PHPStan Rules:
// rules/MyPlugin/NoDirectWPFunctionsRule.php
namespace MyPlugin\Rules;
use PHPStan\Rules\Rule;
use PHPStan\Node\Expr\FuncCallNode;
class NoDirectWPFunctionsRule implements Rule {
public function getNodeTypeNames(): array {
return [FuncCallNode::class];
}
public function processNode(FuncCallNode $node): array {
$functionName = $node->getFunction()->getName();
if (str_starts_with($functionName, 'wp_')) {
return [$node->getFunction(), "Avoid direct WP functions; use services instead."];
}
return [];
}
}
Generating Stubs for Custom WordPress Versions:
git clone https://github.com/php-stubs/generator
cd generator
composer require johnpbloch/wordpress:6.9.1
./generate.sh
Cache Stubs:
./vendor/bin/phpstan clear-cache
Exclude Tests:
phpstan.neon:
excludePaths:
- tests/
- vendor/
Parallel Analysis:
--parallel for large codebases:
./vendor/bin/phpstan analyse --parallel --memory-limit=2G app/Plugins
How can I help you explore Laravel packages today?