Family Best Time >> Electronics

How to change the WordPress database prefix to improve security

The WordPress database is like a brain for your entire WordPress site because every piece of information is stored there, making it a favorite target for hackers. Spammers and hackers run automated code for SQL injections. Well, unfortunately many people forget to change the database prefix while installing WordPress. This makes it easier for hackers to plan a massive attack by targeting the default wp_ prefix. . The smartest way to protect your database is by changing the database prefix which is really easy to do on a site you are setting up. But it takes a few steps to properly change the WordPress database prefix for your established site without completely messing up.

Video Walkthrough

Subscribe to WPBeginner

If you don't like the video or need more instructions, continue reading.

Preparation

We recommend that you back up your WordPress database before making any suggestions in this tutorial. It's important to keep daily backups of your site, we recommend the BackupBuddy plugin to do that. The next thing we recommend is that you redirect your visitors to a temporary maintenance page.

Change table prefix in wp-config.php

Open your wp-config.php file which is located in your WordPress root directory. Change the table prefix line from wp_ to something else like this wp_a123456_

So the line would look like this:

$ table_prefix = 'wp_a123456_';

Note:You can only change it to numbers, letters and underscores.

Change all database tables Name

You need to access your database (probably via phpMyAdmin), and then change the table names to the one we specified in the wp-config.php file. If you are using cPanel WordPress hosting, you can find the phpMyAdmin link in your cPanel. Look at the image below:

How to change the WordPress database prefix to improve security

There are a total of 11 default WordPress tables, so changing them manually would be a pain.

How to change the WordPress database prefix to improve security

That's why to make things faster, we have an SQL query that you can use.

 RENAME la tabla 'wp_commentmeta' TO 'wp_a123456_commentmeta'; RENAME la tabla 'wp_comments' TO 'wp_a123456_comments'; RENAME la tabla 'wp_links' A 'wp_a123456_links'; RENAME la tabla 'wp_options' TO 'wp_a123456_options'; RENAME la tabla 'wp_postmeta' TO 'wp_a123456_postmeta'; RENAME la tabla 'wp_posts' A 'wp_a123456_posts'; RENAME la tabla 'wp_terms' A 'wp_a123456_terms'; RENAME la tabla 'wp_termmeta' A 'wp_a123456_termmeta'; RENAME la tabla 'wp_term_relationships' TO 'wp_a123456_term_relationships'; RENAME la tabla 'wp_term_taxonomy' TO 'wp_a123456_term_taxonomy'; RENAME la tabla 'wp_usermeta' A 'wp_a123456_usermeta'; RENAME la tabla 'wp_users' TO 'wp_a123456_users'; 

You may need to add lines for other plugins that can add their own tables in the WordPress database. The idea is that you change the prefix of all the tables to the one you want.

The options table

We need to search the options table for any other fields that use wp_ as a prefix, so we can replace them. To make the process easier, use this query:

SELECT * FROM 'wp_a123456_options' WHERE 'option_name' LIKE '% wp_%'

This will return many results, and you have to go one by one to change these lines.

UserMeta table

Next, we need to search the usermeta for all fields that use wp_ as a prefix, so we can replace it. Use this SQL query for that:

SELECCIONAR * DE 'wp_a123456_usermeta' DONDE 'meta_key' ME GUSTA '% wp_%'

The number of entries may vary depending on the number of plugins you are using and others. Just change everything with wp_ to the new prefix.

Back up and done

Now you are ready to test the site. If you followed the steps above, then everything should be working fine. Now, you need to make a new backup of your database to be safe.