Change Table Prefix For Self Hosted WordPress

The default wonderful 5-minute installation of wordpress keeps the prefix of “wp_” for all its tables. Now why does anybody wants to change the default prefix?

There are 2 reasons I can think of:

  1. The first and foremost is security. Since the default table prefix is world renowned, changing it will put a first level of defense against any vulnerability or a malicious user trying to execute a rogue code.
  2. Each wordpress installation takes in a configuration to tell it which prefix it uses. By keeping the prefix different from each installation, you can host multiple wordpress installations in a single mySQL database (not recommended from large installations).

Here’s the configuration in the wp-config.php that determines what prefix wordpress uses.

There are wordpress plugins that can change the table prefix automatically, however, I prefer the manual approach so that I understand the changes that are being made.

I’m putting down the steps that I did more for my own reference in the future.

1. Take a backup

Since you will be changing the table structure, backup the database. Next, take backup of the wordpress home folder. Keep both the database backup and home folder backup together in another folder. You will need these for performing a full restore later, in case something goes wrong.

2. Change the configuration in the wp-config.php file

Change this to a prefix to your liking. I would suggest making this as random as choosing the password.

3. Change the wordpress tables names

Login to phpMyAdmin and select the wordpress database. Go to the SQL area and enter the command to rename each table. There are many tables that start with wp_, change one table at a time. Below are the list of tables found in my installation, however make sure all your tables are renamed.

4. Edit wp_options table

Next is to change the values in the options table. This table is called tr1mr1_options after the renaming step above.

Browse the table and look for the value called ‘wp_user_role’ under ‘option_name’ column. Change ‘wp_user_roles’ to ‘tr1mr1_user_roles’.

5. Edit wp_user_meta table

Look every row under ‘meta_key’ column in the table tr1mr1_user_meta table, as it is called after the renaming.

Change all keys that start with ‘wp_’ to ‘tr1mr1_’. This may be different in different installations, so make sure you cover all the rows.

Here are some I found.

6. Test

Done! Now test the installation.

If anything goes wrong, you have a backup to restore and start again. Happy blogging!