WordPress

Table prefix

Table prefix is the string at the start of every WordPress database table name, wp_ by default. It is set in wp-config.php so several installs can share one database.

How it is measured

The setting is $table_prefix = 'wp_'; in wp-config.php, and code reads it as $wpdb->prefix. On Multisite, $wpdb->base_prefix is the network prefix and sites add numbers, such as wp_3_posts. Check what exists with SHOW TABLES LIKE '%posts', or wp db tables.

A mismatch between the prefix in wp-config.php and the tables in MySQL is a classic cause of the Error establishing a database connection or the install screen reappearing.

Worked example

A developer restores a production dump onto a staging server. The tables are named xk7_posts, xk7_options, and so on, but the staging wp-config.php has $table_prefix = 'wp_'. WordPress finds no tables and offers to install a new site.

Changing the prefix line to xk7_ loads the site. The developer then imports a second copy of the site into the same database with the prefix stg_ for a test.

How it differs

A table prefix is the first part of every table name, while wp-config is the file that stores the choice. wpdb reads it and builds table names for you. Changing the prefix is not only a rename of tables: option and user meta keys like wp_capabilities and wp_user_roles also contain it.

Common errors

Changing the value in wp-config.php on a live site without renaming tables. Renaming tables and not updating the usermeta keys, so admins lose their role. Hard-coding wp_posts in custom SQL. Expecting a custom prefix to stop attacks by itself. Using a prefix with a hyphen or a space.

In practice

Use $wpdb->posts and $wpdb->prefix in all custom code. If you want a nondefault prefix, set it at install time. If you must change it later, do it on a staging copy with a script that updates tables, options, and usermeta.

See also

wp-config.php, wpdb

Sources

Count this on a real site.

Watch my website