How To Change WordPress Database Prefix To Improve Security

Most people do not bother changing the default WordPress database prefix (wp_) during its installation. This leaves your website vulnerable to mass attack of hackers and spammers. It’s easier for spammers and hackers to target a WordPress website with default database prefix.

So, don’t worry if you (deliberately ;)) forgot to change default database prefix while installing WordPress. You can take charge of WordPress security by following simple steps mentioned below.

Safety First

I recommend you to back up your website before proceeding and optionally, redirect your website users to a maintenance page.

Change Database Prefix In wp-config.php

Let’s start with wp-config.php file. Open it in your favorite code/text editor. You can find this file in root folder of your WordPress installation. Search following code in the file:


$table_prefix = 'wp_';

Replace wp_ in above code with the prefix of your choice. I am taking wp78u9_ as example here. So, after replacing the default prefix, the code becomes:


$table_prefix = 'wp78u9_';

Let’s save the file back.

Change Prefix In Database Tables

Now we will change prefix of database tables. You need to access the database of your website, most likely via phpMyAdmin. If you have access to cPanel, you can find link to phpMyAdmin there, as in the screenshot belowphpMyAdmin

Navigate to the database of your website from the list in left sidebar, if there are more than one databases.Choose Database phpMyAdmin

Navigate to SQL sectionphpMyAdmin SQL

You need to run SQL queries as mentioned below:


RENAME table `wp_commentmeta` TO `wp78u9_commentmeta`;
RENAME table `wp_comments` TO `wp78u9_comments`;
RENAME table `wp_links` TO `wp78u9_links`;
RENAME table `wp_options` TO `wp78u9_options`;
RENAME table `wp_postmeta` TO `wp78u9_postmeta`;
RENAME table `wp_posts` TO `wp78u9_posts`;
RENAME table `wp_terms` TO `wp78u9_terms`;
RENAME table `wp_term_relationships` TO `wp78u9_term_relationships`;
RENAME table `wp_term_taxonomy` TO `wp78u9_term_taxonomy`;
RENAME table `wp_usermeta` TO `wp78u9_usermeta`;
RENAME table `wp_users` TO `wp78u9_users`;

There may be other tables in your database depending on the plugins you have installed at your website. You have to include every table in the queries so that no table is left with default prefix.

Options Table

We need to replace wp_ prefix being used in option names too. Run following query in SQL section:


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

In the query results, replace wp_ with new prefix in the values in option_name column. You need to manually go through each row and update the values.

Usermeta Table

We need to update prefix in one more table. Run following query in SQL section:


SELECT * FROM `wp78u9_usermeta` WHERE meta_key LIKE 'wp_%'

In the query results, replace wp_ with new prefix in the values in meta_key column. Again, you need to manually go through each row and update the values.

Done

Grab your sandwich and cup of coffee. You are ready to test your website. Just visit couple of pages at front-end and login to your admin panel to make sure everything is fine. Once done, you can keep backup of your newly updated database.

If anything goes wrong or you want to share your experience, post a comment below.

Happy Blogging 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s