Skip to content

Instantly share code, notes, and snippets.

@yousan
Last active May 6, 2020 13:40
Show Gist options
  • Select an option

  • Save yousan/10b44cea1f3d66ee711cc7161e2d5a8a to your computer and use it in GitHub Desktop.

Select an option

Save yousan/10b44cea1f3d66ee711cc7161e2d5a8a to your computer and use it in GitHub Desktop.

Revisions

  1. yousan revised this gist May 6, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion change_db_prefix.sh
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,8 @@ IFS=$'\n'
    for TABLE in ${TABLES}
    do
    NEW_TABLE=`echo ${TABLE} | sed -e 's/^'${PREFIX}'/'${NEW_PREFIX}'/'`
    echo 'RENAME table `'${TABLE}'` TO `'${NEW_TABLE}'`;' | wp db cli # Do RENAME here
    echo 'DROP TABLE IF EXISTS ' ${NEW_TABLE} | wp db cli # Remove tables
    echo 'RENAME TABLE `'${TABLE}'` TO `'${NEW_TABLE}'`;' | wp db cli # Do RENAME here
    done

    # Change table prefix at wp-config.php
  2. yousan revised this gist May 4, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion change_db_prefix.sh
    Original file line number Diff line number Diff line change
    @@ -35,4 +35,4 @@ echo 'RENAME table `'${TABLE}'` TO `'${NEW_TABLE}'`;' | wp db cli # Do RENAME he
    done

    # Change table prefix at wp-config.php
    set table_prefix ${NEW_PREFIX}
    wp config set table_prefix ${NEW_PREFIX}
  3. yousan revised this gist May 4, 2020. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions change_db_prefix.sh
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,12 @@
    # Change WordPress table prefix
    # WP-CLI is reqiured
    # WARNING: BACKUP is strongly recommended
    # usage: change_db_prefix.sh wp_new_prefix_

    # 1. Get all WordPress tables
    # 2. Change wp_options and wp_usermeta values
    # 3. Rename tables
    # 4. Change table prefix at wp-config.php

    PREFIX=`wp db prefix`
    NEW_PREFIX=$1
  4. yousan created this gist May 4, 2020.
    32 changes: 32 additions & 0 deletions change_db_prefix.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/usr/bin/env bash
    # Change WordPress table prefix
    # WP-CLI is reqiured
    # WARNING: BACKUP is strongly recommended

    PREFIX=`wp db prefix`
    NEW_PREFIX=$1

    if [[ -z "$1" ]] ; then
    echo 'No prefix is specified. Please specify new table prefix.'
    exit;
    fi

    # Get all WordPress tables
    # @see https://developer.wordpress.org/cli/commands/db/tables/
    TABLES=`wp db tables --all-tables-with-prefix`

    # Change options and usermeta table value
    wp search-replace ${PREFIX} ${NEW_PREFIX} ${PREFIX}options
    wp search-replace ${PREFIX} ${NEW_PREFIX} ${PREFIX}usermeta

    # Rename tables
    # @see https://www.wpbeginner.com/wp-tutorials/how-to-change-the-wordpress-database-prefix-to-improve-security/
    IFS=$'\n'
    for TABLE in ${TABLES}
    do
    NEW_TABLE=`echo ${TABLE} | sed -e 's/^'${PREFIX}'/'${NEW_PREFIX}'/'`
    echo 'RENAME table `'${TABLE}'` TO `'${NEW_TABLE}'`;' | wp db cli # Do RENAME here
    done

    # Change table prefix at wp-config.php
    set table_prefix ${NEW_PREFIX}