Change Your WordPress Table Prefix in Few Simple Steps

In this tutorial I will show you a step by step process how to change the default WordPress database table name prefix long after your WordPress Website has been online. This is known as "security through obscurity", in which by no means is, on its own, enough to secure your WordPress Website, but it is a one piece of an overall security strategy, as I discussed in my previous tutorial.

By default, WordPress table prefix is wp_, which will be the beginning of all WordPress table names stored in the MySQL database. Upon installing WordPress, you can easily change this by changing the $table_prefix value in your wp-config.php file and WordPress will do the rest. However, you need to do this step on your own since WordPress never alert you to provide a custom table prefix during installation. This is probably the main reason why the default prefix is widely assumed on many WordPress Websites.

Why is this necessary? Because many published WordPress specific SQL injection attacks make the assumption that the table prefix is still the default wp_. Changing it will block some SQL injection attacks, by making it much harder to guess your table names. Changing your database tables prefix protects against many automated attacks, especially used by script kiddies that target default table names. If the security structure of your database is compromised, then changing table names will be meaningless. It is like you lock the windows in your house but you leave the main door open.

This is what this section, taken from the wp-config-sample.php file, looks like:

/**
 * WordPress Database Table prefix.
 * You can have multiple installations in one database if you give each a unique
 * prefix. Only numbers, letters, and underscores please!
 */
$table_prefix  = 'wp_';

To change the database tables prefix long after installing and running your WordPress Website follow these simple 7 steps. Make sure to set aside about fifteen minutes of downtime for your Website. So you might need to do this during off-peak use.

Step1. Backup your Database

Create a backup copy of your WordPress database from your phpMyAdmin Web interface using the Export function. How to do that?

  1. Log into your phpMyAdmin and select your WordPress database.
  2. Click on the "Export" tab at the top.
  3. Follow the instructions as shown in the image below.

Backup WordPress database using phpMyAdmin.

Step2. Change all your WordPress Table Names

In your phpMyAdmin and from your WordPress database, select the SQL tab and enter the following commands to rename all your 11 tables at once and click "GO". Depending on what plugins you installed, you might have more tables starting with “wp_” prefix, that need editing, make sure to rename all tables.
For instance, let´s say you want to replace wp_ with wpr12f_, then Run the following SQL commands as shown in the code and image below.

Rename table wp_commentmeta TO wpr12f_commentmeta;
Rename table wp_comments TO wpr12f_comments;
Rename table wp_links TO wpr12f_links;
Rename table wp_options TO wpr12f_options;
Rename table wp_postmeta TO wpr12f_postmeta;
Rename table wp_posts TO wpr12f_posts;
Rename table wp_terms TO wpr12f_terms;
Rename table wp_term_relationships TO wpr12f_term_relationships;
Rename table wp_term_taxonomy TO wpr12f_term_taxonomy;
Rename table wp_usermeta TO wpr12f_usermeta;
Rename table wp_users TO wpr12f_users;

Renaming WordPress table names using SQL commands in phpMyAdmin Web interface.

If you can´t see the new table names, either refresh the page or logout from phpMyAfmin and log back in.

In my case single quotes around table names cause syntax error. In your case you might need to add single quotes.

Step3. Edit the _options Table

Now search the wpr12f_options table for any instances of the old prefix (wp_). To do this, select the wpr12f_options table and click on the “Browse” tab. You will see all the records stored in that table. Search under the option_name field and change wp_user_roles along with any other records (starting with wp_) created by plugins, custom scripts, and probably others. Rename any options that begin with wp_ to the new prefix. You can change each record by clicking on the "Edit" pencil image for that record. Make sure not to miss any records.

Browse the WordPress database _options table in phpMyAdmin Web interface.

You can also execute a SQL command to find the records that need editing:

SELECT * FROM wpr12f_options WHERE option_name LIKE 'wp_%';

See the image below:

Executing a SQL command in phpMyAdmin Web interface to find records starting with 'wp_' in the 'wpr12f_options' table.

Step4. Edit the _usermeta Table

Now do the same thing for the wpr12f_usermeta table, search for all instances of the old wp_ prefix. Select the wpr12f_usermeta table and then click on the “Browse” tab. Search under the meta_key field all records that start with the old wp_ prefix. Edit each record it by clicking on the "Edit" pencil image for that record. Make sure not to miss any records.

Browse the WordPress database _usermeta table in phpMyAdmin Web interface.

Do NOT edit any records starting with the prefix _wp_, but only records staring with the prefix wp_

You can also execute a SQL command here to find the records that needs editing:

SELECT * FROM wpr12f_usermeta WHERE meta_key LIKE 'wp_%';

See the image below:

Executing a SQL command in phpMyAdmin Web interface to find records starting with 'wp_' in 'wpr12f_usermeta' table.

Step5. Edit Your wp-config.php File

Now open your wp-config.php file and change your table prefix in from wp_ to whatever prefix you decide to use (wpr12f_ for this tutorial). Save and upload it to your server, as shown in the following code snippet:

/**
 * WordPress Database Table prefix.
 * You can have multiple installations in one database if you give each a unique
 * prefix. Only numbers, letters, and underscores please!
 */
//Rename the table prefix variable from the default 'wp_' to a new hard to 
//guess and more secure prefix.
$table_prefix  = 'wpr12f_';

Step6. Test your WordPress Website

Now vigorously test your Website for proper functionality. Test your plugins, contact form, search field, posts, pages, comments, administration area, and anything else you can think of. If your Website is working as before, then the tables prefix change is a success.

Step7. Do Another Backup

Go ahead and do another backup of your database as a good and wise habit.

Conclusion

To emphasize again, changing the WordPress database table prefix helps in some automated scripts that target tables with the default prefix. This is one step in an overall security strategy for your Website. Automated scripts target the WordPress database using the default table prefix. Changing this to something else protects your Website against such attacks. However, if someone is able to access your database, it will not matter what you name your tables.

If you run into any problems or if things need to be done differently or any other ideas you have, don´t hesitate to comment.

Did you know! There are also plugins that change the table prefix, like WP Security Scan. But I haven´t tried it myself so I can´t vouch for it. Also it is safer to take the manual approach, because if you run into a problem you can back track your steps, which is not the case for a plugin.

If you enjoyed this post, please consider: linking back to it, subscribing by email to future posts, or subscribing to the RSS feed to have new articles delivered to your feed reader. Thanks!

About the Author |
Boutros is a professional Drupal & WordPress developer, Web developer, Web designer, Software Engineer and Blogger. He strives for pixel perfect design, clean robust code, and user-friendly interface. If you have a project in mind and like his work, feel free to contact him. Connect with Boutros on Twitter, and LinkedIn.
Visit Boutros AbiChedid Website.

25 Responses to “Change Your WordPress Table Prefix in Few Simple Steps”

  1. Sam Miller says:

    As an alternative to entering a large amount of sql commands, as suggested above, it seems easier to just export the wordpress database from phpmyadmin in cpanel. Do a search and replace with your favorite code editor, replacing all occurrences of “wp_” with the new prefix. Then drop the tables from the current database, again through phpmyadmin, and import the new database. And change your database prefix setting in your wp-config.php to the new one.

    Visit detail instructions here:
    http://www.websitedefender.com/wordpress-security/change-wordpress-database-prefix/

    • Hi Sam,

      That’s another way to change the Table prefix, and I do use it sometimes but I don’t like to advertise it, because you really need to be careful with your method. You can’t just do a global ‘replace all’ as some plugins do use the ‘wp_’ or ‘WP_’ prefix for their tables and don’t need replacing.
      Also what about if you mention the ‘wp_’ in your posts/codes, it will also be replaced with a ‘global replace’.
      I remember once I did a ‘global replace’, and it got me into trouble and I had to revert back and did a ‘replace’ one term at a time.
      But If you know what you’re doing, your method works fine.
      Thanks.

  2. brooke says:

    Ahhh Thank you so much for your tutorial!
    I had a similar problem to Yuri but it turns out I had and extra ‘_’ on one of my ‘wp_’ replacements!

    Much appreciation for the great tutorial!

  3. brooke says:

    Hi Boutros,

    Thank you for your article! It’s very easy to follow for the non tecchie! I have a similar problem to Yuri when I try to log in I get the message “You do not have sufficient permissions to access this page.”

    I have updated all references to wp_ in my usermeta and options tables and changed ALL table prefixes and the config file. Is there anything else I could have missed that would disable my login ability?

    Thanks in advance for any ideas you can offer!

  4. parw says:

    Hi, I have followed ur instructions. But I have got this problem when i want to log into my wordpress dashboard:

    Warning: Cannot modify header information – headers already sent by (output started at /home/username/public_html/myblog.com/wp-settings.php:336)
    in /home/username/public_html/myblog.com/wp-includes/pluggable.php on line 934

    I have 2 blogs in 1 account ( 1 main and 2nd addondomain) so i have 2 database. But i wanted to change only 1 first. So i have modified only 1 database and wp-config.php related to that blog.
    But i have got same problem when i want to log into another wordpress blog …

    What should I do to solve it.
    Please help ..

    • Hi,
      I think that you forgot to change some of your old table prefix in the databse (you still have some in the database that need to be renamed).
      From your ‘phpMyAdmin’ do a quick search for the old table prefix and see what you have missed.
      Hope this helps.
      Boutros.

  5. Hafism says:

    Thanks Boutros! I did not change it and it seems to be ok. Nice tutorial anyway :)
    Hafism.

  6. Rolando says:

    Thank you for your detailed instructions! Worked out great!

  7. Hafism says:

    one more thing, i had plugin which is using uppercase WP_. do i need to change it as well? or just the lowercase wp_?

    thanks in advance.

    • Hi Hafism,

      I am not sure what plugin is that. First try not to change anything for the plugin table, and see if it works. If not change it.
      Depending what plugin you are using, sometimes it is easier to just remove and re-install the plugin (if you can…).
      Most times, for caching and statistics plugins, you don’t have to change their tables.
      {ALWAYS REMEMBER WHAT YOU CHANGED IN CASE YOU NEED TO REVERT BACK)
      Hope this helps,
      Boutros.

  8. Hafism says:

    Hi. Nice tutorial. Btw, under the edit _options table, what if the wp_ prefix exist in option value. do we have to edit it also? or just the option name?

    Thanks

  9. Jad Aoun says:

    Just what I was looking for: step-by-step instructions. Thanks, you’ve been a great help.

  10. Yuri says:

    Thank you for the information, I changed all of the table prefixs and the prefix in the config.php file the other day, I went to log in and could not. I kept getting the following error”You do not have sufficient permissions to access this page.” I knew something was not right, then I found this blog and saw that I also needed to change the table prefixes in the users and usermeta table. You just saved me a huge hassle, thanks again.

  11. Alex Andrei says:

    Thank you very much for this tutorial!

  12. Jake Willis says:

    Awesome tutorial. I couldn’t have explained it any better. You are very talented , Thank you.