Performance Optimization
Performance Optimization
To maintain the high-speed standards of our WordPress implementation, this project utilizes several strategies to reduce server load and improve Time to First Byte (TTFB). Follow these guidelines to ensure your contributions remain performant.
Object Caching
Persistent object caching is the most effective way to reduce database load. By default, this project is configured to support Redis or Memcached drop-ins.
When performing expensive operations—such as complex API calls or heavy data processing—always wrap the logic in the WordPress Cache API.
Usage Example:
/**
* Retrieves data with a cache-first approach.
*
* @param int $user_id The ID of the user to fetch data for.
* @return array The processed data.
*/
function get_processed_user_stats( $user_id ) {
$cache_key = 'user_stats_' . $user_id;
$cache_group = 'user_metrics';
// Attempt to retrieve from cache
$stats = wp_cache_get( $cache_key, $cache_group );
if ( false === $stats ) {
// Expensive database or logic operation here
$stats = perform_heavy_calculation( $user_id );
// Store in cache for 1 hour (3600 seconds)
wp_cache_set( $cache_key, $stats, $cache_group, 3600 );
}
return $stats;
}
Query Optimization
Inefficient database queries are a common bottleneck. When using WP_Query, adhere to these performance-first parameters to bypass unnecessary overhead:
no_found_rows: Set totrueif pagination is not required. This skipsSQL_CALC_FOUND_ROWS, significantly speeding up the query.update_post_meta_cache: Set tofalseif you do not need post metadata for the returned results.fields: Only request'ids'if you only need the ID array, which reduces memory consumption.
Optimized Query Example:
$args = [
'post_type' => 'post',
'posts_per_page' => 10,
'no_found_rows' => true, // Skips pagination count for speed
'update_post_meta_cache' => false, // Reduces metadata overhead
'update_post_term_cache' => false, // Reduces term relationship overhead
'fields' => 'ids', // Returns only post IDs
];
$optimized_query = new WP_Query( $args );
Asset Minification and Enqueuing
This project uses a standardized enqueuing pattern to serve minified assets in production environments while allowing for easier debugging in development.
Automatic Suffix Handling
The codebase checks the SCRIPT_DEBUG constant to determine whether to load .min.js or the source .js file.
Implementation Example:
/**
* Enqueues theme scripts with minification support.
*/
function project_enqueue_scripts() {
// Determine suffix based on environment
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
wp_enqueue_script(
'project-main-js',
get_template_directory_uri() . "/assets/js/main{$suffix}.js",
['jquery'],
'1.0.0',
true
);
}
add_action( 'wp_enqueue_scripts', 'project_enqueue_scripts' );
Best Practices for Assets:
- Minification: Always provide a
.min.jsand.min.cssversion of your assets in thedistorassetsdirectories. - Defer/Async: For non-critical scripts, use the
script_loader_tagfilter to adddeferorasyncattributes to the generated script tags. - Conditional Loading: Only enqueue scripts on the specific templates or post types where they are required to minimize the global payload size.