I have a relatively complex laravel project, within it I have a wordpress installation for a blog on the side which is installed with composer.
In my wp-config.php I use an include to a file in my config directory called application.php (for organisation purposes). I have various cases of define('XXX', 'config stuff'); and similar in the file. When 'config stuff' is hard coded the site works perfectly, but recently I'm trying to use dotenv installed by composer to pull in values from my .env with getenv()
require_once(LARAVEL_PATH . '/vendor/autoload.php');
$dotenv = new Dotenv\Dotenv(APP_ROOT_DIR);
$dotenv->load();
When I var_dump my getenv('example_env_constant') it gives me the correct values absolutely fine. So I went about setting these throughout my application.php file.
But now when I load up the site I get a large number of
Notice: Constant XXX_XXXX_XXX already defined in /path/to/application.php on line X
One for every single define('XXX', value);
And also a
Cannot modify header information
On testing I've found that my wp-config.php file is being run once. But somehow my application.php is being run twice. The 1st run is from my include call in wp-config.php as it should be. The 2nd run which is triggering the errors is happening within wp-settings.php on line 326
do_action( 'plugins_loaded' );
I have no idea how this is happening.
If I remove the dotenv code from application.php and instead put it directly into my wp-config.php the behaviour is exactly the same, with wp-config.php running once and application.php running twice.
Now if I remove application.php and put all of my code into wp-config.php as wp config is traditionally done. Then I get the exact same issue again and it references the deleted file... this of course indicates a cache issue at this point although I don't think cache is the cause of the original issue. Running a cache flush using the wp cli doesn't work as it actually manages to hit the same errors from application.php when flushing. Never mind that caching is disabled in the first place anyway. This isn't a browser cache issue here either as a new incognito chrome instance and hard refreshing makes no difference.
This is a long read so sorry about that, I hope I was clear enough. I'm quite confused as to how this is happening and any help or tips for debugging this would be great. Maybe I've missed something very obvious as it appears that using dotenv for my wp config thoroughly breaks everything in ways that I've never seen before. Worst comes to worst I'll go back to hardcoding the wp-config file
Update:
I was mistake about deleting the application.php file not stopping the related bugs. It stops the require bugs but I get others instead. If I just delete the contents of application.php then I have no difference.
Something is majorly wrong, if anyone simply has any debugging advice it would be highly appreciated
Files:
public/help-advice/wp-config.php
config/application.php
wordpress installation is in public/help-advice/wp
non composer generated wp files are in public/help-advice/app or
public/help-advice
Pasted code if you can't use pastebin:
This is the config.php
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the
* installation. You don't have to use the web site, you can
* copy this file to "wp-config.php" and fill in the values.
*
* This file contains the following configurations:
*
* * MySQL settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* #link https://codex.wordpress.org/Editing_wp-config.php
*
* #package WordPress
*/
/*
* Caching
*/
define('WP_CACHE', true);
define('LARAVEL_PATH', dirname(__FILE__) . '/../..'); // Make sure this is pointed to same server
require_once(LARAVEL_PATH . '/vendor/autoload.php');
require_once(LARAVEL_PATH . '/config/application.php');
require_once(ABSPATH . 'wp-settings.php');
This is the application.php
<?php
//This file pulls in data for WP and is included in the wp-config.php file within help-advice
/*
* Base paths
*/
define('APP_ROOT_DIR', dirname(__DIR__));
// $dotenv = new Dotenv\Dotenv(APP_ROOT_DIR);
// $dotenv->load();
// this one above works but causes this file to run twice causing errors, the one below errors
// if (file_exists(APP_ROOT_DIR . '/.env')) {
// Dotenv\Dotenv::load(APP_ROOT_DIR);
// }
define('APP_PUBLIC_DIR', APP_ROOT_DIR . '/public/help-advice');
define('APP_STORAGE_DIR', APP_ROOT_DIR . '/storage');
define('APP_LOG_DIR', APP_STORAGE_DIR . '/logs');
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'redacted');
/** MySQL database username */
define('DB_USER', 'redacted');
/** MySQL database password */
define('DB_PASSWORD', 'redacted');
/** MySQL hostname */
define('DB_HOST', '127.0.0.1');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8mb4');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
/**##+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {#link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* #since 2.6.0
*/
define('AUTH_KEY', 'redacted');
define('SECURE_AUTH_KEY', 'redacted');
define('LOGGED_IN_KEY', 'redacted');
define('NONCE_KEY', 'redacted');
define('AUTH_SALT', 'redacted');
define('SECURE_AUTH_SALT', 'redacted');
define('LOGGED_IN_SALT', 'redacted');
define('NONCE_SALT', 'redacted');
/*
* Debugging/errors
*/
define('APP_DEBUG', (boolean) getenv('APP_DEBUG'));
// Always log errors
ini_set('log_errors', 1);
ini_set('error_log', APP_LOG_DIR . '/wp_debug.log');
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', false);
define('WP_DEBUG_DISPLAY', APP_DEBUG);
define('SCRIPT_DEBUG', APP_DEBUG);
/*
* URLs
*/
define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'].'/help-advice/wp');
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'].'/help-advice');
/*
* Custom Content Directory (/public/help-advice/app)
*/
define('CONTENT_DIR', '/app');
define('WP_CONTENT_DIR', APP_PUBLIC_DIR . CONTENT_DIR);
define('WP_CONTENT_URL', WP_HOME . CONTENT_DIR);
//google analytics
define('GA_PROPERTY_ID',getenv('GA_PROPERTY_ID'));
/**##-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/../public/help-advice/wp/');
I have no idea why it runs twice or is triggered by the do_action( 'plugins_loaded' ); on the 2nd run.
Since this is a fairly unique build/situation and I don't expect this to be easily fixed or for more than a couple of people to ever need this. So instead of continuing to tear my hair out I added a check to every definition in my application.php to see if it already existed, if so don't re-define. example: if (!defined('WP_DEBUG')) {define('WP_DEBUG', true);}
I was also getting errors from advanced-cache.php missing as a result of the dotenv being added to application.php, no idea why, especially since I never installed or had anything to do with advanced-cache. So I simply disabled this define('WP_CACHE', false);
Related
I am using port 8080 for my local xampp server.
I have configured the setting for the wp-config.php file as below.
Going to localhost:8080 has no issue and can access to php admin very quickly.
Problem is with the wordpress.
i have also tried setting the host file as below:
hosts
localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
::1 localhost
wp-config.php
<?php
/**
* The base configuration for WordPress
*
* The wp-config.php creation script uses this file during the
* installation. You don't have to use the web site, you can
* copy this file to "wp-config.php" and fill in the values.
*
* This file contains the following configurations:
*
* * MySQL settings
* * Secret keys
* * Database table prefix
* * ABSPATH
*
* #link https://codex.wordpress.org/Editing_wp-config.php
*
* #package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'kimseng');
/** MySQL database username */
define('DB_USER', 'kimseng');
/** MySQL database password */
define('DB_PASSWORD', 'kimseng');
/** MySQL hostname */
define('DB_HOST', 'localhost:8080');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
/**##+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {#link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* #since 2.6.0
*/
define('AUTH_KEY', 'put your unique phrase here');
define('SECURE_AUTH_KEY', 'put your unique phrase here');
define('LOGGED_IN_KEY', 'put your unique phrase here');
define('NONCE_KEY', 'put your unique phrase here');
define('AUTH_SALT', 'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT', 'put your unique phrase here');
define('NONCE_SALT', 'put your unique phrase here');
/**##-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each
* a unique prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*
* For information on other constants that can be used for debugging,
* visit the Codex.
*
* #link https://codex.wordpress.org/Debugging_in_WordPress
*/
define('WP_DEBUG', false);
/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
define( 'WP_ALLOW_REPAIR', true );
My root is localhost:8080/test and it takes forever to load and does not throw any error.
Anybody has any idea what is wrong?
Wordpress version 4.9.2
Recently, I migrated WordPress site to Amazon AWS. I am getting this weird problem where wp-amdin folder gets deleted regularly like in once a week. I recover this folder using my backup and the site is live again.
The debug flag is true on wp-config.php. But there's no error reported on the log file.
define('WP_DEBUG', true);
But I am not sure what's causing this problem. Has anyone experienced similar problem?
Current Wordpress Version - 4.2.2
wp-config.phpcontent here with database connection details changed:
<?php
/**
* The base configurations of the WordPress.
*
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
* by visiting {#link http://codex.wordpress.org/Editing_wp-config.php Editing
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
*
* This file is used by the wp-config.php creation script during the
* installation. You don't have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
*
* #package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'database_name');
/** MySQL database username */
define('DB_USER', 'db_user');
/** MySQL database password */
define('DB_PASSWORD', 'password');
/** MySQL hostname */
define('DB_HOST', 'localhost');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
/**##+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {#link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* #since 2.6.0
*/
define( 'AUTH_KEY', 't`DK%X:>xy|e-Z(BXb/f(Ur`8#~UzUQG-^_Cs_GHs5U-&Wb?pgn^p8(2#}IcnCa|' );
define( 'SECURE_AUTH_KEY', 'D&ovlU#|CvJ##uNq}bel+^MFtT&.b9{UvR]g%ixsXhGlRJ7q!h}XWdEC[BOKXssj' );
define( 'LOGGED_IN_KEY', 'MGKi8Br(&{H*~&0s;{k0<S(O:+f#WM+q|npJ-+P;RDKT:~jrmgj#/-,[hOBk!ry^' );
define( 'NONCE_KEY', 'FIsAsXJKL5ZlQo)iD-pt??eUbdc{_Cn<4!d~yqz))&B D?AwK%)+)F2aNwI|siOe' );
define( 'AUTH_SALT', '7T-!^i!0,w)L#JK#pc2{8XE[DenYI^BVf{L:jvF,hf}zBf883td6D;Vcy8,S)-&G' );
define( 'SECURE_AUTH_SALT', 'I6`V|mDZq21-J|ihb u^q0F }F_NUcy`l,=obGtq*p#Ybe4a31R,r=|n#=]#]c #' );
define( 'LOGGED_IN_SALT', 'w<$4c$Hmd%/*]`Oom>(hdXW|0M=X={we6;Mpvtg+V.o<$|#_}qG(GaVDEsn,~*4i' );
define( 'NONCE_SALT', 'a|#h{c5|P &xWs4IZ20c2&%4!c(/uG}W:mAvy<I44`jAbup]t=]V<`}.py(wTP%%' );
/**##-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each a unique
* prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/**
* WordPress Localized Language, defaults to English.
*
* Change this to localize WordPress. A corresponding MO file for the chosen
* language must be installed to wp-content/languages. For example, install
* de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
* language support.
*/
define('WPLANG', '');
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*/
define('WP_DEBUG', true);
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
You should change your secret keys in wp-config.php file. Visit this link to know more.
I moved my wordpress from Openshift to another server. Howerver, It does not work, my website only display white page. I tried to change the Openshift variable into file wp-config.php, but it still not worked.
Can anyone please help me resolve this problem?
My wp-config.php:
<?php
/**
* The base configurations of the WordPress.
*
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
* by visiting {#link http://codex.wordpress.org/Editing_wp-config.php Editing
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
*
* This file is used by the wp-config.php creation script during the
* installation. You don't have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
*
* #package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'mydb');
/** MySQL database username */
define('DB_USER', 'mydb');
/** MySQL database password */
define('DB_PASSWORD', 'mydb123');
/** MySQL hostname */
define('DB_HOST', 'localhost'));
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
/**##+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {#link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* #since 2.6.0
*/
// This is where we define the OpenShift specific secure variable functions
// This function gets called by openshift_secure and passes an array
function make_secure_key($args) {
$hash = $args['hash'];
$key = $args['variable'];
$original = $args['original'];
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$chars .= '!##$%^&*()';
$chars .= '-_ []{}<>~`+=,.;:/?|';
// Convert the hash to an int to seed the RNG
srand(hexdec(substr($hash,0,8)));
// Create a random string the same length as the default
$val = '';
for($i = 1; $i <= strlen($original); $i++){
$val .= substr( $chars, rand(0,strlen($chars))-1, 1);
}
// Reset the RNG
srand();
// Set the value
return $val;
}
// Generate OpenShift secure keys (or return defaults if not on OpenShift)
$array = openshift_secure($_default_keys,'make_secure_key');
// Loop through returned values and define them
foreach ($array as $key => $value) {
define($key,$value);
}
/**##-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each a unique
* prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/**
* WordPress Localized Language, defaults to English.
*
* Change this to localize WordPress. A corresponding MO file for the chosen
* language must be installed to wp-content/languages. For example, install
* de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
* language support.
*/
define('WPLANG', 'vi');
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*/
define('WP_DEBUG', false);
/**
* We prefer to be secure by default
*/
define('FORCE_SSL_ADMIN', false);
/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/** Tell WordPress where the plugins directory really is */
if ( !defined('WP_PLUGIN_DIR') && is_link(ABSPATH . '/wp-content/plugins') )
define('WP_PLUGIN_DIR', realpath(ABSPATH . '/wp-content/plugins'));
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
/**define('WP_HOME','http://www.yode.vn');
define('WP_SITEURL','http://www.yode.vn');*/
Whenever I move Wordpress websites, I always backup the database first.
1) Install the current version of Wordpress on the new site and make sure it works.
2) Install the plugins and make sure it works.
3) Install the theme and make sure it works.
4) Then, import the database.
You shouldn't have to screw around with wp-config.php because step 1 took care of that.
Go to https://api.wordpress.org/secret-key/1.1/salt/
Generate new Keys and replace block started from /**##+ till /**##-*/
Here is full instruction how to migrate wordpress blog from OpenShift
I'm trying to edit my wp-config file so my wordpress information is hidden on my site. When I edit it, the entire site goes blank. When I go to the wordpress dashboard I get the following error message:
ERROR:The themes directory is either empty or doesn’t exist. Please check your installation
Here is my wp-config file unedited:
<?php
/**
* The base configurations of the WordPress.
*
* This file has the following configurations: MySQL settings, Table Prefix,
* Secret Keys, WordPress Language, and ABSPATH. You can find more information
* by visiting {#link http://codex.wordpress.org/Editing_wp-config.php Editing
* wp-config.php} Codex page. You can get the MySQL settings from your web host.
*
* This file is used by the wp-config.php creation script during the
* installation. You don't have to use the web site, you can just copy this file
* to "wp-config.php" and fill in the values.
*
* #package WordPress
*/
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'XXXXX');
/** MySQL database username */
define('DB_USER', 'XXXXX');
/** MySQL database password */
define('DB_PASSWORD', 'XXXXXX');
/** MySQL hostname */
define('DB_HOST', 'XXXXXXX');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');
/**##+
* Authentication Unique Keys and Salts.
*
* Change these to different unique phrases!
* You can generate these using the {#link https://api.wordpress.org/secret- key/1.1/salt/ WordPress.org secret-key service}
* You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
*
* #since 2.6.0
*/
define('AUTH_KEY', '`V)^|<}xOl98X. Mm,|P1(q6tAusj5gjFi1.m} [`/,q`o');
define('SECURE_AUTH_KEY', '6])[E7esUf-b-_a_r!K=n0SJ-!pr+ERArx,]+#B8D(+kO');
define('LOGGED_IN_KEY', 'P-oUq{8S}xo]zu%a5~FN-ssS)DWR=+$]Y q)h;nn>');
define('NONCE_KEY', 'M+zr6C1j.Fly%D)&Il>.E!Tq8Ga+;A#EyE.AjFwrWa:+yA0}|xT%');
define('AUTH_SALT', 'V#1bgex#Wa`47.BJefJq6[joO9V,8taI~c4Wq{3TJ2_>(?C+|Bw^^');
define('SECURE_AUTH_SALT', 'J6nPC&`O5O|Z{-!k>#-!YcK9UzqX=M6; The;TK');
define('LOGGED_IN_SALT', 'rxBL~!l~.#%}.ju_[.Rv+;pj$A_nVf?FS6|EU>sr##');
define('NONCE_SALT', '4$`-yS=LH#qdzidp{!mc-<9|||mi!hila-&!zLHr<');
/**##-*/
/**
* WordPress Database Table prefix.
*
* You can have multiple installations in one database if you give each a unique
* prefix. Only numbers, letters, and underscores please!
*/
$table_prefix = 'wp_';
/**
* WordPress Localized Language, defaults to English.
*
* Change this to localize WordPress. A corresponding MO file for the chosen
* language must be installed to wp-content/languages. For example, install
* de_DE.mo to wp-content/languages and set WPLANG to 'de_DE' to enable German
* language support.
*/
define('WPLANG', '');
/**
* For developers: WordPress debugging mode.
*
* Change this to true to enable the display of notices during development.
* It is strongly recommended that plugin and theme developers use WP_DEBUG
* in their development environments.
*/
define('WP_DEBUG', false);
/* That's all, stop editing! Happy blogging. */
/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');
I'm trying to add the following code (just before the last line [something about require_once]):
define('WP_CONTENT_FOLDERNAME', 'media');
define('WP_CONTENT_DIR', ABSPATH . WP_CONTENT_FOLDERNAME );
define('WP_CONTENT_URL', 'http://brightpixelstudios.com/'.WP_CONTENT_FOLDERNAME);
define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
Any advice would be greatly appreciated! Thanks!
you have to have a valid pathname for
define('WP_CONTENT_DIR', ABSPATH . **WP_CONTENT_FOLDERNAME** );
for example:
define( 'WP_CONTENT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/blog/wp-content' );
same thing for
define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/plugins' );
define('WP_PLUGIN_URL', WP_CONTENT_URL.'/plugins');
to this:
define( 'WP_PLUGIN_DIR', $_SERVER['DOCUMENT_ROOT'] . '/blog/wp-content/plugins' );
define( 'WP_PLUGIN_URL', 'http://example/blog/wp-content/plugins');
more info here
I'm using phpfog.com for hosting and github.com for issue tracking, etc.
I have two remotes setup, one to phpfog.com, and the other to github.
In the back-end admin of phpfog you can define Environment Variables. I did so there and want to use them in my wp-config file.
Here's the code I used:
/** Hardened Salts for use on github.com, phpfog.com, etc.*/
$AUTH_KEY = getenv('AUTH_KEY');
$SECURE_AUTH_KEY = getenv('SECURE_AUTH_KEY');
$LOGGED_IN_KEY = getenv('LOGGED_IN_KEY');
$NONCE_KEY = getenv('NONCE_KEY');
$AUTH_SALT = getenv('AUTH_SALT');
$SECURE_AUTH_SALT = getenv('SECURE_AUTH_SALT');
$LOGGED_IN_SALT = getenv('LOGGED_IN_SALT');
$NONCE_SALT = getenv('NONCE_SALT');
define('AUTH_KEY', $AUTH_KEY);
define('SECURE_AUTH_KEY', $SECURE_AUTH_KEY);
define('LOGGED_IN_KEY', $LOGGED_IN_KEY);
define('NONCE_KEY', $NONCE_KEY);
define('AUTH_SALT', $AUTH_SALT);
define('SECURE_AUTH_SALT', $SECURE_AUTH_SALT);
define('LOGGED_IN_SALT', $LOGGED_IN_SALT);
define('NONCE_SALT', $NONCE_SALT);
There must be a cleaner way of doing this…
You could make it half as long by passing the function result as a constant value without intermediate variable:
define('AUTH_KEY', getenv('AUTH_KEY'));
Or do that in a loop:
$vars = array('AUTH_KEY', 'SECURE_AUTH_KEY', ...);
foreach ($vars as $var) {
define($var, getenv($var));
}
From WordPress 5.5.0
WordPress has added a new function for the environment variables with 3 different possible values.
You can use wp_get_environment_type() function to get the current environment.
Usage example:
If(wp_get_environment_type() === 'development') {
// do something
} else {
// do something
}
By default, if WP_ENVIRONMENT_TYPE is empty or invalid ( anything except development, staging & production), production is returned.
You can define development or staging environment through the wp-config.php file.
define( 'WP_ENVIRONMENT_TYPE', 'development' );
I prefer to use this approach below:
<?php
//GET HOSTNAME INFO
$hostname = $_SERVER['SERVER_NAME'];
//VERIFY WHICH ENVIRONMENT THE APP IS RUNNING
switch ($hostname) {
case 'development.dev':
define('WP_ENV', 'development');
define('WP_DEBUG', true);
break;
case 'staging.mywebsite.com':
define('WP_ENV', 'staging');
define('WP_DEBUG', true);
break;
case 'www.mywebsite.com':
define('WP_ENV', 'production');
define('WP_DEBUG', false);
break;
default:
define('WP_ENV', 'production');
define('WP_DEBUG', false);
}
?>
The best way to use environment variables to control your WP environment is by using DotEnv ( https://github.com/vlucas/phpdotenv )
This approach is laid out in a blog post: https://m.dotdev.co/secure-your-wordpress-config-with-dotenv-d939fcb06e24
The basic approach is to create an .env file in the root of your site with the environment variables.
However there are a few problems with the blog post as DotEnv version 5 no longer uses environment variables by default.
So instead of the code used in the blog post, use this at the top of your wp-config.php file...
$app_env = getenv("APP_ENV");
$file = $app_env == null ? ".env" : ".env.".$app_env;
if(file_exists(__DIR__.'/'.$file))
{
require_once(__DIR__ . '/vendor/autoload.php');
(Dotenv\Dotenv::createUnsafeImmutable(__DIR__,$file))->load();
error_log("Environment loaded from ".$file);
} else {
error_log("*WARNING* environment file not found: ".$file);
}
The .env file looks like this...
# MySQL settings
DB_NAME=wpbench
DB_USER=wpuser
DB_PASSWORD=password
DB_HOST=localhost
DB_CHARSET=utf8
DB_COLLATE=
Defining the constants in the wp-config.php file looks like this...
/** The name of the database for WordPress */
define( 'DB_NAME', getenv('DB_NAME'));
/** MySQL database username */
define( 'DB_USER', getenv('DB_USER'));
/** MySQL database password */
define( 'DB_PASSWORD', getenv('DB_PASSWORD'));
/** MySQL hostname */
define( 'DB_HOST', getenv('DB_HOST'));
/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', getenv('DB_CHARSET'));
/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', getenv('DB_COLLATE'));
Use the APP_ENV variable to switch between variable sets. For example create .env.production and .env.staging files. If the .env file does not exist then the values are pulled from the environment which works well for cloud deployment.