Parcourir la source

Detect table prefix

Some plugins like "All In One WP Security & Firewall" rename the
database tables with a prefix and also set $table_prefix in the
wp-config.php. This makes the wp app fail to start since we generate
wp-config.php on each run and table_prefix is lost.
Girish Ramakrishnan il y a 8 ans
Parent
commit
be198e6859
2 fichiers modifiés avec 7 ajouts et 1 suppressions
  1. 6 0
      start.sh
  2. 1 1
      wp-config.php.template

+ 6 - 0
start.sh

@@ -4,6 +4,11 @@ set -eu
 
 readonly WP="/app/code/wp --allow-root"
 
+# Detect the wordpress prefix from existing database
+table_prefix=$(mysql --user=${MYSQL_USERNAME} --password=${MYSQL_PASSWORD} --host=${MYSQL_HOST} ${MYSQL_DATABASE} -e 'SHOW TABLES' --batch 2>/dev/null | sed -n 's/\(.*_\)usermeta/\1/p')
+[[ -n "${table_prefix}" ]] || table_prefix="wp_"
+echo "Using table prefix ${table_prefix}"
+
 # Settings to be updated on every run. Regenerating salts means users have to relogin
 sed -e "s/##MYSQL_DATABASE/${MYSQL_DATABASE}/" \
     -e "s/##MYSQL_USERNAME/${MYSQL_USERNAME}/" \
@@ -18,6 +23,7 @@ sed -e "s/##MYSQL_DATABASE/${MYSQL_DATABASE}/" \
     -e "s/##SECURE_AUTH_SALT/$(pwgen -1cns 64)/" \
     -e "s/##LOGGED_IN_SALT/$(pwgen -1cns 64)/" \
     -e "s/##NONCE_SALT/$(pwgen -1cns 64)/" \
+    -e "s/##TABLE_PREFIX/${table_prefix}/" \
     /app/code/wp-config.php.template > /run/wordpress/wp-config.php # sed -i seems to destroy symlink
 
 # Used for wp rewrite

+ 1 - 1
wp-config.php.template

@@ -29,7 +29,7 @@ define('SECURE_AUTH_SALT', '##SECURE_AUTH_SALT');
 define('LOGGED_IN_SALT',   '##LOGGED_IN_SALT');
 define('NONCE_SALT',       '##NONCE_SALT');
 
-$table_prefix = 'wp_';
+$table_prefix = '##TABLE_PREFIX';
 
 // prevent user from changing the Settings->General, WordPress and Blog address values.
 define('WP_HOME', '##APP_ORIGIN');