0

I teach students how to fix wordpress sites and I would like to write a script that looks at what information they have in their wp-config.php and add a single letter or number to the database name.

For example the line is such

define('DB_NAME', 'cpanelUser_NameofDB');

I would like to add a number or a line to the end of NameofDB

I can use this to isolate the cpanelUser_NameofDB

grep -i 'DB_NAME' wp-config.php | cut -d"'" -f4

but I'm not sure how to add information, nor if this is the correct script I should run to get there. I would also like it to not matter what the name of the database is since it will be ran on multiple sites. I'm sure I could use regex but I'm not too versed in that and would not know where to start. Help please!

1
  • Did you think about the possibiltity, that the define statement might span several lines, including arbitrary comments? Commented Dec 20, 2019 at 6:49

2 Answers 2

1

You can use WP CLI on the server:

Go to the website root:

cd /var/www/mysite.com/htdocs

...and list all the wp-config values:

wp config list

wp config list - result example

...or set the value you need to change:

sudo wp config set DB_NAME put_my_custom_db_name_here --allow-root

See more WP CONFIG features here: https://developer.wordpress.org/cli/commands/config/

Sign up to request clarification or add additional context in comments.

Comments

0

You can use sed for this purpose to fix this line:

#!/bin/bash

id=34

sed -i "s/^.*DB_NAME.*$/define('DB_NAME', 'cpanelUser_NameofDB${id}');/" wp-config.php

flag -i means that we do change in file directly.

Alternatively you can make template file and generate new file while sed works on stdin and stdout

#!/bin/bash

id=34

cat wp-config.php-template| sed "s/^.*DB_NAME.*$/define('DB_NAME', 'cpanelUser_NameofDB${id}');/" > wp-config.php.$id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.