'object',
'sanitize_callback' => 'envwise_sanitize_settings',
'default' => array(
'production_url' => 'https://www.yoursite.com/',
'disable_when_plugin_disabled' => true,
),
)
);
}
/**
* Sanitize settings.
*
* @param array $input Raw settings input.
* @return array Sanitized settings.
*/
function envwise_sanitize_settings( $input ) {
$sanitized = array();
if ( isset( $input['production_url'] ) ) {
$sanitized['production_url'] = esc_url_raw( trim( $input['production_url'] ) );
// Ensure URL has trailing slash for consistency.
if ( ! empty( $sanitized['production_url'] ) && substr( $sanitized['production_url'], -1 ) !== '/' ) {
$sanitized['production_url'] .= '/';
}
}
// Sanitize the disable when plugin disabled setting.
$sanitized['disable_when_plugin_disabled'] = isset( $input['disable_when_plugin_disabled'] ) ? (bool) $input['disable_when_plugin_disabled'] : false;
return $sanitized;
}
/**
* Get plugin settings
*/
function envwise_get_settings() {
$defaults = array(
'production_url' => 'https://www.yoursite.com/',
'disable_when_plugin_disabled' => true,
);
$settings = get_option( ENVWISE_OPTION, array() );
return wp_parse_args( $settings, $defaults );
}
/**
* Get production URL from settings
*/
function envwise_get_production_url() {
$settings = envwise_get_settings();
return $settings['production_url'];
}
/**
* Admin page content
*/
function envwise_admin_page() {
// Check user capabilities.
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$settings = envwise_get_settings();
$current_url = home_url( '/' );
$is_production = envwise_is_production_environment();
?>
Envwise automatically manages WordPress settings based on your environment (production vs non-production). This plugin serves as a framework that you can extend to implement custom environment-based controls for any WordPress setting.', 'envwise' ),
array( 'strong' => array() )
);
?>
-
Production Environment: When the current URL matches the production URL OR when
WP_ENV is set to \'production\', the site operates in production mode with search engine indexing allowed.', 'envwise' ),
array(
'strong' => array(),
'code' => array(),
)
);
?>
-
Non-Production Environment: When the current URL doesn\'t match the production URL OR when
WP_ENV is not \'production\', the "Discourage search engines from indexing this site" option is automatically enforced.', 'envwise' ),
array(
'strong' => array(),
'code' => array(),
)
);
?>
-
Plugin Disabled Behavior: When the plugin is deactivated, search engines are allowed by default to prevent accidental blocking of production sites. If you turn this option off, the setting for the current environment is saved instead (non-production stays discouraged).', 'envwise' ),
array( 'strong' => array() )
);
?>
-
WP_ENV constant is defined, it takes priority over URL matching', 'envwise' ),
array( 'code' => array() )
);
?>
-
WP_ENV is not defined, the plugin compares the current URL with the production URL', 'envwise' ),
array( 'code' => array() )
);
?>
-
Hook into environment detection: Use
envwise_is_production_environment() in your custom code', 'envwise' ),
array(
'strong' => array(),
'code' => array(),
)
);
?>
- Add custom settings: Extend the settings array and admin interface', 'envwise' ), array( 'strong' => array() ) ); ?>
- Control any WordPress option: Use filters similar to the search engine indexing implementation', 'envwise' ), array( 'strong' => array() ) ); ?>
- Environment-specific features: Enable/disable functionality based on environment', 'envwise' ), array( 'strong' => array() ) ); ?>
-
envwise_is_production_environment() - Check if current environment is production', 'envwise' ),
array( 'code' => array() )
);
?>
-
envwise_get_settings() - Get plugin settings', 'envwise' ),
array( 'code' => array() )
);
?>
-
envwise_get_production_url() - Get configured production URL', 'envwise' ),
array( 'code' => array() )
);
?>
' . __( 'Settings', 'envwise' ) . '';
array_unshift( $links, $settings_link );
return $links;
}
/**
* Check if current environment is production
*
* @return boolean True if production, false otherwise
*/
function envwise_is_production_environment() {
// Check WP_ENV if defined.
if ( defined( 'WP_ENV' ) ) {
return WP_ENV === 'production';
} else {
// Check HOME URL against production URL from settings.
$production_url = envwise_get_production_url();
$current_url = home_url( '/' );
return $current_url === $production_url;
}
}
/**
* Filter the value of the 'blog_public' option based on environment check.
*/
add_filter(
'pre_option_blog_public',
function () {
if ( ! envwise_is_production_environment() ) {
return 0; // Force discourage search engines.
} else {
return 1; // Allow search engines in production.
}
}
);
/**
* Display a notice in the admin area if we're not in production.
* This notice self-dismisses when the environment becomes production.
*/
add_action(
'admin_notices',
function () {
// Only show on plugin settings page and dashboard to avoid overwhelming users.
$current_screen = get_current_screen();
if ( ! $current_screen || ! in_array( $current_screen->id, array( 'dashboard', 'tools_page_envwise' ), true ) ) {
return;
}
if ( ! envwise_is_production_environment() ) {
$message = '' . __( 'Envwise Notice:', 'envwise' ) . ' ';
if ( defined( 'WP_ENV' ) && WP_ENV !== 'production' ) {
/* translators: %s: The current WP_ENV value (e.g., development, staging) */
$message .= sprintf( __( 'WP_ENV is set to %s.', 'envwise' ), '' . esc_html( WP_ENV ) . '' );
} else {
$message .= __( 'Current URL does not match production URL.', 'envwise' );
}
$message .= ' ' . __( 'Search engine indexing is automatically disabled.', 'envwise' );
$message .= ' ' . __( 'Configure Envwise', 'envwise' ) . '';
echo '' . wp_kses_post( $message ) . '
';
}
}
);
/**
* Plugin activation hook
*/
register_activation_hook( __FILE__, 'envwise_activate' );
/**
* Runs on plugin activation to apply the current environment logic immediately.
*/
function envwise_activate() {
// Migrate settings saved by versions released under the previous name.
$legacy_settings = get_option( 'env_control_settings' );
if ( false !== $legacy_settings && false === get_option( ENVWISE_OPTION ) ) {
update_option( ENVWISE_OPTION, $legacy_settings );
}
delete_option( 'env_control_settings' );
// When plugin is activated, apply the current environment logic.
if ( envwise_is_production_environment() ) {
update_option( 'blog_public', '1' ); // Allow search engines.
} else {
update_option( 'blog_public', '0' ); // Discourage search engines.
}
}
/**
* Plugin deactivation hook.
*/
register_deactivation_hook( __FILE__, 'envwise_deactivate' );
/**
* Runs on plugin deactivation.
*
* When the "disable_when_plugin_disabled" setting is enabled, search engines are
* always allowed. Otherwise blog_public is synced to the current environment,
* because the pre_option filter never writes to the database and the stored value
* may be stale (e.g. a production database cloned to staging).
*/
function envwise_deactivate() {
$settings = envwise_get_settings();
if ( $settings['disable_when_plugin_disabled'] || envwise_is_production_environment() ) {
update_option( 'blog_public', '1' ); // Allow search engines.
} else {
update_option( 'blog_public', '0' ); // Keep non-production discouraged.
}
}