How to Change WordPress Database Tables Prefix for More Security

Change WordPress database tables prefix

WordPress security has always been a concern because of many websites get hacked every year. The database of a website is the other half which needs to be protected. You can change WordPress database tables prefix.

It’s because when you install WordPress, the database tables prefix is “wp_” which is default and hackers can easily inject code to your WordPress database tables.

For better security, it’s better to change WordPress database tables prefix so that no one can guess it.

The database tables are the vulnerabilities which should be protected. In this tutorial, you will learn two different ways.

Before You Even Start Editing Any File

It’s always recommended to backup your WordPress website and the database before you start changing anything. The database is very sensitive and even a single wrong SQL query can break it.

You should always have a backup plan. And if possible, you should redirect your website to a temporary maintenance page.

It’s because while changing the database tables prefix, your website will go down for a while. You would be interested in knowing the different ways to backup your WordPress database.

The Things Required for Changing the Database Tables Prefix

First of all, you have to change it from the wp-config.php file from your WordPress directory. Then editing of the database will be done.

Let mention the steps.

  • Changing the tables prefix from the wp-config.php file
  • Change the tables prefix from the database
  • Update the options table
  • Update the UserMeta table

Follow these steps one by one.

#1. Changing the Tables prefix from the wp-config.php File

This file is one of the most important files of the WordPress directory because it’s responsible¬†for making the database connection with the rest of your website.

That’s why it’s recommended to protect the wp-config.php file to secure it.

To change the database tables prefix, you have to find a code of the default tables prefix.

$table_prefix = ‘wp_’;

You will find this code if you haven’t changed the tables prefix ever before.

You can replace the default ‘wp_’ to anything you want including numbers, letters, and underscore. Make sure, you keep the underscore at the last.

Let me show you some example.

  • st23_ti_
  • gtnsl_383_
  • nts4t_
  • lik4ht_
  • tyi0ns_

You can change it like this and update the code. Let say, you choose ‘stw1_’ as the new tables prefix.

$table_prefix = ‘stw1_’;

Save the file. Now if you try to open your website then it will either show nothing or the WordPress installation page will appear.

It’s because the database credentials aren’t matching. To fix this, you have to take the next step.

#2. Change the Tables Prefix from the Database

You have to open the database using the cPanel of your web hosting.

Login the cPanel and under the databases section, open the phpMyAdmin and a new tab will appear to you.

Select the database name from the left side menu and all the database tables will get displayed. You can easily notice the default tables prefix.

There are many default tables which get created when you install WordPress for the first time. You have to change those tables.

change table prefix

Either you can change the prefix for each table individually or you can add the code for all.

Let show you the code you need to add for each table.

RENAME table `wp_comments` TO `stw1_comments`;

As I have taken ‘stw1_’ as the new table prefix so I am using it. You can change it to the one you use. This will be applied to all the tables.

Click on the “SQL” button showing on the main navigation menu.

Add these lines and click on the “Go” button.

RENAME table `wp_commentmeta` TO `stw1_commentmeta`;
RENAME table `wp_comments` TO `stw1_comments`;
RENAME table `wp_links` TO `stw1_links`;
RENAME table `wp_options` TO `stw1_options`;
RENAME table `wp_postmeta` TO `stw1_postmeta`;
RENAME table `wp_posts` TO `stw1__posts`;
RENAME table `wp_terms` TO `stw1_terms`;
RENAME table `wp_termmeta` TO `stw1_termmeta`;
RENAME table `wp_term_relationships` TO `stw1_term_relationships`;
RENAME table `wp_term_taxonomy` TO `stw1_term_taxonomy`;
RENAME table `wp_usermeta` TO `stw1_usermeta`;
RENAME table `wp_users` TO `stw1_users`;

Now when you come back to see all the tables, the default tables prefix has been changed to the new one you have just added.

NOTE: You will also find some other tables which were created by the plugins you have installed. You can apply the same rename SQL query for all of them.

#3. Update the Options Table

For the complete change, you have to search the options table which has the fields with the ‘wp_’ prefix.

change database table prefix

To do so, you have to run an SQL query. Click on the “SQL” option from the navigation menu and add this code.

SELECT * FROM `stw1_options` WHERE `option_name` LIKE ‘%wp_%’

Click on the “Go” button and you will see the number of fields having the ‘wp_’ prefix. It will also show the fields which have this default prefix in between.

But you have to edit only those which has ‘wp_’ in the starting. Click on the “edit” button in front of all the fields one by one.

change wordpress database

Update the table prefix for all.

#4. Update UserMeta Table

Just like the Options Table, you have run an SQL query for UserMeta table.

SELECT * FROM `stw1_usermeta` WHERE `meta_key` LIKE ‘%wp_%’

Follow the same process followed in the previous step. Find the fields having the default prefix and update it to the new table prefix you have been using so far.

Now try to open your website and it will be opening properly. You have successfully changed the database tables prefix of your WordPress website.

Now backup your website and the database again because from now onwards, you’re going to use the database with new tables prefix.

Use a Plugin to Change WordPress Database Tables Prefix

The coding may be scary for many. WordPress has always an alternative. You can use the Change Table Prefix plugin for that.

It doesn’t need any settings, just add your old or the default database tables prefix and change it to the new one.

It’s easy to use. And if you’re using All In One Security plugin for your website then you can change WordPress database tables prefix from this plugin only.

All In One Security plugin users wouldn’t need any extra plugin for that. Just go to WP Security>>Database Security and you will see a checkbox to generate a new database table prefix.

Use it and save the settings. And never forget to backup the database from you perform this action. If you face any problem, feel free to drop a comment.

You can also connect with us on Twitter, LinkedIn, and Facebook.

by Ravi Chahar

A WordPress Professional and the LinkedIn Influencer. A coder by passion and a blogger by choice. WordPress theme development is his forte. He is your WordPress guy who will teach you how to solve WordPress errors, WordPress security issues, design issues and what not.

Get Free Updates Into Your Inbox

Learn Everything Just Like I Did


Leave a Reply

Your email address will not be published. Required fields are marked *