Local Community

Environment Variables (12 factors)

Hello,

I try to set some environment variables in my different environments (local, staging and production).
Except this post I found nothing related to this subject.
I tried using wp_get_environment_type() in wp-config.php :

if ('local' === wp_get_environment_type()) {
    define('MY_ENV_VARIABLE', 'xxxxxxxx');
} else if ('staging' === wp_get_environment_type()) {
    define('MY_ENV_VARIABLE', 'yyyyyyyy');
} else if ('production' === wp_get_environment_type()) {
    define('MY_ENV_VARIABLE', 'zzzzzzzz');
}

Is there another way to do that? Especially for setting some plugin key that depends on environment (like WPML, …).

A way to set some variables by environment in https://app.getflywheel.com/ would be a real game changer.

Maybe there is another way to set variables using bedrock integration into flywheel but I don’t like to set env variables in my code.

Thanks for help

There’s probably a few ways to go about doing this.

I think if you are adding code to the wp-config.php file, then you probably don’t need to those conditionals since the wp-config.php file doesn’t usually “travel” with the site. By that I mean, that something like Local Connect doesn’t deploy the wp-config.php file when pushing a site up to the server.

Because of this, you can just add this to your Local site’s config file:

define('MY_ENV_VARIABLE', 'xxxxxxxx');

this to your staging site’s file:

define('MY_ENV_VARIABLE', 'yyyyyyyy');

and so on.

But if you are wanting to have this code “travel” with the site, then you’ll likely want to put that code in a Must Use plugin

Again, you can either reach out to Flywheel support to add the specific environmental vars to the remote site’s wp-config.php file, or if you go the “Must use plugin” route above, then Flywheel should be setting the value for staging and production for you.

I’m not sure, but that video doesn’t seem to cover how they deploy the site once it’s ready to go live.

As it is today, there really isn’t any Bedrock integration with Flywheel (or WP Engine) – both of these managed WP hosts have custom configurations that are very different than what Bedrock recommends. Because of this, many of the reasons to choose a Bedrock installation don’t make sense if your remote environments are hosted at WP Engine or Flywheel.

I used to use Bedrock and the larger family of Roots.io things, but now, I pretty much only use their Sage theme. A big reason for this is that I no maintain my own DigitalOcean boxes using Trellis. For me, the cost savings of a $5/mo DO box wasn’t enough of an incentive to just pay more for a Manage WordPress hosting provider.

That being said, if others in the community are using some sort of custom CI to deploy to FW or WPE, I’d love to hear more!

Hope that helps untangle and clarify things! Let us know if you have any other questions!

Thanks for clarifications, updating wp-config.php do the job.

It would be really handy if one could set those env variables using some export command or better with an .env file (deployed manually or automagically). Unfortunately, exported variables are attached to ssh session (not persisted), and I guess there is no mechanism so that Local load en .env file.

BTW, I use Bedrock only as a boilerplate so that I can import plugins and theme using composer. I do not use Trellis, juste Flywheel with some custom nginx config.

If that’s the case then maybe you don’t need Bedrock? I don’t know if this fits your needs, but you might try using composer’s installer-paths attribute to install plugins into specific directories. Since this is Local, and the WP site follows a more traditional folder structure, I think you can create a composer.json file in the app folder, next to the public folder:

{
	"require": {
	    "wpackagist-plugin/akismet":"3.0",
	    "wpackagist-theme/astra":"*"
	},
	"extra": {
	        "installer-paths": {
	            "public/wp-content/plugins/{$name}/": [
	                "type:wordpress-plugin"
	            ],
	            "public/wp-content/themes/{$name}/": [
	                "type:wordpress-theme"
	            ]
	        }
	},
	"repositories": [
	        {
	            "type": "composer",
	            "url": "https://wpackagist.org"
	        }
	]
}  

There’s probably some edge case that isn’t accounted for, but at least for me, I was able to install akismet and astra using the built in composer that’s bundled with Local.

Thanks for sharing Ben !

I also need to import wordpress core and some of my themes from Gitlab, to keep a size of repository as small as possible.

I guess I can do it manually but I don’t have any feedback on the way it could work with Flywheel.

Bedrock provides from scratch some nice way to do it so that’s why I use it. I did not already try to deploy it using Flywheel to check if it goes to production easily… (will give a try as soon as my current project is finished).

Thanks again.

Thomas

1 Like