wp-cli/eval-command
Adds WP-CLI commands to run arbitrary PHP code or execute PHP files from the command line. Supports running with or without loading WordPress, delaying execution until a specific hook, and passing args to scripts.
tinker or artisan for core Laravel logic.--hook=<hook>) aligns with WordPress’s event-driven architecture, making it ideal for debugging plugins/themes tied to WordPress lifecycle events. Laravel’s event system differs, so cross-framework hook synchronization would require custom adapters.wp eval runs in the global scope, which can conflict with Laravel’s autoloading or service container. Mitigation: Use --skip-wordpress for isolated execution or wrap calls in explicit namespaces.wp eval could expose WordPress internals or Laravel’s shared environment (if integrated). Mitigation: Restrict usage to trusted environments (e.g., CI, local dev) and avoid --skip-wordpress in production.$wpdb) may clash. Mitigation: Use global $var; explicitly or scope evaluations to specific contexts.eval’d code may obscure Laravel/WP-CLI call stacks. Mitigation: Log context (e.g., wp eval 'error_log("Stack: " . debug_backtrace()); ...') or use Xdebug.eval calls in loops could degrade performance. Mitigation: Cache compiled scripts or use eval-file for reusable logic.artisan schedule:run) or queue workers?php artisan tinker or php -a suffice for non-WordPress logic? If not, why is WP-CLI’s eval-command necessary?wp-cli facade?wp_get_current_user()) from Laravel CLI.wp eval-file plugin-test.php --hook=wp_loaded).tinker, artisan) instead.wp eval/eval-file equivalents (e.g., replace ssh user@server "php -r 'echo get_option('foo');'" with wp eval 'echo get_option("foo");').composer require wp-cli/wp-cli
wp eval in a sandboxed Laravel Valet/Sail container to validate isolation from Laravel’s autoloader.artisan command with a WordPress-specific wp eval call:
// Before (Laravel-only)
Artisan::call('command:custom-logic');
// After (WordPress + Laravel hybrid)
$output = shell_exec('wp eval \'global $wpdb; echo $wpdb->get_var("SELECT COUNT(*) FROM posts");\'');
wp eval into Laravel’s artisan as a custom command (e.g., php artisan wp:eval):
// In Laravel's app/Console/Kernel.php
protected $commands = [
\App\Console\Commands\WpEvalCommand::class,
];
--skip-wordpress for Laravel-only logic").wp eval-file for reusable logic.| Factor | Compatibility | Mitigation |
|---|---|---|
| PHP Version | PHP 7.2.24+ (Laravel 8.0+ is compatible) | Upgrade PHP if using older Laravel versions. |
| WP-CLI Version | Requires WP-CLI v2.12+ (bundled with WP-CLI 2.7+) | Update WP-CLI: wp cli update. |
| WordPress Integration | Tightly coupled to WordPress core. | Use --skip-wordpress for non-WP logic or wrap calls in WordPress bootstrap. |
| Laravel Autoloader | May conflict with eval’d code (e.g., undefined classes). |
Prepend eval with spl_autoload_register() adjustments or use eval-file. |
| Hook System | WordPress hooks only (e.g., wp_loaded). |
For Laravel events, use eval-file with custom event listeners. |
php -r or SSH-based PHP execution with wp eval.wp_enqueue_scripts hook:
wp eval 'add_action("wp_enqueue_scripts", function() { var_dump(wp_scripts()->registered); });'
wp eval-file.wp eval-file cleanup.php --hook=wp_loaded
wp eval to Laravel’s phpunit.xml for pre-commit testing:
<php>
<ini name="error_reporting" value="-1"/>
<file name="vendor/autoload.php"/>
<file name="vendor/wp-cli/wp-cli/bin/wp"/>
</php>
- name: Run WP-CLI eval tests
run: wp eval-file tests/eval/test-script.php
// app/Console/Commands/WpDebugCommand.php
public function handle() {
$output = shell_exec('wp eval \'global $wpdb; return $wpdb->get_results("SELECT * FROM posts LIMIT 5");\'');
$this->info($output);
}
How can I help you explore Laravel packages today?