spatie/wordpress-ray
Send debug output from WordPress to Ray, Spatie’s desktop debugging app. Use a consistent debugging API to inspect dumps, arrays, HTML, queries, and more, measure performance, and pause execution—all from your WordPress project.
Install the Package
composer require spatie/wordpress-ray
Add to wp-config.php (before WP_DEBUG):
require __DIR__ . '/vendor/autoload.php';
$wpRay = new \Spatie\WordPressRay\WordPressRay();
Configure Ray
wp-config.php:
$wpRay->setApiKey('your-ray-api-key');
First Debug Output
Use ray() helper (auto-loaded) or $wpRay->ray():
ray($this->get_post(), 'Current Post Data');
// or
$wpRay->ray($user, 'User Object');
Debug a custom WordPress plugin/theme:
add_action('wp_enqueue_scripts', function() {
$query = new WP_Query(['post_type' => 'product']);
ray($query->posts, 'Products Query Result');
});
Replacing var_dump/error_log
Replace legacy debugging with ray() for structured output:
// Before
error_log(print_r($user, true));
// After
ray($user, 'User Object');
Conditional Debugging
Use WP_DEBUG to toggle Ray output:
if (WP_DEBUG) {
ray($this->get_options(), 'Plugin Options');
}
Performance Profiling Measure execution time:
$wpRay->startTimer('plugin_init');
// ... code ...
$wpRay->stopTimer('plugin_init');
Query Debugging Inspect database queries:
global $wpdb;
$results = $wpdb->get_results("SELECT * FROM wp_posts");
ray($results, 'Raw Posts');
Hooks & Filters Debug hook execution:
add_action('init', function() {
ray(current_filter(), 'Current Hook');
});
REST API Debugging Log API responses:
add_filter('rest_pre_serve_request', function($response) {
ray($response, 'API Response');
return $response;
});
Theme Development Debug template data:
add_action('wp_head', function() {
ray(get_post_meta(get_the_ID()), 'Post Meta');
});
CLI Debugging Use Ray in WP-CLI scripts:
// wp-cli-script.php
require __DIR__ . '/vendor/autoload.php';
$wpRay = new \Spatie\WordPressRay\WordPressRay();
$wpRay->setApiKey(getenv('RAY_API_KEY'));
ray(wp_list_plugins(), 'Installed Plugins');
API Key Leaks
wp-config.php with hardcoded API keys. Use environment variables:
$wpRay->setApiKey(getenv('RAY_API_KEY'));
Performance Overhead
if (!defined('WP_ENV') || WP_ENV !== 'production') {
$wpRay->setApiKey(getenv('RAY_API_KEY'));
}
Large Data Dumps
WP_Query results with posts_per_page=-1). Use array_slice():
ray(array_slice($posts, 0, 10), 'First 10 Posts');
Ray App Not Connecting
Inspect Ray Messages
Use ray() with context:
ray($this->get_errors(), 'Plugin Errors', ['context' => 'admin']);
Group Related Outputs Use tags for organization:
ray($user, 'User Data', ['tags' => ['auth', 'user']]);
Pause Execution
Use ray()->pause() to halt script execution for inspection.
Markdown Support Format output with Markdown:
ray("# User Data\n\n- Name: {$user->name}\n- Role: {$user->roles[0]}");
Custom Handlers
Extend Spatie\WordPressRay\WordPressRay to add custom logic:
$wpRay->extend(function($data, $label, $tags = []) {
if (is_wp_error($data)) {
return ray($data->get_error_message(), $label, $tags);
}
return $data;
});
Override Default Ray Client Swap the underlying Ray client for custom transport:
$wpRay->setRayClient(new \Spatie\Ray\RayClient('custom-endpoint'));
Hook into Ray Events Listen for message sent events:
$wpRay->onMessageSent(function($message) {
do_action('wp_ray_message_sent', $message);
});
Theme-Specific Output Create a custom Ray theme for WordPress:
$wpRay->setTheme('wordpress');
// Requires Ray Pro or custom theme setup
How can I help you explore Laravel packages today?