10 January 2024

15 Handy wp-config Snippets for Your WordPress Site

By usamerica_us

When it comes to managing your WordPress site, the admin dashboard provides a user-friendly interface that eliminates the need to deal with complex code. But did you know that there’s a separate file called “wp-config.php” that allows you to further customize your site?

Wp-config is a special file where your personalized hosting data, such as your database name and host, are stored when you install a self-hosted WordPress site. This file also allows you to add other configuration options that can enhance your site’s functionality, including enabling or disabling debugging, cache, multisite capabilities, SSL login, automatic updates, and much more.

Here are 15 valuable wp-config snippets that can help you configure your WordPress site:

15 Handy wp-config Snippets for Your WordPress Site

Configuring Your WordPress Installation

When you first download WordPress, you won’t find the wp-config.php file in the installation folder. Instead, you’ll come across a file called wp-config-sample.php, which you’ll need to duplicate and rename to wp-config.php. After that, you’ll need to provide some basic connection information (like the database name, username, password, hostname, and security keys) in this file.

If your web hosting provider uses the Softaculous auto-installer (which is common), this whole process will be automated for you. Just connect to your server via FTP, and you’ll find both the wp-config.php and wp-config-sample.php files in your root folder.

15 Handy wp-config Snippets for Your WordPress Site

Remember, the order of settings matters, so don’t rearrange them. When you’re editing the wp-config file, stick to using a code editor like Sublime Text, Atom, Notepad++, Visual Studio Code, or TextMate. Avoid word processors (like Microsoft Office, Google Docs, LibreOffice, etc.). They’ll mess up your file, so never use them for editing code files.

The settings you save into wp-config will take priority over the database, even if there’s a similar configuration in both places (like the home URL).

Where should you put the code snippets?

In this article, I’ve compiled 20 handy code snippets that you can use to easily customize your wp-config.php file.

15 Handy wp-config Snippets for Your WordPress Site

1. Enable WP debugging

If you want to debug your WordPress website, you can easily turn on the debugging mode in the wp-config file. In the code snippet below, you’ll find a line that is already present in the wp-config file. However, the value is currently set to “false”. To enable debugging, simply change the value to “true”.

There’s another code snippet that allows you to debug the CSS and JavaScript scripts on the front-end of your website. It’s important to note that these debuggers should only be used on development sites and not on live production websites.

# Enable PHP debugging

define( ‘WP_DEBUG’, true );

# Enable CSS and JavaScript debugging

define( ‘SCRIPT_DEBUG’, true );

2. Change database table prefix

The default table prefix for WordPress is “wp_”. However, if you want to enhance the security of your database, it’s recommended to choose a more complex table prefix.

In the default wp-config file, there’s a config option that allows you to change the value of the $table_prefix variable to a more secure one. This option is also available in the wp-config file.

However, you should only change the table prefix if you have a clean install or if you’re working on a development site. It’s risky to do so on a production site.

Here’s an example of how you can create a secure table prefix for your database tables in the wp-config file:

# Creates secure table prefix for database tables # Only numbers, letters, underscores $table_prefix = ‘a81kJt_’;

Another thing you can do is change the WordPress and home URLs. In the WordPress admin, you can set these URLs under the Settings > General menu. But you can also configure them in the wp-config file.

There are two advantages to defining the WP_SITEURL and WP_HOME constants in the wp-config file:

1. It gives you more control over the URLs.

2. It allows you to easily switch between different environments, like a development site and a production site.

  • It’s really important in case you can’t access your admin area for some reason.
  • It helps to reduce the number of database calls while your site is loading (as wp-config overrides the options saved in the database).
  • WP_SITEURL specifies the URL users can reach your site with, while WP_HOME defines the root of your WP install. If you installed WordPress into your root folder (this is the default option) they have the same value.

    # 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’);

    4. Empty your trash after a certain time

    # Emptying the Trash After 7 Days

    When it comes to managing my WordPress site, I’ve discovered a neat trick that helps keep things tidy. By adjusting a few lines of code in the `wp-config.php` file, I can automatically empty the trash after 7 days. This prevents clutter and frees up valuable space on my server.

    To make this happen, all I need to do is add the following line of code:

    “`php

    define( ‘EMPTY_TRASH_DAYS’, 7 );

    “`

    This simple adjustment ensures that any trashed content, like deleted posts or comments, will be automatically removed after 7 days. It’s a small change with a big impact on my site’s cleanliness.

    # Enabling WordPress Cache

    If you’re looking to improve the performance and speed of your WordPress site, enabling caching is a great way to achieve this. Fortunately, WordPress has a built-in caching feature that you can easily enable.

    To activate this feature, you can add the following line of code to your `wp-config.php` file:

    “`php

    define( ‘WP_CACHE’, true );

    “`

    Once added, this code snippet enables WordPress’ caching functionality, which helps store and retrieve data more efficiently. It’s a simple and effective way to boost your site’s speed and responsiveness.

    Many caching plugins, such as W3 Total Cache and WP Super Cache, automatically include this code in the `wp-config.php` file, so you might not even need to add it manually. Check your caching plugin’s settings to see if it’s already enabled.

    # Enabling WordPress Multisite

    If you’re interested in creating a network of interconnected WordPress sites, WordPress Multisite is a fantastic feature to enable. It allows you to manage multiple sites from a single installation of WordPress.

    To activate WordPress Multisite, you just need to add the following line of code to your `wp-config.php` file:

    “`php

    define( ‘WP_ALLOW_MULTISITE’, true );

    “`

    Once this code is in place, you’ll have access to the powerful multisite feature. You can then create multiple sites, each with its own unique content and settings, all within a single WordPress installation. It’s a convenient way to manage multiple websites efficiently.

    # Redirecting Non-Existing Subdomains and Subfolders

    When it comes to managing my WordPress site’s URLs, I’ve found a helpful trick that ensures a smooth user experience. By implementing a simple code modification, I can redirect non-existing subdomains and subfolders to a specified location.

    To accomplish this, you can add the following code to your `.htaccess` file:

    “`html

    RewriteEngine On

    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ /index.php [L]

    “`

    This code snippet checks if a requested subdomain or subfolder doesn’t exist as a physical file or directory. If it doesn’t, it redirects the user to the main homepage of the site. It’s a clever way to handle any mistyped or non-existing URLs and ensure a smooth browsing experience for your visitors.

    Sometimes, when you type in a part of a website that doesn’t actually exist, you end up on a page that says “404 not found.” But instead of showing that error page, you can send those visitors to another page on your website. For example, you could redirect them to your homepage. You can do this using the NOBLOGREDIRECT constant, which is a special code that tells your website where to send these visitors. Here’s how you can use it:

    “`

    define( ‘NOBLOGREDIRECT’, ‘http://www.yourwebsite.com’ );

    “`

    Another useful feature of WordPress is its built-in system for keeping track of different versions of your posts. Every time you make changes to a post, WordPress saves a new version of it. This is helpful because it allows you to go back and see previous versions if you need to. However, if you make a lot of changes, your database can start to get crowded with old versions of your posts. To help with this, you can control how many versions of each post are saved, or you can disable this feature altogether. To do this, you can use the WP_POST_REVISIONS constant. By adjusting this constant, you can choose how many revisions to keep, or you can turn off revisions completely. This can help keep your database clean and efficient.

    To make sure I have control over my WordPress website, I disable a few unnecessary features. One of them is post revisions. I completely turn them off using the following code in the wp-config file:

    # Completely disables post revisions

    define(‘WP_POST_REVISIONS’, false);

    Now, let’s move on to database optimization. Did you know that WordPress has a built-in feature for that? It’s pretty handy! To enable it, all you have to do is add this line to your wp-config file:

    # Turns on database optimization feature

    define(‘WP_ALLOW_REPAIR’, true);

    But remember, this feature is available to everyone, even non-logged in visitors. So, I suggest you only enable it when you want to use the optimization tool. And don’t forget to disable it afterwards.

    Lastly, I also like to disable automatic updates. That way, I have full control over when to update my website.

    That’s it! With these settings, you’ll have more control and peace of mind.

    By default, WordPress takes care of updates for minor releases and translation files without requiring any action from you.

    But if you want to have more control over updates, you can easily toggle this feature on and off.

    To turn off automatic updates for all types of updates, simply define the AUTOMATIC_UPDATER_DISABLED constant as true in your WordPress settings.

    Conversely, if you want to enable automatic updates, including major releases, set the WP_AUTO_UPDATE_CORE constant as true.

    To only allow automatic updates for minor releases, set the WP_AUTO_UPDATE_CORE constant as ‘minor’.

    Now, let’s move on to another important topic: increasing your PHP memory limit.

    Sometimes, you might need to increase the amount of memory that your hosting provider has allocated to your website. This is especially true if you see the error message “Allowed memory size of xxxxxx bytes exhausted.” To accomplish this, you can use the WP_MEMORY_LIMIT constant for your website and the WP_MAX_MEMORY_LIMIT constant for the admin area.

    It’s important to note that not all hosting providers allow you to manually increase the memory limit. If this is the case, you’ll need to contact them and ask them to do it for you.

    Here’s an example of how you can set the memory limit in your website’s code:

    “`php

    # Sets memory limit for the website

    define( ‘WP_MEMORY_LIMIT’, ’96M’ );

    # Sets memory limit for the admin area

    define( ‘WP_MAX_MEMORY_LIMIT’, ‘128M’ );

    “`

    Now, let’s move on to the next topic.

    12. Force SSL login.

    Note that the previously valid FORCE_SSL_LOGIN constant in WordPress 4.0 has been deprecated, and now you must always use FORCE_SSL_ADMIN.

    # Forces SSL login define(‘FORCE_SSL_ADMIN’, true);

    13. Turn off plugin and theme edits/updates

    When you’re an administrator in WordPress, you can edit plugin and theme files right from the admin area. To make your site more secure, disable the plugin and theme editors by using the DISALLOW_FILE_EDIT constant. This way, if your site gets hacked, the hacker won’t have access to your plugin and theme files.

    You can also prevent plugin and theme updates using DISALLOW_FILE_MODS. This will keep administrators from updating plugins and themes in the admin area.

    # Disables the plugin and theme editor define( ‘DISALLOW_FILE_EDIT’, true ); # Disables the plugin & theme editor PLUS the plugin & theme update define( ‘DISALL

    While I can’t publish unfiltered HTML, I know that editors and administrators have the power to do so in WordPress. It seems unfair, but there is a way to tighten security and prevent high-level users from publishing unfiltered HTML.

    All you need to do is add the following line of code to your wp-config file:

    # Disables unfiltered HTML for admins and editors define( ‘DISALLOW_UNFILTERED_HTML’, true );