How to Set Up a Staging Site for WordPress Development

Lukas K. Dec 29, 2024 Staging Site Setup
How can I create a test version of my website so I can try out changes without affecting the live site?
What are the best practices for setting up a staging environment for WordPress development to safely test updates and new features?
Andy answered Dec 29, 2024

Different Methods to Create a Staging Site

1. Using Managed WordPress Hosting

Many hosting providers offer one-click staging site creation:

  • WP Engine
  • Kinsta
  • SiteGround
  • Flywheel

Benefits:

  • Automated process
  • Built-in sync tools
  • Secure environment
  • Easy deployment

2. Manual Setup Method

Steps for Local Development:

  1. Install local development software:

    • LocalWP (formerly Local by Flywheel)
    • XAMPP
    • MAMP
  2. Export live site database

  3. Copy all files

  4. Update configuration

Here's how to update wp-config.php for staging:

// Prevent staging site from sending emails
define('WP_MAIL_SMTP_MAIL_FROM', 'no-reply@staging-domain.com');

// Disable search engine indexing
define('DISALLOW_INDEXING', true);

// Set staging environment
define('WP_ENVIRONMENT_TYPE', 'staging');

3. Using Plugins

Recommended plugins:

  1. WP Staging
  • Free and Pro versions
  • Creates staging copy within your existing WordPress
  • Plugin Website
  1. Duplicator
  • Creates complete site packages
  • Perfect for migration and staging
  • Plugin Website

Best Practices

  1. Security Measures
  • Password protect staging site
  • Block search engines
  • Use robots.txt

Example robots.txt for staging:

User-agent: *
Disallow: /
  1. Database Handling
  • Regular backups
  • Clean up unused data
  • Update site URLs

URL update SQL query:

UPDATE wp_options SET option_value = replace(option_value, 'https://www.production.com', 'https://staging.production.com') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'https://www.production.com', 'https://staging.production.com');
UPDATE wp_posts SET post_content = replace(post_content, 'https://www.production.com', 'https://staging.production.com');
  1. Version Control
  • Use Git for code management
  • Create separate branches for features
  • Document changes

Common Pitfalls to Avoid

  1. Email Configuration
  • Disable email notifications
  • Use staging email addresses
  • Prevent customer notifications
  1. Payment Gateways
  • Use test/sandbox modes
  • Disable live payment processing
  • Clear test orders regularly
  1. Cache Management
  • Clear cache after migration
  • Update cache configurations
  • Disable unnecessary caching

Deployment Process

  1. Test thoroughly on staging
  2. Document all changes
  3. Create backup of live site
  4. Deploy during low-traffic hours
  5. Verify functionality
  6. Monitor after deployment

Additional Tips

  1. Use different favicon/colors for staging
  2. Add staging environment indicator
  3. Implement version control
  4. Regular sync with production
  5. Clean up staging regularly

This code adds a staging environment indicator:

function add_staging_indicator() {
    if (WP_ENVIRONMENT_TYPE === 'staging') {
        echo '<div style="background: #ff6b6b; color: white; text-align: center; padding: 10px;">STAGING ENVIRONMENT</div>';
    }
}
add_action('wp_body_open', 'add_staging_indicator');

Remember to keep your staging environment as close as possible to production while maintaining security and preventing any accidental interactions with live systems.