• Resolved erniecom

    (@erniecom)


    I wanted to add a more descriptive environment type to support for instance Lando dev environment (on top of Docker). With the help of ChatGPT I found the following code to replace display-environment-type/app/Plugin.php method get_env_type_name:

    public static function get_env_type_name( $env_type ) {
    switch ( $env_type ) {
    case 'local':
    $name = __( 'Local', 'display-environment-type' );
    break;
    case 'development':
    $name = __( 'Development', 'display-environment-type' );
    break;
    case 'staging':
    $name = __( 'Staging', 'display-environment-type' );
    break;
    case 'production':
    $name = __( 'Production', 'display-environment-type' );
    break;
    default:
    $name = __( 'Production', 'display-environment-type' );
    }

    /**
    * Filter the environment type name.
    *
    * @param string $name The translated environment name.
    * @param string $env_type The environment type key.
    */
    return apply_filters( 'display_environment_type_name', $name, $env_type );
    }

    User code to in theme or plugin to add custom environment types:


    add_filter( 'display_environment_type_name', function ( $name, $env_type ) {
    // Map custom environment types to user-friendly names.
    $custom_env_types = [
    'lando' => __( 'Lando', 'display-environment-type' ),
    'custom' => __( 'Custom Environment', 'display-environment-type' ),
    ];

    // Return custom name if it exists, otherwise fallback to the original name.
    return isset( $custom_env_types[ $env_type ] ) ? $custom_env_types[ $env_type ] : $name;
    }, 10, 2 );

    Would you please consider adding this small change in your code for our convenience? Thank you!

    • This topic was modified 3 weeks, 5 days ago by erniecom.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Roy Tanck

    (@roytanck)

    Thank you for your suggestion. Support for custom environment types was explicitly removed in WP 5.5.1: https://make.wordpress.org/core/2020/08/27/wordpress-environment-types/ . It appears that your code relies on the $env_type parameter being set to something other than the four officially allowed values? That could be problematic, considering that WP (according to the blog post) resets any other values to production.

    Thread Starter erniecom

    (@erniecom)

    You are right. I tested it and only the agreed environment types are passed on by core. Still the same, the filter allows a user to change the Notification text that is seen in the admin menu. I changed the filter codo to test for an environment variable that only Lando sets in its http server and now it displays Lando in stead of Local:

    add_filter( 'display_environment_type_name', function ( $name, $env_type ) {
    // Map custom environment types to user-friendly names.
    $custom_env_types = [];
    if ($_ENV['LANDO']=='ON'){
    $custom_env_types['local'] = __( 'Lando', 'display-environment-type' );
    }

    // Return custom name if it exists, otherwise fallback to the original name.
    return isset( $custom_env_types[ $env_type ] ) ? $custom_env_types[ $env_type ] : $name;
    }, 10, 2 );

    I also added filter code that shows the environment type in wp-login.php, which was what I was originally looking for. It would be another valuable addition to your already appreciated plug-in. When I see a wrong notice there I avoid both that of logging in and the risk of forgetting to look at the top line notice. Two notices is better than one for some.

    Plugin Author Roy Tanck

    (@roytanck)

    I think I agree that adding the hook would increase the plugin’s flexibility. I’ll add it in the next release.

    Thanks for suggesting a notice on the login screen. I’ll definitely look into that.

    Thread Starter erniecom

    (@erniecom)

    I can share the code here as far as I got it:

    add_filter( 'login_message', function ( $message ) {
    // Get the environment type using the WP_ENVIRONMENT_TYPE constant.
    $environment_type = defined( 'WP_ENVIRONMENT_TYPE' ) ? WP_ENVIRONMENT_TYPE : 'unknown';

    // Add a styled message to the login page.
    $environment_message = sprintf(
    '<p style="background: #f5f5f5; border-left: 4px solid %s; padding: 10px; margin-bottom: 20px; font-size: 14px;">
    <strong>Environment:</strong> %s
    </p>',
    // Choose a color based on the environment type.
    $environment_type === 'production' ? '#d9534f' : ($environment_type === 'staging' ? '#f0ad4e' : '#5bc0de'),
    ucfirst( $environment_type )
    );

    // Append the environment message to the existing login message.
    return $environment_message . $message;
    });

    It directly reads the constant WP_ENVIRONMENT_TYPE but it might read as well take it from wp_get_environment_type() or better still, from your code so the new hook will work for both.

    • This reply was modified 3 weeks, 4 days ago by erniecom.
    Plugin Author Roy Tanck

    (@roytanck)

    I’ve just released version 1.3.4 which adds the hook (and is properly tested with WP 1.3.4). Thanks for the suggestion!

    As for the login screen, I’m wondering whether that should be optional (which would require adding settings to the plugin), and I want to investigate different ways that could be displayed. Definitely something I’ll consider for a future version.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.