Psalm Plugin for WordPress
Write type-safe WordPress code. This plugin supports WordPress 3 and newer (4, 5, 6, currently WP 6.7) and generally also works on WordPress trunk and beta/RC versions.
This Psalm plugin provides all WordPress and WP CLI stubs, so your WordPress based project or plugin will have type information for calls to WordPress APIs. This ensures your WordPress plugin or theme has less bugs!
Stubs for all of WordPress Core
Stubs for WP CLI
Types for apply_filters return values.
Types for add_filter / add_action
Configuration options to use your own stubs
Installation
Please refer to the full Psalm documentation for a more detailed guide on introducing Psalm into your project.After Psalm is installed, install this package and enable the plugin:composer require --dev humanmade/psalm-plugin-wordpress
./vendor/bin/psalm-plugin enable humanmade/psalm-plugin-wordpress
WordPress paths
To help Psalm analyze your project you might need to define some of WordPress’ default global constants such as those for paths.
<?xml version="1.0"?> <psalm autoloader="tests/bootstrap.php" xmlns="https://getpsalm.org/schema/config"> <!-- project configuration --> </psalm>
The following example bootstrap file is for a Bedrock installation:
<?php require_once dirname( __DIR__ ) . '/config/application.php'; define( 'WPMU_PLUGIN_DIR', WP_CONTENT_DIR . '/mu-plugins' ); define( 'WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
You could require WordPress’ default constants functions but that requires a lot more boilerplating to allow those functions to effectively define constants.
