wp-config.php is the configuration file for WordPress. Naibabiji shares 15 advanced uses of wp-config.php here. Let's see if there are any techniques you need.
Understanding wp-config.php
wp-config.phpis the configuration file for WordPress. It does not exist by default and is automatically created during website installation. The one in the WordPress installation package you downloaded
wp-config-sample.phpRename to
wp-config.phpto manually create this file, then insert the following 15 code snippets to implement some advanced features. Note: It is recommended to use a dedicated code editor to modify
wp-config.phpsuch as Sublime Text, Notepad++, etc.
15 Advanced Uses of wp-config.php

When writing the code, we insert it above the database settings.
Enable WordPress Debug Mode
WordPress Debug Mode is used by developers to troubleshoot certain error messages on the website. It is disabled by default. When our website encounters issues and we cannot determine the cause, we can enable WordPress Debug Mode. The default wp-config.php includes this parameter; we only need to change its value to true and save.
/** 开启WordPress调试模式 */
define( 'WP_DEBUG', true );
If you need to debug CSS or JavaScript scripts on the frontend, you can also add the following code to enable it.
/** 开启CSS和JS调试模式 */
define( 'SCRIPT_DEBUG', true );
Change Database Prefix
By default,
WordPress Installationuses the default prefix wp_. If you are installing multiple WP websites within one database, it is recommended to change it to a different database prefix. It is not recommended to modify the database prefix for an already installed website. Also, the fresh WordPress installation interface allows you to choose the database prefix.
/** 自定义数据库前缀 */
$table_prefix = 'a81kJt_';
Change WordPress Site URL
When you change your website's domain name and cannot access the WP Admin Dashboard, besides modifying the website's URL from the database, you can also use the
wp-config.phpfile to change the URL address.
/** 设置wordpress网站地址 */
define('WP_SITEURL', 'https://blog.naibabiji.com');
define('WP_HOME', 'https://blog.naibabiji.com');Automatically Clean the Trash
If you have the habit of deleting posts to the Trash, you can use the following code to implement scheduled automatic emptying of the Trash to save website resources.
/** 自动清理回收站 */
define( 'EMPTY_TRASH_DAYS', 7 );
The number 7 above is the number of days, which you can modify yourself. If set to 0, the Trash function will be disabled, and when you want to delete a post, it will be permanently deleted directly.
Enable WordPress Multisite Feature
Naibabiji has written a separate article on the WP Multisite feature. For an in-depth understanding, refer to the URL below:
/** WP开启多站点模式 */
define( 'WP_ALLOW_MULTISITE', true );
Redirect Non-existent Subdomains and Folders
This code is used when the Multisite mode is enabled. When a user accesses a non-existent subdomain or subfolder, it will automatically redirect to the page you set, such as the homepage.
/** 多站点不存在子域和文件夹跳转 */
define( 'NOBLOGREDIRECT', 'https://blog.naibabiji.com' );
Manage Post Revisions
By default, WordPress retains post revisions, making it easy for you to compare new and old versions while writing articles or to recover from errors caused by mistaken operations. However, most people don't use this feature, and it occupies website database space. Previously, Naibabiji also separately shared
How to Disable or Reduce the Number of Saved Revisions。
/** 完全禁用修订版本 */
define( 'WP_POST_REVISIONS', false );
/** 保留5份修订版本 */
define( 'WP_POST_REVISIONS', 5 );
Use WordPress's Built-in Database Repair and Optimization Features
I believe 99% of WordPress users don't know that WP actually comes with a feature to automatically repair and optimize the database, because most people directly use database optimization plugins, such as the following:
If you don't want to use plugins, you can also try using WP's built-in database repair and optimization feature.
/** 打开WP内置的数据库修复优化 */
define( 'WP_ALLOW_REPAIR', true );
Add the above code to
wp-config.phpthen via
你网址/wp-admin/maint/repair.phpthis URL, you can perform database repair and optimization work. Note: This URL is accessible to anyone, so it's best to delete the code after you're done optimizing the database.
Disable Automatic Updates
The WordPress automatic update feature has its pros and cons. For webmasters who dislike automatic upgrades, you can use the following code to disable automatic updates, especially for friends using domestic servers. It is recommended to disable it to avoid website inaccessibility due to failed automatic updates. For detailed automatic update configuration files, you can refer to
the official documentationhere are the most commonly used ones listed.
/** 禁用WP自动更新功能 */
define( 'AUTOMATIC_UPDATER_DISABLED', true );
If you want to disable plugin and theme updates, you can refer to the following article:
Increase PHP Memory Limit
This feature is useful for many shared hosting users, but it may not always work. If the service provider restricts memory usage on the server, this code won't function either, so it's still
using a VPS for website buildingis more convenient.
/** 设置WordPress网站内存限制 */
define( 'WP_MEMORY_LIMIT', '96M' );
/** 设置WordPress管理后台内存限制 */
define( 'WP_MAX_MEMORY_LIMIT', '128M' );
Force SSL Login
This feature is actually unnecessary when we enable forced SSL redirect after installing an SSL certificate for the website. However, if you haven't enabled forced SSL redirect, you can use the following method to require SSL login for the website.
/** 强制WordPress通过ssl登录 */
define( 'FORCE_SSL_ADMIN', true );
Disable Plugin and Theme Editing/Updates
If your website will be used by clients or novice users, it is recommended to prohibit them from editing and updating themes and plugins to avoid breaking the site.
/** 禁止主题和插件的编辑功能 */
define( 'DISALLOW_FILE_EDIT', true );
/** 禁止主题和插件的编辑以及更新 */
define( 'DISALLOW_FILE_MODS', true );
Of course, this code only prevents editing and updating by clicking links in the admin dashboard. If FTP updates are used, this code is still powerless.
Delete Redundant Files Generated During Image Editing
This feature is probably not encountered by many users. What does it do? By default, after uploading an image to the WordPress Media Library, you can choose to edit the image, such as cropping its size, which will add another image. If you have thumbnails enabled, multiple images will be added. When you click the edit image function again, WP will generate multiple images again. However, if you use the following code, it will only generate the edited image file once, and multiple edits will overwrite the previous one.
/** 删除图像编辑多余文件 */
define( 'IMAGE_EDIT_OVERWRITE', true );
Disable Unfiltered HTML Code
If your website allows user registration and posting, it is recommended to enable the following feature to prevent users with posting permissions from publishing unfiltered HTML code.
/** 开启未过滤HTML */
define( 'DISALLOW_UNFILTERED_HTML', true );
The above are 15 advanced uses of wp-config.php. Do you need any of these features? Go try them out now.

Comments are closed
The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.