WordPress Bash Scripts

Posted in Web Development on 06.10.2015 by @chasebadkids

I’ve been doing a lot of bash scripting with WordPress, especially with WP CLI. Here are some snippets I found useful.

All the scripts assume a standard wp-config.php file with the database variables set.

Getting the Database table prefix

If you need to make any SQL queries using WP CLI, you will need the database prefix for table names

Getting WP Config Database user and password

If you’ve just downloaded an install of WordPress, these will give you the details to retrieve the database

Setting WP Config database user and password

If you download a copy of an install, you may have to modify the DB details, this will do that automatically

Adding SUNRISE to  wp-config.php

Modifying wp-config.php to add new items isn’t as simple as appending to the end.

Getting a list of active plugins and looping over them

Here I export the active plugin list as a CSV with a single column and ignore the name entry, letting me iterate through active plugins

List all blogs via WP CLI

Listing and processing all blogs and sites on a multisite network isn’t as trivial as it’s made out to be, here is how you get the site id, URL, and blog id individually

Installing Domain mapping via WP CLI

Simply installing and activating domain mapping isn’t enough, you need to move sunrise.php and setup the necessary DEFINE in wp-config.php

Getting the domain name of a WP site via WP CLI

If I’m in an install and want a clean URL such as without the trailing slash and protocol identifier, I would do this:

Get the blog ID of a blog given a URL

WP CLI can list all sites for you, but sometimes you only have a URL to go by and need to find its blog ID

Setting Up Domain Mapping via WP CLI

A manual SQL query is needed for this, followed by the setting of some options.

NOTE: Domain mapping only generates the table when you visit the domain mapping admin page, so if you setup a site and run the query it will fail if nobody has logged into the site and visited the network admin panel.