Configuration
Advanced WordPress configuration and optimization settings
Configuration Management
Master advanced WordPress configuration options for optimal performance, security, and functionality. Learn to customize wp-config.php, optimize server settings, and implement advanced configurations.
WordPress Configuration Files
wp-config.php Optimization
Database Configuration
// Optimized database settings
define('DB_CHARSET', 'utf8mb4');
define('DB_COLLATE', 'utf8mb4_unicode_ci');
// Database connection optimization
define('DB_HOST', 'localhost:3306'); // Specify port if needed
define('DB_MAX_CONNECTIONS', 10); // Limit connectionsSecurity Enhancements
// Security keys (generate unique ones)
define('AUTH_KEY', 'your-unique-auth-key');
define('SECURE_AUTH_KEY', 'your-unique-secure-auth-key');
define('LOGGED_IN_KEY', 'your-unique-logged-in-key');
define('NONCE_KEY', 'your-unique-nonce-key');
define('AUTH_SALT', 'your-unique-auth-salt');
define('SECURE_AUTH_SALT', 'your-unique-secure-auth-salt');
define('LOGGED_IN_SALT', 'your-unique-logged-in-salt');
define('NONCE_SALT', 'your-unique-nonce-salt');
// Additional security settings
define('DISALLOW_FILE_EDIT', true);
define('DISALLOW_FILE_MODS', false); // Allow plugin/theme updates
define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);Performance Optimization
// Memory limits
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');
// Post revisions
define('WP_POST_REVISIONS', 5); // Limit revisions
define('AUTOSAVE_INTERVAL', 300); // 5 minutes
// Object caching
define('WP_CACHE', true);
define('WP_CACHE_KEY_SALT', 'your-unique-salt');
// File system
define('UPLOADS', 'wp-content/uploads');
define('WP_TEMP_DIR', '/tmp/wordpress');Advanced wp-config.php Settings
Development Environment
// Development settings
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
define('SCRIPT_DEBUG', true);
define('SAVEQUERIES', true);
// Disable caching in development
define('WP_CACHE', false);
define('DISABLE_WP_CRON', true);Production Environment
// Production optimizations
define('WP_DEBUG', false);
define('WP_DEBUG_LOG', false);
define('WP_DEBUG_DISPLAY', false);
define('ENFORCE_GZIP', true);
// Enable caching
define('WP_CACHE', true);
define('WP_SUPER_CACHE', true);Multisite Configuration
// Enable multisite
define('WP_ALLOW_MULTISITE', true);
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true); // or false for subdirectories
define('DOMAIN_CURRENT_SITE', 'yourdomain.com');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);Server Configuration
PHP Optimization
php.ini Settings
; Memory and execution
memory_limit = 256M
max_execution_time = 300
max_input_time = 300
; File uploads
upload_max_filesize = 64M
post_max_size = 64M
max_file_uploads = 20
; Performance
opcache.enable = 1
opcache.memory_consumption = 256
opcache.max_accelerated_files = 7963
opcache.revalidate_freq = 0
; Security
expose_php = Off
display_errors = Off
log_errors = OnApache Configuration
.htaccess Optimization
# Enable compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>
# Browser caching
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 week"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
</IfModule>
# Security headers
<IfModule mod_headers.c>
Header always set X-Frame-Options DENY
Header always set X-Content-Type-Options nosniff
Header always set X-XSS-Protection "1; mode=block"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
</IfModule>Nginx Configuration
Server Block Optimization
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/html;
index index.php index.html;
# Security headers
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
# Gzip compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# Browser caching
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# PHP handling
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 300;
}
}Database Optimization
MySQL Configuration
my.cnf Optimization
[mysqld]
# Connection settings
max_connections = 100
max_connect_errors = 100000
# Memory settings
innodb_buffer_pool_size = 1G
innodb_log_file_size = 256M
innodb_log_buffer_size = 16M
# Performance
query_cache_size = 256M
query_cache_type = ON
query_cache_limit = 8M
# Security
skip_name_resolve = ON
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZEROWordPress Database Settings
// Database optimization constants
define('WP_ALLOW_REPAIR', true); // Enable repair functionality
define('DO_NOT_UPGRADE_GLOBAL_TABLES', true); // For multisite
define('CUSTOM_USER_TABLE', 'my_users'); // Custom user table
define('CUSTOM_USER_META_TABLE', 'my_usermeta'); // Custom user meta tableDatabase Maintenance
-- Optimize tables
OPTIMIZE TABLE wp_posts;
OPTIMIZE TABLE wp_postmeta;
OPTIMIZE TABLE wp_users;
OPTIMIZE TABLE wp_usermeta;
-- Clean up orphaned data
DELETE pm FROM wp_postmeta pm
LEFT JOIN wp_posts p ON p.ID = pm.post_id
WHERE p.ID IS NULL;
-- Remove spam comments
DELETE FROM wp_comments WHERE comment_approved = 'spam';
-- Remove old transients
DELETE FROM wp_options WHERE option_name LIKE '_transient_%' AND option_value < NOW();Caching Configuration
Object Caching
Redis Configuration
// Redis object cache settings
define('WP_REDIS_HOST', '127.0.0.1');
define('WP_REDIS_PORT', 6379);
define('WP_REDIS_PASSWORD', 'your-redis-password');
define('WP_REDIS_DATABASE', 0);
define('WP_CACHE_KEY_SALT', 'your-unique-salt');Memcached Configuration
// Memcached settings
define('WP_MEMCACHED_HOST', '127.0.0.1');
define('WP_MEMCACHED_PORT', 11211);
define('WP_MEMCACHED_KEY_SALT', 'your-unique-salt');Page Caching
WP Super Cache Configuration
// WP Super Cache settings
define('WPCACHEHOME', '/path/to/wp-content/plugins/wp-super-cache/');
define('WP_CACHE', true);
define('WPCACHEPAGE', true);
define('WPCACHERSS', true);W3 Total Cache Configuration
// W3 Total Cache settings
define('W3TC_CONFIG_DATABASE', true);
define('W3TC_CONFIG_FILE', true);
define('W3TC_CONFIG_EDGE_MODE', false);CDN Integration
CDN Configuration
// CDN settings
define('CDN_DOMAIN', 'cdn.yourdomain.com');
define('CDN_CONTENT_TYPES', 'jpg|jpeg|png|gif|css|js|ico|svg|woff|woff2');
define('CDN_EXCLUDE', 'wp-admin|wp-login');
// CDN URL rewriting
function cdn_rewrite_url($url) {
$content_types = explode('|', CDN_CONTENT_TYPES);
$exclude_patterns = explode('|', CDN_EXCLUDE);
// Skip excluded patterns
foreach ($exclude_patterns as $pattern) {
if (strpos($url, $pattern) !== false) {
return $url;
}
}
// Rewrite CDN URLs
return str_replace(site_url(), CDN_DOMAIN, $url);
}
add_filter('wp_get_attachment_url', 'cdn_rewrite_url');Email Configuration
SMTP Settings
// SMTP configuration
define('SMTP_HOST', 'smtp.yourdomain.com');
define('SMTP_PORT', 587);
define('SMTP_SECURE', 'tls'); // or 'ssl'
define('SMTP_AUTH', true);
define('SMTP_USER', 'your-email@yourdomain.com');
define('SMTP_PASS', 'your-email-password');
define('SMTP_FROM', 'noreply@yourdomain.com');
define('SMTP_FROM_NAME', 'Your Site Name');Email Delivery Optimization
// Email queue settings
define('EMAIL_QUEUE_ENABLED', true);
define('EMAIL_QUEUE_BATCH_SIZE', 50);
define('EMAIL_QUEUE_INTERVAL', 300); // 5 minutes
// Email logging
define('EMAIL_LOG_ENABLED', true);
define('EMAIL_LOG_PATH', '/path/to/email-logs/');Security Configuration
Firewall Rules
// WordPress firewall settings
define('WP_FIREWALL_ENABLED', true);
define('WP_FIREWALL_BLOCKED_IPS', '192.168.1.100,10.0.0.1');
define('WP_FIREWALL_ALLOWED_USER_AGENTS', 'Googlebot,Bingbot');
// File upload restrictions
define('WP_UPLOAD_RESTRICTIONS', true);
define('WP_ALLOWED_FILE_TYPES', 'jpg,jpeg,png,gif,pdf,doc,docx');
define('WP_MAX_FILE_SIZE', 10485760); // 10MBSSL/TLS Configuration
// SSL settings
define('WP_SSL_ENABLED', true);
define('WP_SSL_REDIRECT', true);
define('WP_SSL_FORCE_ADMIN', true);
// HSTS settings
define('WP_HSTS_ENABLED', true);
define('WP_HSTS_MAX_AGE', 31536000); // 1 year
define('WP_HSTS_INCLUDE_SUBDOMAINS', true);Backup Configuration
Automated Backup Settings
// Backup configuration
define('WP_BACKUP_ENABLED', true);
define('WP_BACKUP_FREQUENCY', 'daily'); // daily, weekly, monthly
define('WP_BACKUP_RETENTION', 30); // days to keep backups
define('WP_BACKUP_METHOD', 'zip'); // zip or tar.gz
define('WP_BACKUP_LOCATION', '/path/to/backups/');
// Database backup settings
define('WP_DB_BACKUP_ENABLED', true);
define('WP_DB_BACKUP_COMPRESS', true);
define('WP_DB_BACKUP_TABLES', 'all'); // all, core, customPerformance Monitoring
Performance Settings
// Performance monitoring
define('WP_PERFORMANCE_MONITORING', true);
define('WP_PERFORMANCE_LOG_QUERIES', true);
define('WP_PERFORMANCE_SLOW_QUERY_THRESHOLD', 0.5); // seconds
// Resource monitoring
define('WP_RESOURCE_MONITORING', true);
define('WP_MEMORY_THRESHOLD', 100); // MB
define('WP_CPU_THRESHOLD', 80); // percentageCustom Configuration
Environment-Specific Settings
// Environment detection
$environment = wp_get_environment_type(); // development, staging, production
switch ($environment) {
case 'development':
define('WP_DEBUG', true);
define('WP_CACHE', false);
define('SAVEQUERIES', true);
break;
case 'staging':
define('WP_DEBUG', false);
define('WP_CACHE', true);
define('WP_DEBUG_LOG', true);
break;
case 'production':
define('WP_DEBUG', false);
define('WP_CACHE', true);
define('WP_DEBUG_LOG', false);
define('ENFORCE_GZIP', true);
break;
}Feature Flags
// Feature flags for gradual rollouts
define('FEATURE_NEW_EDITOR', true);
define('FEATURE_ADVANCED_ANALYTICS', false);
define('FEATURE_SOCIAL_SHARING', true);
define('FEATURE_CUSTOM_DASHBOARD', false);
// Feature flag helper
function is_feature_enabled($feature) {
$constant = 'FEATURE_' . strtoupper($feature);
return defined($constant) ? constant($constant) : false;
}Configuration Management
Version Control for Configuration
# Track configuration changes
git init
git add wp-config.php
git commit -m "Initial WordPress configuration"
# Create configuration templates
cp wp-config.php wp-config.production.php
cp wp-config.php wp-config.staging.php
cp wp-config.php wp-config.development.phpConfiguration Validation
// Configuration validation
function validate_wordpress_config() {
$errors = array();
// Check required constants
$required_constants = array('DB_NAME', 'DB_USER', 'DB_PASSWORD', 'AUTH_KEY');
foreach ($required_constants as $constant) {
if (!defined($constant)) {
$errors[] = "Required constant {$constant} is not defined";
}
}
// Check file permissions
$wp_config_path = ABSPATH . 'wp-config.php';
if (!is_readable($wp_config_path)) {
$errors[] = 'wp-config.php is not readable';
}
// Check database connection
try {
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($db->connect_error) {
$errors[] = 'Database connection failed: ' . $db->connect_error;
}
$db->close();
} catch (Exception $e) {
$errors[] = 'Database connection error: ' . $e->getMessage();
}
return $errors;
}Troubleshooting Configuration Issues
Common Configuration Problems
Memory Issues
// Increase memory limits
define('WP_MEMORY_LIMIT', '512M');
define('WP_MAX_MEMORY_LIMIT', '1024M');
// Check current memory usage
function log_memory_usage() {
$memory_usage = memory_get_peak_usage(true);
$memory_limit = ini_get('memory_limit');
error_log("Memory Usage: " . round($memory_usage / 1024 / 1024, 2) . "MB / {$memory_limit}");
}
add_action('shutdown', 'log_memory_usage');Database Connection Issues
// Debug database connection
function debug_database_connection() {
global $wpdb;
if ($wpdb->error) {
error_log('Database Error: ' . $wpdb->error->get_error_message());
}
// Log slow queries
if ($wpdb->time_taken > 0.5) {
error_log('Slow Query: ' . $wpdb->last_query . ' (' . $wpdb->time_taken . 's)');
}
}
add_action('shutdown', 'debug_database_connection');Performance Issues
// Performance monitoring
function monitor_performance() {
$start_time = microtime(true);
// ... your code ...
$end_time = microtime(true);
$execution_time = $end_time - $start_time;
if ($execution_time > 1.0) { // Log slow executions
error_log('Slow execution: ' . round($execution_time, 3) . 's - ' . $_SERVER['REQUEST_URI']);
}
}
add_action('wp_loaded', 'monitor_performance');Best Practices
Configuration Best Practices
- Environment Separation: Different configs for dev/staging/production
- Version Control: Track configuration changes in git
- Documentation: Document all custom configurations
- Security: Never commit sensitive data to version control
- Validation: Always validate configuration changes
- Backup: Backup working configurations
- Monitoring: Monitor configuration performance
- Updates: Regularly review and update configurations
Security Best Practices
- File Permissions: Set correct file permissions
- Database Security: Use strong database credentials
- SSL/TLS: Always use HTTPS
- Firewall: Implement web application firewall
- Updates: Keep WordPress and plugins updated
- Monitoring: Monitor for security issues
- Backup: Regular configuration backups
Master advanced WordPress configuration for optimal performance and security.