wp eval
Execute a PHP expression or a block of code directly within the fully bootstrapped WordPress environment. This is the Swiss Army knife of WP-CLI — perfect for one-off tasks, testing internal function behavior, inspecting globals, or modifying data that lacks a dedicated CLI command.
What It Does
wp eval initializes the WordPress environment (loading wp-config.php, core files, and active plugins) and then evaluates the PHP string you provide. The code runs with full access to all WordPress functions, globals, and constants — exactly as if it were executing inside a plugin.
Syntax
wp eval <php-code> [--skip-wordpress]
Options
| Flag | Description |
|---|---|
--skip-wordpress | Run PHP without bootstrapping WordPress (raw PHP environment only) |
--skip-plugins | Load WP without active plugins |
--skip-themes | Load WP without active themes |
Basic Examples
Print the site URL
wp eval 'echo get_option("siteurl");'
Output:
https://example.com
Get the site name
wp eval 'echo get_bloginfo("name");'
Output:
My WordPress Site
Check the WordPress version from PHP
wp eval 'global $wp_version; echo $wp_version;'
Output:
6.4.2
Check the active theme name
wp eval 'echo wp_get_theme()->get("Name");'
Output:
Twenty Twenty-Four
Check current PHP memory limit
wp eval 'echo ini_get("memory_limit");'
Output:
256M
Inspecting Data
Dump all registered post types
wp eval 'print_r(array_keys(get_post_types()));'
Output:
Array
(
[0] => post
[1] => page
[2] => attachment
[3] => revision
[4] => nav_menu_item
[5] => product
...
)
Count posts of a given type
wp eval 'echo wp_count_posts("post")->publish;'
Output:
142
Inspect a specific option (serialized data)
wp eval 'var_dump(get_option("active_plugins"));'
Output:
array(5) {
[0] => string(27) "akismet/akismet.php"
[1] => string(31) "woocommerce/woocommerce.php"
...
}
Inspect a user object
wp eval 'var_dump(get_userdata(1)->user_email);'
Output:
string(17) "admin@example.com"
Modifying Data
Update a single option
wp eval 'update_option("blogdescription", "The best site on the web.");'
Output: (no output — runs silently)
Verify it worked:
wp eval 'echo get_option("blogdescription");'
Output:
The best site on the web.
Clear a specific transient
wp eval 'var_dump(delete_transient("my_weather_data"));'
Output:
bool(true)
Trigger a custom action hook for testing
wp eval 'do_action("my_plugin_daily_cron");'
Force-send a test email
wp eval 'var_dump(wp_mail("admin@example.com", "Test", "Hello from WP-CLI"));'
Output:
bool(true)
Advanced Usage
Run multi-line code
wp eval '
$args = [
"post_type" => "product",
"post_status" => "publish",
"posts_per_page" => -1,
];
$q = new WP_Query($args);
echo "Products: " . $q->found_posts;
wp_reset_postdata();
'
Output:
Products: 87
Modify a nested serialized option
wp eval '
$settings = get_option("my_plugin_settings");
$settings["api"]["timeout"] = 30;
update_option("my_plugin_settings", $settings);
echo "Updated timeout.";
'
Output:
Updated timeout.
Check database table sizes
wp eval '
global $wpdb;
$tables = $wpdb->get_results("SHOW TABLE STATUS");
foreach ($tables as $t) {
echo $t->Name . ": " . round(($t->Data_length + $t->Index_length) / 1024, 2) . " KB\n";
}
'
Output:
wp_posts: 1024.50 KB
wp_options: 256.75 KB
wp_postmeta: 512.00 KB
...
Inspect a global object
wp eval 'global $current_user; var_dump($current_user->roles);'
Output:
array(1) {
[0] => string(13) "administrator"
}
Shell Escaping Rules
This is the most common source of errors with wp eval.
Rule: Always wrap your PHP in single quotes '...' so the shell does not interpret $ as a shell variable.
✅ Correct — shell won't evaluate $wpdb:
wp eval 'global $wpdb; echo $wpdb->dbname;'
❌ Wrong — shell evaluates $wpdb as an empty shell variable:
wp eval "global $wpdb; echo $wpdb->dbname;"
If you must use double quotes, escape every $:
wp eval "global \$wpdb; echo \$wpdb->dbname;"
Error Handling
PHP errors surface directly in the terminal:
wp eval 'undefined_function_call();'
Output:
PHP Fatal error: Uncaught Error: Call to undefined function undefined_function_call()
wp eval 'echo get_option("siteurl")' # missing semicolon
Output:
PHP Parse error: syntax error, unexpected end of file
Comparison: wp eval vs wp eval-file vs wp shell
| Tool | Best For |
|---|---|
wp eval | Quick one-liners, ad-hoc debugging |
wp eval-file | Multi-step scripts, reusable migrations |
wp shell | Interactive experimentation (REPL) |
Quick Reference
wp eval 'echo get_option("siteurl");' # Print option
wp eval 'var_dump(is_plugin_active("akismet/akismet.php"));' # Debug
wp eval 'update_option("blogname", "New Name");' # Modify
wp eval 'echo wp_count_posts("post")->publish;' # Count
wp eval 'delete_transient("my_key");' # Clear transient
Next Steps
wp eval-file— execute a full PHP file.wp shell— interactive REPL.wp help— look up any command syntax.