wp-configr

Let’s say you’re scripting an automated deployment of WordPress. Chances are, you’ll need to edit a wp-config.php file.

By default, wp-config.php looks a bit like this:

<?php
define( 'DB_NAME',     'database_name_here' );
define( 'DB_USER',     'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST',     'localhost' );
// ...

…but you’ll need to edit it to look more like this:

<?php
define( 'DB_NAME',     'wp_foo' );
define( 'DB_USER',     'dodxcqhmudmh' );
define( 'DB_PASSWORD', 'abrZlVkLOTzh' );
define( 'DB_HOST',     'wp.int.mycorp.com' );
// ...

wp-configr is a Python package I wrote to read and write key-value pairs in wp-config.php files.

The package can be used directly via the command-line or imported into a Python script.

Installation

pip install wpconfigr

Command-line usage

To create or update a key with a string value, use the following arguments:

  • --filename: Full path of the wp-config.php file.
  • --key: Key to create/update.
  • --value: String value to set.
  • --log-level: (Optional) Log level.

For example, this command will update /www/wp-config.php to create/update a key named DB_NAME with value wp_001:

python -m wpconfigr --filename /www/wp-config.php \
                    --key      DB_NAME \
                    --value    wp_001

To set a Boolean value, use --set-true or --set-false instead of --value.

For example, this script will update /www/wp-config.php to enable WordPress debugging:

python -m wpconfigr --filename /www/wp-config.php \
                    --key      WP_DEBUG \
                    --set-true

To read a value, don’t specify --value, --set-true or --set-false.

For example, this script will print the value of the DB_NAME key in /www/wp-config.php to stdout:

python -m wpconfigr --filename /www/wp-config.php \
                    --key      DB_NAME

Code usage

Loading wp-config.php content

The package can interact directly with a wp-config.php file, or it can work with a string if you already have the content loaded into memory.

To load wp-config.php content from a file, instantiate the WpConfigFile class.

from wpconfigr import WpConfigFile
config = WpConfigFile('/www/wp-config.php')

To load wp-config.php content from a string, instantiate the WpConfigString class:

from wpconfigr import WpConfigString
config = WpConfigString('<?php\ndefine( \'DB_NAME\', \'wp_foo\' );\n// ...')

Setting a value

Use the set method to set a value. For example, this code will create/update the DB_NAME key value to wp_001:

config.set('DB_NAME', 'wp_001')

This code will enable debug mode in your WordPress installation:

config.set('WP_DEBUG', True)

Warning! Take care that you specify Boolean True and False for key-value pairs which expect Boolean values. The code below will set the value of WP_DEBUG to the string value "false", which WordPress (well, PHP) will read as truthy:

# Don't do this:
config.set('WP_DEBUG', "false")

# Do this:
config.set('WP_DEBUG', False)

The set method on WpConfigFile will update the file immediately. There is no separate method call to commit the changes.

Getting a value

Use the get method to get a value.

For example, this code will get the value of the DB_NAME key:

db_name = config.get('DB_NAME')

Full code example

This example will load /www/wp-config.php, print the database connection details, then force WordPress into debug mode.

from wpconfigr import WpConfigFile

config = WpConfigFile('/www/wp-config.php')

print('Database     :' + config.get('DB_NAME'))
print('Database host:' + config.get('DB_HOST'))

config.set('WP_DEBUG', True)

Report a bug, request a feature or ask a question

Please use the issues page to raise a bug, request a feature or ask a question.

Licence

wp-configr is published under the MIT License.

Comments