Skip to content

Instantly share code, notes, and snippets.

@vapvarun
Created May 20, 2026 03:15
Show Gist options
  • Select an option

  • Save vapvarun/577a3b03e9c523484605b21c526b7450 to your computer and use it in GitHub Desktop.

Select an option

Save vapvarun/577a3b03e9c523484605b21c526b7450 to your computer and use it in GitHub Desktop.
WordPress Performance Optimization and Core Web Vitals 2026 Guide (wppioneer.com)
-- Query Monitor shows this in its panel; here's the raw SQL to find slow queries
-- Run in phpMyAdmin or CLI: mysql -u user -p db_name
SELECT
query_time,
lock_time,
rows_examined,
rows_sent,
sql_text
FROM mysql.slow_log
WHERE start_time > NOW() - INTERVAL 1 HOUR
ORDER BY query_time DESC
LIMIT 20;
<?php
// wp-config.php performance settings — add before "That's all, stop editing!" comment
// 1. Disable post revisions (keep last 3 only)
define( 'WP_POST_REVISIONS', 3 );
// 2. Disable auto-save to reduce DB writes during editing
define( 'AUTOSAVE_INTERVAL', 300 ); // 5 minutes instead of 60 seconds
// 3. Empty trash every 7 days instead of 30
define( 'EMPTY_TRASH_DAYS', 7 );
// 4. Disable the link manager (legacy, unused on modern sites)
define( 'LINK_MANAGER_ENABLED', false );
// 5. Increase memory limit if hitting the default 40M
define( 'WP_MEMORY_LIMIT', '256M' );
// 6. Allow uploads of larger files for media optimization workflows
define( 'WP_MAX_UPLOAD_SIZE', '64M' );
-- Find autoloaded options that are bloating wp_options
-- Run in phpMyAdmin or via WP-CLI: wp db query "SELECT ..."
-- Total autoload size
SELECT
SUM(LENGTH(option_value)) AS total_autoload_bytes,
ROUND(SUM(LENGTH(option_value)) / 1024, 1) AS total_autoload_kb,
COUNT(*) AS total_autoload_rows
FROM wp_options
WHERE autoload = 'yes';
-- Top 20 largest autoloaded options (find the culprits)
SELECT
option_name,
autoload,
ROUND(LENGTH(option_value) / 1024, 2) AS size_kb
FROM wp_options
WHERE autoload = 'yes'
ORDER BY LENGTH(option_value) DESC
LIMIT 20;
#!/usr/bin/env bash
# WP-CLI database cleanup commands
# Run these from your site root (where wp-config.php lives)
# 1. Delete all post revisions
wp post delete $(wp post list --post_type=revision --format=ids) --force
# 2. Delete orphaned post metadata
wp db query "DELETE pm FROM wp_postmeta pm LEFT JOIN wp_posts wp ON wp.ID = pm.post_id WHERE wp.ID IS NULL"
# 3. Delete expired transients
wp transient delete --expired
# 4. Delete ALL transients (nuclear option if transients are heavily stale)
wp transient delete --all
# 5. Delete spam and trashed comments
wp comment delete $(wp comment list --status=spam --format=ids) --force
wp comment delete $(wp comment list --status=trash --format=ids) --force
# 6. Optimize all tables after cleanup
wp db optimize
# 7. Check total database size before/after
wp db size --tables
# Nginx FastCGI cache config for WordPress
# Add to your Nginx server block (usually /etc/nginx/sites-available/yoursite.conf)
# 1. Define the cache zone (add to nginx.conf http{} block)
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
# 2. Server block settings
server {
# ... your existing server block ...
set $skip_cache 0;
# Skip cache for logged-in users and WooCommerce pages
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|woocommerce_cart_hash|woocommerce_items_in_cart") {
set $skip_cache 1;
}
if ($request_method = POST) { set $skip_cache 1; }
if ($query_string != "") { set $skip_cache 1; }
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap") {
set $skip_cache 1;
}
location ~ \.php$ {
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 60m;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-FastCGI-Cache $upstream_cache_status;
# ... your existing fastcgi_pass config ...
}
}
<?php
/**
* Inline critical CSS and defer non-critical stylesheets.
* Add to your theme's functions.php or a site-specific plugin.
*
* Strategy: inline the above-the-fold CSS, load full stylesheet async.
* Only do this after extracting critical CSS with a tool like criticalcss.com or Penthouse.
*/
add_action( 'wp_head', 'my_inline_critical_css', 1 );
function my_inline_critical_css() {
$critical_css_file = get_template_directory() . '/critical.css';
if ( file_exists( $critical_css_file ) ) {
echo '<style id="critical-css">';
echo file_get_contents( $critical_css_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
echo '</style>';
}
}
/**
* Convert render-blocking stylesheets to load asynchronously.
* Applies to non-critical CSS registered via wp_enqueue_style().
* Excludes: critical-css (inlined above), admin, login.
*/
add_filter( 'style_loader_tag', 'my_defer_non_critical_css', 10, 4 );
function my_defer_non_critical_css( $html, $handle, $href, $media ) {
$exclude = array( 'critical-css', 'admin-bar', 'wp-block-library' );
if ( in_array( $handle, $exclude, true ) || is_admin() ) {
return $html;
}
// Load async via link rel=preload trick
$html = sprintf(
'<link rel="preload" id="%s-css" href="%s" as="style" onload="this.onload=null;this.rel=\'stylesheet\'">' . "\n",
esc_attr( $handle ),
esc_url( $href )
);
$html .= sprintf(
'<noscript><link rel="stylesheet" id="%s-css" href="%s"></noscript>' . "\n",
esc_attr( $handle ),
esc_url( $href )
);
return $html;
}
#!/usr/bin/env bash
# Set up Redis object cache on Ubuntu/Debian with WP-CLI
# Assumes PHP-FPM and Nginx stack
# 1. Install Redis server
sudo apt-get install -y redis-server
# 2. Enable and start Redis
sudo systemctl enable redis-server
sudo systemctl start redis-server
# 3. Install PHP Redis extension
sudo apt-get install -y php-redis
# 4. Restart PHP-FPM to load the extension
sudo systemctl restart php8.2-fpm # adjust version to match your PHP
# 5. Install the Redis Object Cache plugin via WP-CLI
wp plugin install redis-cache --activate --path=/var/www/html
# 6. Add Redis config to wp-config.php (run once)
wp config set WP_CACHE true --raw --path=/var/www/html
wp config set WP_REDIS_HOST '127.0.0.1' --path=/var/www/html
wp config set WP_REDIS_PORT '6379' --raw --path=/var/www/html
wp config set WP_REDIS_DATABASE '0' --raw --path=/var/www/html
# 7. Enable the object cache drop-in
wp redis enable --path=/var/www/html
# 8. Verify connection
wp redis status --path=/var/www/html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment