Because every piece of information on your WordPress site is kept in the database, it’s a hacker’s preferred target. Automated SQL injection codes are used by spammers and hackers. Unfortunately, while installing WordPress, many individuals neglect to update the database prefix. By targeting the default prefix wp_, hackers can plan a mass attack more easily. The smartest approach to safeguard your database is to change the database prefix, which is really simple to perform on a new site. However, changing the WordPress database prefix effectively for your existing site without entirely messing it up requires a few steps.
Preparation
Before you do anything else in this article, we recommend that you back up your WordPress database. Daily backups of your site are essential, and we recommend using the BackupBuddy plugin to accomplish it. The next step is to send your visitors to a maintenance page that is only temporary.
Change Table Prefix in wp-config.php
Open your wp-config.php file which is located in your WordPress root directory. Change the wp_ table prefix to anything different, like this. wp a123456_
As a result, the line would be as follows:
1
|
$table_prefix = 'wp_a123456_' ; |
It’s important to note that you can only modify it to numbers, characters, and underscores.
Change the names of all database tables
You need to access your database (most likely through phpMyAdmin), and then change the table names to the one we specified in wp-config.php file. If you’re using cPanel for WordPress hosting, the phpMyAdmin link can be found in your cPanel. Take a look at the illustration below:
There are 11 default WordPress tables, so manually altering them would be a headache.
As a result, we have a SQL query that you may use to make things go faster.
1
2
3
4
5
6
7
8
9
10
11
12
|
RENAME table `wp_commentmeta` TO `wp_a123456_commentmeta`; RENAME table `wp_comments` TO `wp_a123456_comments`; RENAME table `wp_links` TO `wp_a123456_links`; RENAME table `wp_options` TO `wp_a123456_options`; RENAME table `wp_postmeta` TO `wp_a123456_postmeta`; RENAME table `wp_posts` TO `wp_a123456_posts`; RENAME table `wp_terms` TO `wp_a123456_terms`; RENAME table `wp_termmeta` TO `wp_a123456_termmeta`; RENAME table `wp_term_relationships` TO `wp_a123456_term_relationships`; RENAME table `wp_term_taxonomy` TO `wp_a123456_term_taxonomy`; RENAME table `wp_usermeta` TO `wp_a123456_usermeta`; RENAME table `wp_users` TO `wp_a123456_users`; |
Other plugins that may add their own tables to the WordPress database may require additional lines. The concept is that you alter the prefix of all tables to the one you wish.
The Table of Options
We’ll need to look through the options table for any other fields that use the wp_ prefix and replace them. Use the following query to speed up the process:
1
|
SELECT * FROM `wp_a123456_options` WHERE `option_name` LIKE '%wp_%' |
You’ll get a lot of results, and you’ll have to go through them one by one to adjust the lines.
Table of UserMeta
Next, we must scan the usermeta for all fields that include wp_ as a prefix so that we may replace it. For that, use the following SQL query:
1
|
SELECT * FROM `wp_a123456_usermeta` WHERE `meta_key` LIKE '%wp_%' |
The number of entries you have depends on how many plugins you have installed and other factors. Simply replace the wp_ prefix with the new one.
Done with the backup
You’re now ready to put the site to the test. Everything should be operating great if you followed the steps above. You should now create a new backup of your database simply to be cautious.