Performance: Custom endpoint registration
MEDIUM IMPACT
This affects the server response time and client-side loading speed by adding custom REST API endpoints.
<?php add_action('rest_api_init', function () { global $wpdb; register_rest_route('myplugin/v1', '/data/', array( 'methods' => 'GET', 'callback' => function() { // Use transient cache to store results for 5 minutes global $wpdb; $cached = get_transient('myplugin_data_cache'); if ($cached !== false) { return $cached; } $results = $wpdb->get_results("SELECT * FROM large_table"); set_transient('myplugin_data_cache', $results, 300); return $results; }, )); });
<?php add_action('rest_api_init', function () { global $wpdb; register_rest_route('myplugin/v1', '/data/', array( 'methods' => 'GET', 'callback' => function() { // Heavy database queries without caching global $wpdb; $results = $wpdb->get_results("SELECT * FROM large_table"); return $results; }, )); });
| Pattern | Server Processing | Network Transfer | Client Render Delay | Verdict |
|---|---|---|---|---|
| Uncached heavy queries | High CPU and DB load | Longer due to slow response | Delays LCP by 200+ ms | [X] Bad |
| Cached query results | Low CPU and DB load | Faster due to cached data | Improves LCP by 70-90% | [OK] Good |