<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>wordpress environments Archives - WP-CLI Mastery</title>
	<atom:link href="https://wpclimastery.com/blog/tag/wordpress-environments/feed/" rel="self" type="application/rss+xml" />
	<link>https://wpclimastery.com/blog/tag/wordpress-environments/</link>
	<description>Automate WordPress Like a DevOps Pro.</description>
	<lastBuildDate>Mon, 24 Nov 2025 11:16:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://wpclimastery.com/wp-content/uploads/2025/11/cropped-favicon-32x32.webp</url>
	<title>wordpress environments Archives - WP-CLI Mastery</title>
	<link>https://wpclimastery.com/blog/tag/wordpress-environments/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Multi-Environment WordPress Management with WP-CLI (Dev/Staging/Prod)</title>
		<link>https://wpclimastery.com/blog/multi-environment-wordpress-management-with-wp-cli-dev-staging-prod/</link>
					<comments>https://wpclimastery.com/blog/multi-environment-wordpress-management-with-wp-cli-dev-staging-prod/#respond</comments>
		
		<dc:creator><![CDATA[Krasen]]></dc:creator>
		<pubDate>Mon, 24 Nov 2025 11:16:24 +0000</pubDate>
				<category><![CDATA[WordPress DevOps]]></category>
		<category><![CDATA[dev staging production]]></category>
		<category><![CDATA[environment synchronization]]></category>
		<category><![CDATA[wordpress environments]]></category>
		<category><![CDATA[wordpress workflow]]></category>
		<category><![CDATA[wp-cli environment management]]></category>
		<guid isPermaLink="false">https://wpclimastery.com/?p=222</guid>

					<description><![CDATA[<p>Professional WordPress development requires managing multiple environments to test changes safely before deploying to production. WP-CLI streamlines environment management, enabling efficient synchronization, configuration control, and deployment workflows that minimize downtime...</p>
<p>The post <a href="https://wpclimastery.com/blog/multi-environment-wordpress-management-with-wp-cli-dev-staging-prod/">Multi-Environment WordPress Management with WP-CLI (Dev/Staging/Prod)</a> appeared first on <a href="https://wpclimastery.com">WP-CLI Mastery</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Professional WordPress development requires managing multiple environments to test changes safely before deploying to production. WP-CLI streamlines environment management, enabling efficient synchronization, configuration control, and deployment workflows that minimize downtime and reduce errors.</p>



<h2 class="wp-block-heading" id="understanding-wordpress-environment-architecture">Understanding WordPress Environment Architecture</h2>



<p>A robust WordPress workflow typically includes three distinct environments: development for building new features, staging for testing changes in production-like conditions, and production where live users interact with your site.</p>



<p>Each environment serves a specific purpose. Development allows rapid iteration without affecting users, staging validates changes before release, and production serves actual traffic. Proper environment management ensures code flows smoothly through this pipeline while maintaining data integrity and security.</p>



<h2 class="wp-block-heading" id="setting-up-environment-specific-configurations">Setting Up Environment-Specific Configurations</h2>



<p>WordPress 5.5 introduced native environment type support through the&nbsp;<code>WP_ENVIRONMENT_TYPE</code>&nbsp;constant. WP-CLI leverages this for environment-aware operations.</p>



<pre class="wp-block-code"><code><em># Set environment type in wp-config.php</em>
wp config set WP_ENVIRONMENT_TYPE 'development' --raw

<em># Verify current environment</em>
wp config get WP_ENVIRONMENT_TYPE

<em># Different values: local, development, staging, production</em>
</code></pre>



<p>Create environment-specific configuration files:</p>



<pre class="wp-block-code"><code><em># wp-config-local.php for development</em>
cat &gt; wp-config-local.php &lt;&lt; 'EOF'
&lt;?php
// Development settings
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);
define('SCRIPT_DEBUG', true);
define('SAVEQUERIES', true);

// Disable caching in development
define('WP_CACHE', false);

// Development database
define('DB_NAME', 'wp_dev');
define('DB_USER', 'dev_user');
define('DB_PASSWORD', 'dev_password');
define('DB_HOST', 'localhost');
EOF

<em># wp-config-staging.php</em>
cat &gt; wp-config-staging.php &lt;&lt; 'EOF'
&lt;?php
// Staging settings
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

// Prevent search engine indexing
define('DISCOURAGE_SEARCH', true);

// Staging database
define('DB_NAME', 'wp_staging');
define('DB_USER', 'staging_user');
define('DB_PASSWORD', 'staging_password');
define('DB_HOST', 'localhost');
EOF

<em># wp-config-production.php</em>
cat &gt; wp-config-production.php &lt;&lt; 'EOF'
&lt;?php
// Production settings
define('WP_DEBUG', false);
define('WP_DEBUG_LOG', false);
define('WP_DEBUG_DISPLAY', false);

// Security enhancements
define('DISALLOW_FILE_EDIT', true);
define('DISALLOW_FILE_MODS', false);
define('FORCE_SSL_ADMIN', true);

// Production database
define('DB_NAME', 'wp_production');
define('DB_USER', 'prod_user');
define('DB_PASSWORD', 'secure_production_password');
define('DB_HOST', 'production-db.example.com');
EOF
</code></pre>



<h2 class="wp-block-heading" id="database-synchronization-strategies">Database Synchronization Strategies</h2>



<p>Syncing databases between environments requires careful handling of environment-specific data like URLs, file paths, and credentials.</p>



<pre class="wp-block-code"><code>#!/bin/bash
<em># sync-db-staging-to-dev.sh - Sync staging database to development</em>

set -euo pipefail

STAGING_PATH="/var/www/staging"
DEV_PATH="/var/www/development"
BACKUP_DIR="/backups"
TIMESTAMP=$(date +%Y%m%d-%H%M%S)

echo "Starting database sync from staging to development"

<em># Backup development database before sync</em>
echo "Backing up development database..."
wp --path="$DEV_PATH" db export "$BACKUP_DIR/dev-pre-sync-$TIMESTAMP.sql"

<em># Export staging database</em>
echo "Exporting staging database..."
wp --path="$STAGING_PATH" db export "$BACKUP_DIR/staging-export-$TIMESTAMP.sql"

<em># Get staging URL</em>
STAGING_URL=$(wp --path="$STAGING_PATH" option get siteurl)

<em># Get development URL</em>
DEV_URL=$(wp --path="$DEV_PATH" option get siteurl)

<em># Import to development</em>
echo "Importing to development..."
wp --path="$DEV_PATH" db import "$BACKUP_DIR/staging-export-$TIMESTAMP.sql"

<em># Update URLs</em>
echo "Updating URLs from $STAGING_URL to $DEV_URL"
wp --path="$DEV_PATH" search-replace "$STAGING_URL" "$DEV_URL" --all-tables

<em># Update file paths if different</em>
STAGING_PATH_ABS=$(wp --path="$STAGING_PATH" eval 'echo ABSPATH;')
DEV_PATH_ABS=$(wp --path="$DEV_PATH" eval 'echo ABSPATH;')

if &#91; "$STAGING_PATH_ABS" != "$DEV_PATH_ABS" ]; then
    echo "Updating file paths..."
    wp --path="$DEV_PATH" search-replace "$STAGING_PATH_ABS" "$DEV_PATH_ABS" --all-tables
fi

<em># Flush cache</em>
wp --path="$DEV_PATH" cache flush

<em># Update permalinks</em>
wp --path="$DEV_PATH" rewrite flush

echo "Database sync completed successfully"
</code></pre>



<h2 class="wp-block-heading" id="selective-data-synchronization">Selective Data Synchronization</h2>



<p>Production databases often contain sensitive user data that shouldn&#8217;t be copied to development environments. Implement selective synchronization:</p>



<pre class="wp-block-code"><code>#!/bin/bash
<em># sync-selective.sh - Sync database with data sanitization</em>

WP_PATH="/var/www/development"
TEMP_SQL="/tmp/staging-sanitized.sql"

<em># Export from staging (on staging server)</em>
ssh staging@staging.example.com "wp --path=/var/www/html db export - " &gt; "$TEMP_SQL"

<em># Import to development</em>
wp --path="$WP_PATH" db import "$TEMP_SQL"

<em># Sanitize sensitive data in development</em>
echo "Sanitizing user data..."

<em># Anonymize user emails (except admin)</em>
wp --path="$WP_PATH" db query "
UPDATE wp_users
SET user_email = CONCAT('user', ID, '@example.local')
WHERE ID != 1
"

<em># Clear user passwords</em>
wp --path="$WP_PATH" db query "
UPDATE wp_users
SET user_pass = MD5(CONCAT('dev_password_', ID))
"

<em># Remove personal data from comments</em>
wp --path="$WP_PATH" db query "
UPDATE wp_comments
SET comment_author_email = CONCAT('commenter', comment_ID, '@example.local'),
    comment_author_IP = '127.0.0.1'
"

<em># Clear transients and cached data</em>
wp --path="$WP_PATH" transient delete --all

<em># Update site URLs</em>
wp --path="$WP_PATH" search-replace 'https://staging.example.com' 'http://dev.local' --all-tables

echo "Selective sync completed with data sanitization"
rm "$TEMP_SQL"
</code></pre>



<h2 class="wp-block-heading" id="file-synchronization">File Synchronization</h2>



<p>Sync uploads and plugin/theme files between environments while excluding unnecessary files.</p>



<pre class="wp-block-code"><code>#!/bin/bash
<em># sync-files.sh - Sync WordPress files between environments</em>

SOURCE_PATH="/var/www/staging"
DEST_PATH="/var/www/development"

<em># Sync uploads directory</em>
echo "Syncing uploads..."
rsync -avz --delete \
    --exclude='cache/' \
    --exclude='backup/' \
    "$SOURCE_PATH/wp-content/uploads/" \
    "$DEST_PATH/wp-content/uploads/"

<em># Sync plugins (optional - usually managed by version control)</em>
echo "Syncing plugins..."
rsync -avz --delete \
    --exclude='*/cache' \
    --exclude='*.log' \
    "$SOURCE_PATH/wp-content/plugins/" \
    "$DEST_PATH/wp-content/plugins/"

<em># Sync themes</em>
echo "Syncing themes..."
rsync -avz --delete \
    "$SOURCE_PATH/wp-content/themes/" \
    "$DEST_PATH/wp-content/themes/"

<em># Set correct permissions</em>
chown -R www-data:www-data "$DEST_PATH/wp-content"
find "$DEST_PATH/wp-content" -type d -exec chmod 755 {} \;
find "$DEST_PATH/wp-content" -type f -exec chmod 644 {} \;

echo "File sync completed"
</code></pre>



<h2 class="wp-block-heading" id="environment-aware-deployment-scripts">Environment-Aware Deployment Scripts</h2>



<p>Create intelligent deployment scripts that adapt based on target environment.</p>



<pre class="wp-block-code"><code>#!/bin/bash
<em># deploy.sh - Environment-aware deployment script</em>

set -euo pipefail

<em># Configuration</em>
ENVIRONMENTS=("development" "staging" "production")
TARGET_ENV="$1"

if &#91;&#91; ! " ${ENVIRONMENTS&#91;@]} " =~ " ${TARGET_ENV} " ]]; then
    echo "Error: Invalid environment. Use: development, staging, or production"
    exit 1
fi

<em># Load environment-specific configuration</em>
case $TARGET_ENV in
    development)
        WP_PATH="/var/www/development"
        BACKUP_RETENTION=7
        UPDATE_CORE=true
        UPDATE_PLUGINS=true
        ;;
    staging)
        WP_PATH="/var/www/staging"
        BACKUP_RETENTION=14
        UPDATE_CORE=true
        UPDATE_PLUGINS=true
        ;;
    production)
        WP_PATH="/var/www/html"
        BACKUP_RETENTION=30
        UPDATE_CORE=false  <em># Manual approval required</em>
        UPDATE_PLUGINS=false
        ;;
esac

echo "Deploying to: $TARGET_ENV"

<em># Pre-deployment checks</em>
echo "Running pre-deployment checks..."

<em># Verify WordPress is installed</em>
if ! wp --path="$WP_PATH" core is-installed; then
    echo "Error: WordPress not properly installed at $WP_PATH"
    exit 1
fi

<em># Check database connection</em>
if ! wp --path="$WP_PATH" db check; then
    echo "Error: Database connection failed"
    exit 1
fi

<em># Create backup</em>
TIMESTAMP=$(date +%Y%m%d-%H%M%S)
BACKUP_DIR="/backups/$TARGET_ENV"
mkdir -p "$BACKUP_DIR"

echo "Creating backup..."
wp --path="$WP_PATH" db export "$BACKUP_DIR/pre-deploy-$TIMESTAMP.sql"

<em># Clean old backups</em>
find "$BACKUP_DIR" -name "pre-deploy-*.sql" -mtime +$BACKUP_RETENTION -delete

<em># Maintenance mode</em>
if &#91;&#91; "$TARGET_ENV" == "production" ]]; then
    echo "Enabling maintenance mode..."
    wp --path="$WP_PATH" maintenance-mode activate
fi

<em># Deploy code (assuming Git deployment)</em>
echo "Pulling latest code..."
cd "$WP_PATH"
git pull origin "$(git rev-parse --abbrev-ref HEAD)"

<em># Install/update dependencies</em>
if &#91; -f "composer.json" ]; then
    composer install --no-dev --optimize-autoloader
fi

<em># Run database migrations if needed</em>
if wp --path="$WP_PATH" plugin is-installed wp-migrate-db; then
    wp --path="$WP_PATH" migratedb migrate
fi

<em># Update plugins and core if allowed</em>
if &#91;&#91; "$UPDATE_PLUGINS" == true ]]; then
    echo "Updating plugins..."
    wp --path="$WP_PATH" plugin update --all --exclude=custom-plugin
fi

if &#91;&#91; "$UPDATE_CORE" == true ]]; then
    echo "Updating WordPress core..."
    wp --path="$WP_PATH" core update --minor
fi

<em># Clear caches</em>
echo "Clearing caches..."
wp --path="$WP_PATH" cache flush
wp --path="$WP_PATH" transient delete --expired

if wp --path="$WP_PATH" plugin is-installed wp-super-cache --status=active; then
    wp --path="$WP_PATH" cache flush
fi

<em># Regenerate assets if needed</em>
if &#91; -f "package.json" ]; then
    npm run build
fi

<em># Update permalinks</em>
wp --path="$WP_PATH" rewrite flush

<em># Deactivate maintenance mode</em>
if &#91;&#91; "$TARGET_ENV" == "production" ]]; then
    echo "Disabling maintenance mode..."
    wp --path="$WP_PATH" maintenance-mode deactivate
fi

<em># Post-deployment verification</em>
echo "Running post-deployment verification..."

<em># Check site is accessible</em>
SITE_URL=$(wp --path="$WP_PATH" option get siteurl)
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$SITE_URL")

if &#91; "$HTTP_CODE" != "200" ]; then
    echo "Warning: Site returned HTTP $HTTP_CODE"

    if &#91;&#91; "$TARGET_ENV" == "production" ]]; then
        echo "Rolling back..."
        wp --path="$WP_PATH" db import "$BACKUP_DIR/pre-deploy-$TIMESTAMP.sql"
        exit 1
    fi
fi

<em># Verify core integrity</em>
if ! wp --path="$WP_PATH" core verify-checksums; then
    echo "Warning: Core checksum verification failed"
fi

echo "Deployment to $TARGET_ENV completed successfully"

<em># Send notification (production only)</em>
if &#91;&#91; "$TARGET_ENV" == "production" ]]; then
    echo "Deployment completed at $(date)" | mail -s "Production Deployment Success" admin@example.com
fi
</code></pre>



<h2 class="wp-block-heading" id="configuration-management">Configuration Management</h2>



<p>Manage environment-specific settings without hardcoding sensitive values.</p>



<pre class="wp-block-code"><code>#!/bin/bash
<em># config-manager.sh - Environment configuration management</em>

WP_PATH="/var/www/html"
ENV_FILE=".env"

<em># Load environment variables from .env file</em>
if &#91; -f "$ENV_FILE" ]; then
    export $(cat "$ENV_FILE" | grep -v '^#' | xargs)
fi

<em># Set configuration based on environment</em>
set_config() {
    local key="$1"
    local value="$2"
    local type="${3:-constant}"

    if &#91;&#91; "$type" == "constant" ]]; then
        wp --path="$WP_PATH" config set "$key" "$value" --raw
    else
        wp --path="$WP_PATH" option update "$key" "$value"
    fi
}

<em># Get current environment</em>
CURRENT_ENV=$(wp --path="$WP_PATH" config get WP_ENVIRONMENT_TYPE 2&gt;/dev/null || echo "production")

echo "Configuring for environment: $CURRENT_ENV"

<em># Apply environment-specific settings</em>
case $CURRENT_ENV in
    development|local)
        set_config WP_DEBUG true constant
        set_config WP_DEBUG_LOG true constant
        set_config WP_DEBUG_DISPLAY true constant
        set_config SCRIPT_DEBUG true constant
        set_config SAVEQUERIES true constant

        <em># Development plugins</em>
        wp --path="$WP_PATH" plugin activate query-monitor
        wp --path="$WP_PATH" plugin activate debug-bar
        ;;

    staging)
        set_config WP_DEBUG true constant
        set_config WP_DEBUG_LOG true constant
        set_config WP_DEBUG_DISPLAY false constant
        set_config SCRIPT_DEBUG false constant

        <em># Disable search indexing</em>
        set_config blog_public 0 option

        <em># Staging-specific plugins</em>
        wp --path="$WP_PATH" plugin deactivate google-analytics
        ;;

    production)
        set_config WP_DEBUG false constant
        set_config WP_DEBUG_LOG false constant
        set_config WP_DEBUG_DISPLAY false constant
        set_config SCRIPT_DEBUG false constant

        <em># Security settings</em>
        set_config DISALLOW_FILE_EDIT true constant
        set_config FORCE_SSL_ADMIN true constant

        <em># Enable search indexing</em>
        set_config blog_public 1 option

        <em># Deactivate development plugins</em>
        wp --path="$WP_PATH" plugin deactivate query-monitor --quiet 2&gt;/dev/null || true
        wp --path="$WP_PATH" plugin deactivate debug-bar --quiet 2&gt;/dev/null || true
        ;;
esac

echo "Configuration applied for $CURRENT_ENV environment"
</code></pre>



<h2 class="wp-block-heading" id="automated-environment-testing">Automated Environment Testing</h2>



<p>Verify environment configuration and functionality before accepting deployments.</p>



<pre class="wp-block-code"><code>#!/bin/bash
<em># test-environment.sh - Automated environment testing</em>

WP_PATH="/var/www/staging"

echo "=== Environment Testing ==="

<em># Test WordPress installation</em>
echo "Testing WordPress installation..."
if wp --path="$WP_PATH" core is-installed; then
    echo "✓ WordPress is installed"
else
    echo "✗ WordPress installation check failed"
    exit 1
fi

<em># Test database connection</em>
echo "Testing database connection..."
if wp --path="$WP_PATH" db check; then
    echo "✓ Database connection successful"
else
    echo "✗ Database connection failed"
    exit 1
fi

<em># Verify core files</em>
echo "Verifying core files..."
if wp --path="$WP_PATH" core verify-checksums; then
    echo "✓ Core files verified"
else
    echo "⚠ Core checksum verification failed"
fi

<em># Check for required plugins</em>
REQUIRED_PLUGINS=("wordpress-seo" "wordfence")
echo "Checking required plugins..."
for plugin in "${REQUIRED_PLUGINS&#91;@]}"; do
    if wp --path="$WP_PATH" plugin is-installed "$plugin"; then
        STATUS=$(wp --path="$WP_PATH" plugin status "$plugin" 2&gt;&amp;1 | grep -o "Active" || echo "Inactive")
        echo "✓ $plugin: $STATUS"
    else
        echo "✗ $plugin: Not installed"
    fi
done

<em># Test URL accessibility</em>
echo "Testing site accessibility..."
SITE_URL=$(wp --path="$WP_PATH" option get siteurl)
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" "$SITE_URL")

if &#91; "$HTTP_CODE" == "200" ]; then
    echo "✓ Site accessible (HTTP $HTTP_CODE)"
else
    echo "✗ Site returned HTTP $HTTP_CODE"
    exit 1
fi

<em># Check permalink structure</em>
echo "Testing permalinks..."
wp --path="$WP_PATH" rewrite flush
if wp --path="$WP_PATH" rewrite list &gt; /dev/null 2&gt;&amp;1; then
    echo "✓ Permalinks configured"
else
    echo "✗ Permalink configuration issue"
fi

<em># Performance checks</em>
echo "Running performance checks..."
DB_SIZE=$(wp --path="$WP_PATH" db size --human-readable --format=json | jq -r '.Size')
echo "Database size: $DB_SIZE"

AUTOLOAD=$(wp --path="$WP_PATH" db query "SELECT CONCAT(ROUND(SUM(LENGTH(option_value))/1024/1024,2),'MB') as size FROM wp_options WHERE autoload='yes'" --skip-column-names)
echo "Autoload size: $AUTOLOAD"

echo "=== Environment Testing Complete ==="
</code></pre>



<h2 class="wp-block-heading" id="related-links">Related Links</h2>



<ul class="wp-block-list">
<li><a href="https://developer.wordpress.org/cli/commands/">WP-CLI Environment Commands</a></li>



<li><a href="https://developer.wordpress.org/themes/advanced-topics/wordpress-development-best-practices/">WordPress Development Best Practices</a></li>



<li><a href="https://developer.wordpress.org/advanced-administration/">Version Control for WordPress</a></li>



<li><a href="https://developer.wordpress.org/cli/commands/db/">Database Management with WP-CLI</a></li>



<li><a href="https://www.deployhq.com/wordpress">Continuous Deployment Strategies</a></li>
</ul>
<p>The post <a href="https://wpclimastery.com/blog/multi-environment-wordpress-management-with-wp-cli-dev-staging-prod/">Multi-Environment WordPress Management with WP-CLI (Dev/Staging/Prod)</a> appeared first on <a href="https://wpclimastery.com">WP-CLI Mastery</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://wpclimastery.com/blog/multi-environment-wordpress-management-with-wp-cli-dev-staging-prod/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
