16 Best wp-config Snippets for WordPress

The wp-config file is one of the most important files in your WordPress install. You can use it to customize your site to suit your needs. With wp-config, you can make your WordPress site more secure, enforce best practices, reduce page load time, optimize your database, and perform many other customizations. You can find the wp-config.php file in the root folder of your WordPress install. The root folder is usually the public_html directory on live servers.

In this article, we’ll take a look at the best wp-config snippets you can use on any WordPress site. You need to add most of these snippets to the beginning of the file, however some of the snippets go to the end of the file. We have specified the location you need to place each snippet, respectively.

1. Change Table Prefixes

Location: already exists in the default wp-config, you only need to find and overwrite it

To harden WordPress security, you can change the prefixes of your database tables. By default, WordPress uses the wp_ prefix. However, you can modify that to a more secure prefix that contains more characters, therefore harder to find out. You simply need to overwrite the value of the $table_prefix variable in your wp-config file.

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give 
 * each a unique prefix. Only numbers, letters, and underscores 
 * please!
 */
$table_prefix = 'wp_';

2. Enable the WordPress Debugger

Location: already exists in the default wp-config, you only need to find and overwrite it

WordPress has a built-in debugger you can use to get notifications about bugs and different issues on your site. The WordPress Debugger is disabled by default, as you shouldn’t use it on a production site—unless you want your website visitors to see the notifications about the errors. However, you can easily enable it by changing the value of the WP_DEBUG constant from false to true in your wp-config file.

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the Codex.
 *
 * @link https://codex.wordpress.org/Debugging_in_WordPress
 */
define( 'WP_DEBUG', true );

3. Empty Trash Automatically

Location: Beginning of the wp-config file, above the MySQL settings

With the help of wp-config, you can configure how frequently WordPress empties your trash. You can specify the number of days after which your post, pages, attachments you moved to the Trash folder will be emptied. If you use 0 as value the Trash feature will be completely disabled on your site. You need to add the following line of code to the beginning of your wp-config.php file:

define( 'EMPTY_TRASH_DAYS', 30 ); 
// 30 days

4. Specify an Auto-Save Interval

Location: Beginning of the wp-config file, above the MySQL settings

WordPress auto-saves your posts and pages in every 60 seconds by default. You can easily increase or decrease this value in your wp-config file. You need to specify the interval in seconds and add the following line to the beginning of the file:

define( 'AUTOSAVE_INTERVAL', 180 ); 
// 180 seconds

5. Limit the Number of Post Revisions

Location: Beginning of the wp-config file, above the MySQL settings

WordPress automatically saves post revisions to the database as many times as you save the post. Besides, the latest autosave is also stored in the database. As a result, a single post may have more than  20 or 30 versions in the wp-posts database table. With time, this can lead to a lot of redundancies in the database. However, you can limit the number of revisions or completely switch it off:

define( 'WP_POST_REVISIONS', 5 );
// saves 5 post revisions

define( 'WP_POST_REVISIONS', false );
// disables post revisions

6. Change Your Site and Blog Address

Location: Beginning of the wp-config file, above the MySQL settings

You can specify the URL of your website and homepage in your wp-config file. If your WordPress install is saved into your root folder these two URLs will be the same. Although you can configure your site and home address in the WordPress dashboard, this feature can be a godsend if you can’t access your admin area for some reason.

define( 'WP_SITEURL', 'http://example.com/wordpress' );
// the folder in which your core WordPress files reside

define( 'WP_HOME', 'http://example.com' );
// the URL people type into the browser's URL bar to reach your site

7. Enable WordPress Multisite

Location: Middle of the wp-config file, above the /* That’s all, stop editing! Happy blogging. */ line

WordPress Multisite is an awesome feature that can come handy if you need to manage several sites at the same time. It allows you to create a network of sites and manage them from the same WordPress dashboard. You can turn on the feature by adding the following line to your wp-config file:

define( 'WP_ALLOW_MULTISITE', true );

8. Turn On WordPress Cache

Location: Beginning of the wp-config file, above the MySQL settings

Although there are several ways to enable caching on your WordPress site, if you don’t need any custom caching feature and are happy with the basic WordPress cache you can easily enable it in your wp-config file:

define( 'WP_CACHE', true );

9. Require SSL Login

Location: Beginning of the wp-config file, above the MySQL settings

To make your WordPress site more secure, you can force users to use SSL whenever they access your admin area. You can only use this feature if you have an SSL certificate installed on your site. The following line doesn’t only redirect admins to the secure URL (https://) but all other users as well:

define( 'FORCE_SSL_ADMIN', true );

10. Redirect Non-Existent Subdomains

Location: Beginning of the wp-config file, above the MySQL settings

It happens several times that a user tries to access an invalid subdomain or subdirectory on your domain. A subdomain uses the blog.example.com format, while a subdirectory follows the www.example.com/blog format. With wp-config, you can redirect all these invalid requests to the homepage (or any other URL on your site):

define( 'NOBLOGREDIRECT', 'https://example.com' );

11. Disable the Theme and Plugin Editors

Location: Beginning of the wp-config file, above the MySQL settings

By default, you can edit theme and plugin files right from your WordPress admin. While this can be a great feature, it can also be dangerous when your site gets compromised and a hacker gets access to your admin area. Therefore, it can be a good idea to disable your theme and plugin editors. You can do that by adding the following line to your wp-config file:

define( 'DISALLOW_FILE_EDIT', true );

12. Disable Theme and Plugin Installation and Update

Location: Beginning of the wp-config file, above the MySQL settings

If you want even more security on your site you can also restrict theme and plugin installations and updates through your wp-config file. This means you won’t be able to update or install WordPress plugins and themes from your dashboard. You will need to use an FTP/SFTP client and perform the installation or update manually.

define( 'DISALLOW_FILE_MODS', true );

13. Disable Automatic WordPress Updates

Location: Beginning of the wp-config file, above the MySQL settings

WordPress performs automatic updates in the background for minor releases and translation files. Although WordPress never runs core updates by default, some site owners still consider the automatic update feature as a security vulnerability. If you want you can easily disable all automatic updates by adding the following line to your wp-config file:

define( 'AUTOMATIC_UPDATER_DISABLED', true );

14. Increase Memory Allocated to PHP

Location: Beginning of the wp-config file, above the MySQL settings

You can also specify the maximum amount of memory that WordPress allocates to PHP. The default memory limit is 40 MByte for single sites and 64 MByte for multisite. While you can increase these values in your wp-config file, you can’t decrease it, as WordPress needs a minimum amount of memory to properly run everything:

define( 'WP_MEMORY_LIMIT', '96M' );
// increases WP memory limit to 96 Mbyte

 15. Clean Up Image Edits

Location: Beginning of the wp-config file, above the MySQL settings

Images are stored in the wp-content folder on your server. By default, whenever you edit an image and save it, WordPress creates a new set of images of different dimensions. In most cases, it makes no sense to store all those old images on the server, as they take up a lot of space. The following wp-config snippet cleans up old image edits and keeps only the latest set:

define( 'IMAGE_EDIT_OVERWRITE', true );

16. Optimize Your Database Automatically

Location: End of the wp-config file, below the require_once(ABSPATH . ‘wp-settings.php’); line

WordPress has an awesome automatic database optimization feature you can switch on through your wp-config file. The built-in database optimizer can solve several issues you may encounter on your WordPress site, such as the much dreaded “Error establishing a database connection” error.

After switching on the feature, you need to visit the http://www.yoursite.com/wp-admin/maint/repair.php URL and run the script. Note that you need to add this snippet to the very end of your wp-config file.

 define( 'WP_ALLOW_REPAIR', true );

Next Steps

By adding these snippets to your wp-config file, you can quickly perform customizations that would require a plugin otherwise. If you want to know more about how to configure WordPress we also have an article of the best .htaccess snippets with which you can customize your server settings. Besides, if you are interested in how the WordPress database works check out our tutorial about how to change the database prefix.

Finally, to stay tuned with the latest WordPress development news, tips, and tricks don’t forget to follow Developer Drive on Facebook and Twitter.

Home CSS Deals DesignBombs HTML HTML5 JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress