Editing WordPress wp-config.php with wp-cli and adding variables with sed
The WordPress CLI (command line interface) is a huge step for enabling developers and devops/sysadmin folks manage their WordPress installations. It’s awesome for writing scripts to automate key tasks.
This post covers editing the WordPress configuration file wp-config.php
with the WP-CLI’s wp config
command, as well as using the sed
command to address a key missing feature of WP-CLI: the ability to add new config variables.
There are plenty of reasons you might want to edit wp-config.php
via a script or directly via the command line. For example, developers might appreciate a bash script that sets WP_DEBUG to true, and a devops person might want to create an automated deploy process that ensures key SMTP settings are in place.
The rest of this post will assume your WP-CLI command can be invoked with wp
. Depending on how you installed it, the command might be available to you as wp-cli
or wp-cli.phar
. If you are running wp-cli’s phar file directly, substitute php wp-cli.phar
in place of wp
in the examples.
Editing config variables with wp-cli’s config command
WP-CLI supports modifying config variables in wp-config.php
via wp config
.
This is a great feature, albeit with the noted catch that wp config
only works for a given variable if that variable is already defined in wp-config.php
. I’ll show you how to work around that and add variables with sed
in the next section.
The following example uses sudo
to run wp config
as the _www
web server user, the default web server user on MacOS. On Ubuntu and many other linux distros, this user is likely www-data
:
sudo -u _www wp config set FS_METHOD 'direct'
sudo -u _www wp config set DISABLE_WP_CRON true
sudo -u _www wp config set WP_DEBUG true
sudo -u _www wp config set WP_DEBUG_LOG true
These are some of the most popular config options that developers and admins want to modify.
Adding config variables to wp-config.php using sed
There are a number of command-line utilities on Linux and Unix-like systems that can edit text files. One of the most popular is sed
, the quintessential stream editor. Unix admins have been working with streams forever, long before NodeJS made it cool :).
sed
is pre-installed on most systems and can be used directly in the Terminal or inside a bash (or other shell) script.
The following example uses sed
to add config variables to wp-config.php
right before the well-known “That’s all, stop editing!” comment line found in the file.
This snippet works on MacOS and elsewhere. MacOS and OSX, as well as their related family in the BSD/unix world, generally bundle a classic POSIX-compliant version of the sed
command which is more limited vs. the more common and more popular GNU sed
that ships with major linux distributions like Ubuntu. If you’re on linux, delete the double quotes ''
immediately following the -i
flag to be compatible with GNU sed
.
Editing wp-config.php
with sed
:
sed -i '' '/\/\* That.s all, stop editing! Happy blogging. \*\// i\
// FX_SCRIPT FS_METHOD \
define( "FS_METHOD", "direct" ); \
\
// FX_SCRIPT WP_DEBUG \
define( "WP_DEBUG", true ); \
define( "WP_DEBUG_LOG", true ); \
\
// FX_SCRIPT DISABLE_WP_CRON \
define( "DISABLE_WP_CRON", true ); \
\
' wp-config.php
The -i
option tells sed
to edit the file in-place i.e. modify the file directly. Otherwise, sed
lives up to its name and streams output to stdout.
The MacOS version of sed
requires a backup file to be specified as the first argument whenever the -i
option is used. You can pass empty quotes ''
to specify no backup file as demonstrated in the example.
The linux version of sed
does not require a backup filename to be specified. You can simply delete the ''
arguments as noted above.
The way that single and double quotes are used is very important for getting this command to work. Getting them right is one of the trickiest parts about using sed
.
Also note how backslashes are used at the end of each line. This is required to make the command portable and universal: classic sed
(BSD/Unix/MacOS) does not recognize \n
as a placeholder for the newline character while GNU sed
(linux) does. The backslashes enable a trick to use actual newlines instead of placeholders.
Finally, my example adds comment lines that start with // FX_SCRIPT
before each change (get it? FX = firxworx, the name of this blog!). I do this to make it easy to search with grep
and/or visually look for changes in wp-config.php
files that were made by my scripts. You may wish to follow a similar practice. This makes it easier to write other scripts that might find and comment out or delete these entries at a later time.