Skip to main content

Ask whether this install is staging

Free Intermediate

There are three different questions here and add-ons reliably reach for the wrong one.

StagingManager::is_staging() asks “is this a non-production environment?” and is the answer core applies its restrictions from — the orange admin bar, the live-Stripe-key refusal, the staging subscriber allowlist. InstallIdentity::is_production() asks “am I still the install I armed myself as?” — clone detection, hostname-blind. EnvironmentResolver asks only “what has the operator declared in wp-config.php?” and is the single place either environment constant is read.

Reach for is_staging() for almost everything. Reach for the resolver when you specifically need to know whether a human declared the environment, as opposed to the plugin having guessed it from the hostname.

Code

<?php
use Benecaster\Staging\EnvironmentDeclaration;
use Benecaster\Staging\EnvironmentResolver;
use Benecaster\Staging\StagingManager;

add_action( 'benecaster_boot', function ( \Benecaster\Container $container ): void {
    // The ordinary question: should my add-on hold back from doing
    // anything that touches real customers on this install?
    if ( $container->make( StagingManager::class )->is_staging() ) {
        add_filter( 'my_addon_send_real_emails', '__return_false' );
    }

    // The narrower question: was that DECLARED, or merely detected?
    // Useful when you want to warn about a guess but trust a decision.
    $declared = $container->make( EnvironmentResolver::class )->declared();

    if ( EnvironmentDeclaration::Undeclared === $declared ) {
        // Nobody said. Core fell through to hostname detection, which
        // can be wrong in both directions — surface it rather than
        // silently acting on it.
        my_addon_log( 'Environment was detected, not declared.' );
    }
} );

View on GitHub →

Need this built rather than just documented? See our services →