Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read variables from wp-config.php

I'm busy writing a basic migration scripts for some WP sites, and i'm trying to automate creating mysql database and user credentials from reading the wp-config file

how can i read the following variables from the wp-config file so that i effectively end up with 3 variables within a bash script:

/** The name of the database for WordPress */
define('DB_NAME', 'somedbname');

/** MySQL database username */
define('DB_USER', 'someusername');

/** MySQL database password */
define('DB_PASSWORD', 'somerandompassword');

eg my output should effectively give me:

WPDBNAME=somedbname
WPDBUSER=somedbuser
WPDBPASS=somerandompassword
like image 268
anthonysomerset Avatar asked Sep 28 '11 17:09

anthonysomerset


4 Answers

Try this:

WPDBNAME=`cat wp-config.php | grep DB_NAME | cut -d \' -f 4`
WPDBUSER=`cat wp-config.php | grep DB_USER | cut -d \' -f 4`
WPDBPASS=`cat wp-config.php | grep DB_PASSWORD | cut -d \' -f 4`
like image 95
Femi Avatar answered Oct 23 '22 14:10

Femi


If you want to use wp-config details to connect to mysql you can use;

mysql -u `cat wp-config.php | grep DB_USER | cut -d \' -f 4` -p`cat wp-config.php | grep DB_PASSWORD | cut -d \' -f 4` -h `cat wp-config.php | grep DB_HOST | cut -d \' -f 4` `cat wp-config.php | grep DB_NAME | cut -d \' -f 4`
like image 33
Matt Clegg Avatar answered Oct 23 '22 16:10

Matt Clegg


you can use awk:

awk -F"[()']" '/^define/{printf "%s=\"%s\"\n", $3, $5;}' < foo.php

This will give you:

DB_NAME="somedbname"
DB_USER="someusername"
DB_PASSWORD="somerandompassword"

Note, that this solution will work with variables containing spaces.

like image 2
Michał Šrajer Avatar answered Oct 23 '22 14:10

Michał Šrajer


find . -name "wp-config.php" -print0 | xargs -0 -r grep -e "DB_NAME" -e "DB_USER" -e "DB_PASSWORD"
like image 1
Jeff Avatar answered Oct 23 '22 14:10

Jeff