10 WordPress wp-config codes to improve your website

The WordPress admin makes it easy to manage settings without touching a line of code. These basic configuration settings are stored in the wp-options table within the database. But, WordPress also has a separate configuration file, called wp-config.php, which can be used for other customizations.

Wp-config is the file where your custom hosting details (database name, database host, etc.) are saved when you install a self-hosted WordPress site. You can also add other configuration options to this file, with which you can enable or disable features such as debugging, caching, multisite, SSL login, automatic updates, and many others.

Locate and edit wp-config

When you download WordPress, the wp-config.php file is not yet present inside the installation folder. However, there is a file called wp-config-sample.php that you need to copy and rename wp-config.php. Then you need to add your basic connection details (database name, database username, database password, hostname, security keys) to this file.

If your hosting provider uses the Softaculous auto-installer (most do), this process is automated for you and you will find wp-config.php a
wp-config-sample.php file and a file in your root folder when you connect to your server via FTP.

Note that the configuration order is important, so donā€™t rearrange them. When editing the wp-config file, always use a code editor such as Sublime Text, Atom, Notepad++, Visual Studio Code, or TextMate. Word processors (Microsoft Office, Google Docs, LibreOffice, etc.) will damage your file, never use them to edit code files.

Settings saved in wp-config override the database, in case the same type of configuration is present in both (eg home URL)

Where to put the code snippets

In this article, you can find 20 code snippets that you can use to customize your wp-config.php file.

Most of these configuration options do not exist in wp-config by default. If you want to use them, you need to add them under the initial General menu. However, you can also configure these URLs in the wp-config file.

Defining the WP_SITEURL and WP_HOME constants in the wp-config file has two benefits:

  1. It can save lives if you are unable to access your admin area for any reason.
  2. can reduce the number of calls to the database while your site is loading (since wp-config overrides the options saved in the database)

WP_SITEURLĀ specifies the URL with which users can access your site, whileĀ WP_HOMEĀ defines the root of your WP installation. If you installed WordPress in your root folder (this is the default) they take the same value.

1
2
3
4
5
# Specifies site URL
define('WP_SITEURL', 'http://www.yourwebsite.com');Ā 
# Specifies home URL (the root of the WP install)
define('WP_HOME', 'http://www.yourwebsite.com/wordpress');

Empty the trash after a certain time

You can have WordPress automatically empty your trash after a certain number of dates. The smallest value of this constant is 0, in this case it disables the trash can function.

1
2
# Empties trash after 7 days
define( 'EMPTY_TRASH_DAYS', 7 );

Enable WordPress Multisite

By adding the WP_ALLOW_MULTISITE constant to your wp-config file, you can enable the WordPress multisite feature which allows you to create a network of WP sites.

1
2
# Turns on WordPress Multisite
define( 'WP_ALLOW_MULTISITE', true );

Redirect non-existing subdomains and subfolders

Sometimes visitors type a non-existent subdomain or subfolder in the URL bar. You can redirect these users to another page of your domain, for example to the home page with the help of theĀ NOBLOGREDIRECTconstante

1
2
# Redirects non-existing subdomains and subfolders to homepage
define( 'NOBLOGREDIRECT', 'http://www.yourwebsite.com

Manage Post Revisions

WordPress has a built-in version control system, which means it saves all subsequent revisions you create. A frequently edited post can have 25-30 revisions which can take up a lot of database space after a while.

With theĀ WP_POST_REVISIONSĀ constant, you can maximize the number of subsequent checks or disable the feature altogether.

1
2
3
4
5
# Completely disables post revision
define( 'WP_POST_REVISIONS', false );Ā 
# Allows maximum 5 post revisions
define( 'WP_POST_REVISIONS', 5 );

Enable built-in database optimization

WordPress has a built-in database optimization feature that you can activate by adding the following line to the wp-config.

I wrote in detail about how this tool works in this article. The most important thing to note is that the database optimization screen is available to anyone (even non-logged-in visitors). Enable the feature only for the length of time you want to run the optimization tool, then donā€™t forget to disable it.

1
2
# Turns on database optimization feature
define( 'WP_ALLOW_REPAIR', true );

Turn off automatic updates

WordPress runs automatic background updates by default for minor versions and translation files.

You can turn this feature on and off by setting the values of the constantsĀ AUTOMATIC_UPDATER_DISABLEDĀ (for all updates) andĀ WP_AUTO_UPDATE_COREĀ (for major updates) according to the following rules:

1
2
3
4
5
6
7
8
9
10
11
# Disables all automatic updates
define( 'AUTOMATIC_UPDATER_DISABLED', true );Ā 
# Disables all core updates
define( 'WP_AUTO_UPDATE_CORE', false );Ā 
# Enables all core updates, including minor and major releases
define( 'WP_AUTO_UPDATE_CORE', true );Ā 
# Enables core updates only for minor releases (default)
define( 'WP_AUTO_UPDATE_CORE', 'minor' );

Increase PHP memory limit

Sometimes you may want to increase the PHP memory limit that your hosting provider has allocated to your site, especially if you get the dreaded ā€œAllowed memory size of xxxxxx bytes exhaustedā€ message. To do this, use Ā WP_MEMORY_LIMITĀ for the website andĀ WP_MAX_MEMORY_LIMITĀ for the administration area.

Note that some hosts do not allow you to manually increase the memory limit; in this case, contact them and ask them to do it for you.

1
2
3
4
5
# Sets memory limit for the website
define( 'WP_MEMORY_LIMIT', '96M' );Ā 
# Sets memory limit for the admin area
define( 'WP_MAX_MEMORY_LIMIT', '128M' );

Disable unfiltered HTML

Although low-level users (subscribers, contributors, authors) cannot post unfiltered HTML to WordPress, editors and admins can.

By adding the following line of code to your wp-config file, you can increase security by preventing high-level users from posting unfiltered HTML.

1
2
# Disables unfiltered HTML for admins and editors
define( 'DISALLOW_UNFILTERED_HTML', true );
Scroll to Top