- What is wp-cli/eval-command used for in a Laravel project?
- It provides the wp eval and wp eval-file commands for executing arbitrary PHP code or files in a WordPress environment. In Laravel projects it matters only when the application includes WordPress, such as a Laravel-based WordPress plugin or hybrid setup. It helps run WordPress-specific snippets, debug WordPress hooks, or bootstrap WordPress logic from the command line. It does not replace Laravel Artisan or Tinker for core Laravel application code.
- How do I install wp-cli/eval-command?
- The package is bundled with WP-CLI, so normal WP-CLI installations already include wp eval and wp eval-file. To install the latest version over the bundled copy, run wp package install [email protected]:wp-cli/eval-command.git. There is no Laravel service provider or package discovery because this is a WP-CLI command package. In a Laravel-WordPress hybrid, install WP-CLI in the environment where WordPress is available.
- Is wp-cli/eval-command compatible with Laravel 10 and Laravel 11?
- The package does not target Laravel versions directly; it depends on WP-CLI and PHP 7.2.24 or higher. Laravel 10 and Laravel 11 require PHP 8.1 or higher, so the PHP requirement is satisfied by default. Actual compatibility depends on your WordPress and WP-CLI integration, not on the Laravel framework version. Test the command in the same PHP and WordPress runtime that your hybrid app uses.
- Can I run wp eval from a Laravel Artisan command?
- Yes, you can shell out to wp eval from an Artisan command or a Laravel process if WP-CLI and WordPress are installed in the target environment. This is useful in Laravel-WordPress hybrids when you need to trigger WordPress hooks or read WordPress options. Be careful with working directories, environment variables, and global state because the evaluated code runs in WP-CLI's process, not in Laravel's container. Keep these calls in trusted local, CI, or admin-only contexts.
- How do I execute code after a specific WordPress hook with wp eval?
- Use the --hook=<hook> option, for example wp eval 'echo wp_get_current_user()->user_login;' --hook=wp_loaded. WP-CLI loads WordPress, waits for the named hook to fire, and then runs your code. This is helpful for debugging plugins or themes that depend on WordPress lifecycle events. Laravel's event system is separate, so cross-framework hook synchronization would require custom code.
- How should I test code that runs through wp eval or wp eval-file?
- Run the commands against a disposable WordPress test installation in CI rather than a shared or production database. Use wp eval-file to check reusable test scripts and assert expected output or side effects in your CI pipeline. Keep the PHP version and WordPress version aligned with production to catch runtime differences. For pure PHP helpers, unit test them outside WP-CLI and reserve eval commands for WordPress integration checks.
- Is wp-cli/eval-command safe to use in production?
- It executes arbitrary PHP code, so it is high risk if exposed to untrusted users or automated web requests. Restrict it to trusted local development, CI, or admin-controlled maintenance windows. Avoid --skip-wordpress in production because it bypasses WordPress loading and can run code in an unexpected context. Add safeguards such as access controls, audit logging, and no eval calls from public automation.
- What is the difference between wp eval and wp eval-file?
- wp eval takes a PHP code string directly on the command line, while wp eval-file loads and executes a PHP file. wp eval-file also supports positional arguments placed in $args, reading from STDIN with '-', --use-include, --skip-wordpress, and --hook. Use wp eval for short snippets and wp eval-file for reusable scripts or larger test files. Both execute code inside a method, so global variables must be declared global explicitly.
- How do I pass arguments or read from STDIN with wp eval-file?
- Add positional arguments after the file path, and wp eval-file places them in the $args variable inside the executed file. For example, wp eval-file script.php foo bar makes $args contain foo and bar. Use '-' as the file path to read PHP code from STDIN. These features help build parameterized WordPress maintenance scripts and CI tasks without writing a custom WP-CLI command.
- What alternatives to wp-cli/eval-command should Laravel developers consider?
- For pure Laravel logic, use php artisan tinker, Artisan commands, or php -a instead of WP-CLI eval. For WordPress-only tasks, custom WP-CLI commands or wp shell may be safer and more maintainable than arbitrary eval snippets. In Laravel-WordPress hybrids, a custom Artisan command that calls WordPress APIs can provide better structure than shelling out to wp eval. The eval-command package is maintained as part of WP-CLI, so track WP-CLI updates for security and compatibility rather than expecting Laravel-specific releases.