0
0
Wordpressframework~8 mins

Custom endpoint registration in Wordpress - Performance & Optimization

Choose your learning style9 modes available
Performance: Custom endpoint registration
MEDIUM IMPACT
This affects the server response time and client-side loading speed by adding custom REST API endpoints.
Adding a custom REST API endpoint to serve data
Wordpress
<?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;
    },
  ));
});
Caching reduces database load and speeds up API response times.
📈 Performance GainReduces server response time by 70-90%, improving LCP.
Adding a custom REST API endpoint to serve data
Wordpress
<?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;
    },
  ));
});
Heavy database queries run on every request without caching, causing slow server responses.
📉 Performance CostBlocks server response for 200+ ms on each API call, increasing LCP.
Performance Comparison
PatternServer ProcessingNetwork TransferClient Render DelayVerdict
Uncached heavy queriesHigh CPU and DB loadLonger due to slow responseDelays LCP by 200+ ms[X] Bad
Cached query resultsLow CPU and DB loadFaster due to cached dataImproves LCP by 70-90%[OK] Good
Rendering Pipeline
Custom endpoints affect the server-side response time, which impacts when the browser can start rendering the main content.
Server Processing
Network Transfer
First Paint
⚠️ BottleneckServer Processing due to heavy or uncached data queries in endpoint callbacks.
Core Web Vital Affected
LCP
This affects the server response time and client-side loading speed by adding custom REST API endpoints.
Optimization Tips
1Avoid heavy database queries in endpoint callbacks without caching.
2Use transient or object caching to store endpoint data temporarily.
3Test endpoint response times in DevTools Network panel to ensure fast server replies.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk when registering a custom REST API endpoint without caching?
AIncreased client-side JavaScript bundle size
BSlow server response due to repeated heavy database queries
CMore CSS reflows on the page
DLonger DOM tree construction
DevTools: Network
How to check: Open DevTools > Network tab, filter by your custom endpoint URL, and observe the response time.
What to look for: Look for long server response times (Time to First Byte) indicating slow endpoint processing.