How to Change Your WordPress Database Prefix to Improve Security?

How-to-Change-WordPress-database-table-prefix-to-improve-security-themaverickspirit Pin It

If you’re running your WordPress website with default settings, then your website might end up being hacked.

And the reason might be a vulnerable plugin, vulnerable theme, database with default settings, weak server, etc.

Here, we’re going to focus on the WordPress database.

What is a WordPress Database? WordPress database is just like the central hub for any website. All the information, data, settings get stored in the WordPress database.

So if someone gets access to your database, your website is considered to be compromised.

Cloudways-wordpress-hosting-themaverickspirit

WordPress database can be hacked using one of the widely user hacking technique – SQL Injection in which spammers and hackers try to attack WordPress database by running automated codes.

But what makes the database vulnerable? The answer is – DEFAULT WORDPRESS DATABASE SETTINGS, i.e., default database prefix.

And in this post, I’m going to show you “how to change the WordPress default database prefix?”.

Let’s dive right in.


Preparations – Take a Backup

As we know how crucial is to mess with the database, so I will recommend you is to take a complete backup of your whole website.

And if you don’t take your website’s backup then you might end up making your website inaccessible.

You can check out the list of top WordPress Backup and Restore Plugins.


How To Change WordPress Database Prefix?

Below are the steps to change WordPress database prefix –

  1. Change Table Prefix in wp-config.php
  2. Change all Database Tables Name
  3. The Options Table
  4. UserMeta Table

1. Change Table Prefix in wp-config.php

The very first step you need to take is to change the table prefix in wp-config.php file.

In case, you don’t know how to edit config.php file then you can check this article – How to Edit wp-config.php File in WordPress?

How to change table prefix in wp-config.php?

1. Open your cPanel > File Manager > Go to public_html folder > [WordPress Folder] > wp-config.php

2. Right-click on the wp-config.php and edit the file.

3. Change the table prefix from wp_ to something more secure like “wp_wp123456_”, “tblpfx_” etc.

After the change the line would look like this:

$table_prefix  = 'wp_wp123456_';

Note : Make sure you only have numbers, letters, and underscores.


2. Change all Database Tables Name

Now that we’ve changed the prefix in the wp-config.php, next thing we need to do is to change the database table names.

The table prefix should be the same as we have used in the wp-config.php file and it can be changed from phpMyAdmin in the cPanel.

How to change a database table name?

WordPress generates 12 tables by default and changing their names manually one-by-one would be pain.

Therefore, we’re going to change them all at once by running a SQL query.

  1. Open your cPanel > Go to phpMyAdmin.
  2. Select the database from the left panel and you will see a list of all the database tables. You need to check if there are any other database tables apart from 12 default tables.
    If there are any tables other than the default ones, then you need to change their table prefix as well.
  3. Below is the list of 12 default tables –
    1. wp_commentmeta
    2. wp_comments
    3. wp_links
    4. wp_options
    5. wp_postmeta
    6. wp_posts
    7. wp_terms
    8. wp_termmeta
    9. wp_term_relationships
    10. wp_term_taxonomy
    11. wp_usermeta
    12. wp_users

3. You need to use the same database prefix you set in wp-config.php here. I am using ‘wp_wp123456_’ in the below code –

RENAME table `wp_commentmeta` TO `wp_wp123456_commentmeta`;
RENAME table `wp_comments` TO `wp_wp123456_comments`;
RENAME table `wp_links` TO `wp_wp123456_links`;
RENAME table `wp_options` TO `wp_wp123456_options`;
RENAME table `wp_postmeta` TO `wp_wp123456_postmeta`;
RENAME table `wp_posts` TO `wp_wp123456_posts`;
RENAME table `wp_terms` TO `wp_wp123456_terms`;
RENAME table `wp_termmeta` TO `wp_wp123456_termmeta`;
RENAME table `wp_term_relationships` TO `wp_wp123456_term_relationships`;
RENAME table `wp_term_taxonomy` TO `wp_wp123456_term_taxonomy`;
RENAME table `wp_usermeta` TO `wp_wp123456_usermeta`;
RENAME table `wp_users` TO `wp_wp123456_users`;

All you need to do now is to copy the above lines and paste them in the SQL field and run it.


3. The Options Table

The next thing you need to do is to check if there are any other fields present in the options table using “wp_” as a prefix.

Fields with “wp_” also need to get replaced with the new prefix, i.e., “wp_wp123456_”.

All you need to do is to run the below SQL query. You need to replace “wp_wp123456_” with “wp_” prefix.

SELECT * FROM `wp_wp123456_options` WHERE `option_name` LIKE '%wp_%'

The query will return a list of results and you need to change them one-by-one.  


4. UserMeta Table

The last thing, you need to do is to check if there are any fields in the usermeta table using “wp_” as a table prefix. You can do this by running this SQL query –

SELECT * FROM ‘wp_wp123456_usermeta’ WHERE ‘meta_key’ LIKE ‘%wp_%’

You need to make changes to all the tables that has wp_ to the new prefix.


Wrapping Up

Now you are ready to change your website’s database prefix and make your website more secure. We always recommend you to take a proper backup before making any changes to the database.

You can also check our other WordPress tutorial about How to Safely Add Code Snippets to Functions.Php File in WordPress?

Don’t forget to leave comments, alternative methods if there are any, and issues you’re facing while following the above process. We would be very happy to help you out.

Leave a Reply

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

3.6K views
Share via
Copy link