
WordPress 配置
WP-config is one of the most powerful files on your WordPress site, and it plays an important role in WordPress 的幕后工作原理. There are some very useful WordPress configuration tricks that most beginners don’t know about. In this article, we will share some of the most useful WordPress configuration tricks that will help you troubleshoot, optimize, and secure your WordPress site.
WP-config是WordPress网站上功能最强大的文件之一,它在WordPress 在幕后工作起着重要作用。 大多数初学者都不知道有一些非常有用的WordPress配置技巧。 在本文中,我们将分享一些最有用的WordPress配置技巧,可帮助您解决,优化和保护WordPress网站。

如何使用这些WordPress配置技巧? (How to Use these WordPress Configuration Tricks?)
WordPress comes with a powerful configuration file called wp-config.php. It is located in the root folder of every WordPress site and contains important configuration settings.
WordPress随附一个功能强大的配置文件wp-config.php 。 它位于每个WordPress网站的根文件夹中,并且包含重要的配置设置。
To learn more, see our guide on 如何在 WordPress 中编辑 wp-config.php 文件.
要了解更多信息,请参阅有关如何在 WordPress 中编辑 wp-config.php 文件指南。
All the 最好的 WordPress 托管 companies come with 1-click WordPress安装 which means you would never need to edit the wp-config.php file during the installation. This is the main reason why many users are not familiar with the power of this file.
所有最好的 WordPress 托管公司都附带一键式WordPress安装 ,这意味着您在安装过程中将不需要编辑wp-config.php文件。 这是许多用户不熟悉此文件功能的主要原因。
您可以使用 wp-config 文件来排除故障、优化和保护您的 WordPress 网站。
您可以使用 wp-config 文件来排除故障、优化和保护您的 WordPress 网站。
The wp-config.php file is a powerful tool, and a tiny mistake in the code can make your website inaccessible. You should only edit this file when necessary and always create a complete WordPress 备份 before making any changes,
wp-config.php文件是一个功能强大的工具,并且代码中的微小错误会使您的网站无法访问。 您只应在必要时编辑此文件,并在进行任何更改之前始终创建完整的WordPress 备份 ,
话虽这么说,让我们看一下可以在网站上使用的一些方便的 WordPress 配置技巧。
话虽这么说,让我们看一下可以在网站上使用的一些方便的 WordPress 配置技巧。
1.基本的WordPress配置设置 (1. The Basic WordPress Configuration Settings)
默认情况下,您只需要在 WordPress 安装过程中填写数据库设置即可。如果您没有 wp-config.php 文件,系统会要求您填写数据库信息来创建一个文件。
默认情况下,您只需要在WordPress安装过程中填写数据库设置即可。如果您没有 wp-config.php 文件,系统会要求您填写数据库信息来创建一个文件。

WordPress 将尝试通过生成 wp-config.php 文件自动保存这些设置。但是,如果失败,则您将需要手动添加它们。
WordPress 将尝试通过生成 wp-config.php 文件自动保存这些设置。但是,如果失败,您将需要手动添加它们。
为此,您需要使用 FTP 客户端连接您的网站。连接后,您需要将 wp-config-sample.php 文件重命名为 wp-config.php。
为此,您需要使用 FTP 客户端连接到您的网站。连接后,您需要将 wp-config-sample.php 文件重命名为 wp-config.php。

之后,您可以继续编辑新创建的 wp-config.php 文件。您需要通过更改以下行来添加数据库信息:
之后,您可以继续编辑新创建的 wp-config.php 文件。您需要通过更改以下行来添加数据库信息:
define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');
不要忘记保存更改并将文件上传回服务器。
不要忘记保存更改并将文件上传回服务器。
2.在WordPress中添加安全密钥 (2. Adding Security Keys in WordPress)
默认 WordPress 安装会自动将安全密钥添加到您的配置文件中。这些安全密钥用于为您的 WordPress 登录和 cookie 身份验证添加额外的安全层。
默认 WordPress 安装会自动将安全密钥添加到您的配置文件中。这些安全密钥用于为您的 WordPress 登录和 cookie 身份验证添加额外的安全层。
You can always 重新生成安全密钥 if you feel someone may be accessing your website without proper authentication. Changing security keys will log out all logged in users.
如果您发现有人未经适当的身份验证可能正在访问您的网站,则可以始终重新生成安全密钥 。 更改安全密钥将注销所有已登录的用户。
define( 'AUTH_KEY', 'put your unique phrase here' );
define( 'SECURE_AUTH_KEY', 'put your unique phrase here' );
define( 'LOGGED_IN_KEY', 'put your unique phrase here' );
define( 'NONCE_KEY', 'put your unique phrase here' );
define( 'AUTH_SALT', 'put your unique phrase here' );
define( 'SECURE_AUTH_SALT', 'put your unique phrase here' );
define( 'LOGGED_IN_SALT', 'put your unique phrase here' );
define( 'NONCE_SALT', 'put your unique phrase here' );
For more information, see our article on WordPress 安全密钥 and how to use them.
有关更多信息,请参阅关于WordPress 安全密钥及其使用方法的文章。
3.更改WordPress表前缀 (3. Change WordPress Table Prefix)
A typical default WordPress installation adds a WP_ prefix to all WordPress 数据库 table names. Some WordPress security experts believe that changing the table prefix can make your WordPress database more secure.
典型的默认WordPress安装会将WP_前缀添加到所有WordPress 数据库表名称中。 一些WordPress安全专家认为,更改表前缀可以使WordPress数据库更安全。
为此,您需要更改 WordPress 配置中的以下行。
为此,您需要更改 WordPress 配置中的以下行。
$table_prefix = 'wp_';
If you are doing this for an existing website, then you will also need to change the table prefix in your WordPress database. To do that, see our article on how to 更改 WordPress 数据库前缀.
如果对现有网站执行此操作,则还需要更改WordPress数据库中的表前缀。 为此,请参阅有关如何有关更改 WordPress 数据库前缀的文章 。
4.打开WordPress中的调试 (4. Turn on Debugging in WordPress)
WordPress 附带了一个简洁的调试功能,允许您在调试模式下查看或隐藏 WordPress 错误。要打开此功能,您需要在 WordPress 配置文件中添加此规则。
WordPress 具有简洁的调试功能,允许您在调试模式下查看或隐藏 WordPress 错误。要启用此功能,您需要在 WordPress 配置文件中添加此规则。
define( 'WP_DEBUG', true );
您还可以打开调试,同时隐藏网站上的错误并将其保存在日志文件中。为此,请将以下行添加到您的配置设置中。
您还可以打开调试,同时隐藏网站上的错误并将其保存在日志文件中。为此,请将以下行添加到您的配置设置中。
define( 'WP_DEBUG', true );
define( 'WP_DEBUG_LOG', true );
define( 'WP_DEBUG_DISPLAY', false );
这将在您网站的 wp-content 文件夹中创建一个 debug.log 文件,并将所有调试错误和通知存储在日志文件中。
这将在您网站的 wp-content 文件夹中创建一个 debug.log 文件,并将所有调试错误和通知存储在日志文件中。
5.更改您的网站或WordPress地址 (5. Changing Your Site or WordPress Address)
Normally, you can set your WordPress and Site URLs from 设置 » 常规 page. However, you may not be able to do that if you don’t have access to your WordPress site, seeing 重定向错误, or have just moved your site.
通常,您可以从“设置”»“常规”页面设置 WordPress和站点URL。 但是,如果您无权访问WordPress网站,看到重定向错误或刚刚移动了网站,则可能无法执行此操作。
在这种情况下,您可以通过 wp-config.php 文件添加以下行来更改您的网站和 WordPress URL:
在这种情况下,您可以通过 wp-config.php 文件添加以下行来更改站点和 WordPress URL:
define('WP_HOME', 'http://www.example.com');
define('WP_SITEURL', 'http://www.example.com');
不要忘记将 example.com 替换为您自己的域名。
不要忘记将 example.com 替换为您自己的域名。
6.覆盖文件权限 (6. Override File Permissions)
如果您的主机对所有用户文件具有限制性权限,则 WordPress 允许您覆盖文件权限。大多数用户不需要这个,但它是为那些需要它的人而存在的。
如果您的主机对所有用户文件具有限制性权限,WordPress 允许您覆盖文件权限。大多数用户不需要它,但对于那些需要它的人来说,它就在那里。
define('FS_CHMOD_FILE', 0644);
define('FS_CHMOD_DIR', 0755);
To learn more about file permissions, see our article on how-to 修复 WordPress 中的文件和文件夹权限错误.
要了解有关文件权限的更多信息,请参阅有关如何有关修复 WordPress 中的文件和文件夹权限错误的文章 。
7.更改修订版设置 (7. Changing Post Revision Settings)
WordPress has a very useful a 修订后 feature which allows you to undo changes to your 帖子和页面 by reverting back to a previous version or an autosave.
WordPress具有非常有用的后期修改功能,该功能可让您通过恢复为先前版本或自动保存来撤消对帖子和页面的更改。
您可以通过配置文件禁用或更改修订后设置。以下是您可以使用的不同修订后设置。
您可以通过配置文件禁用或更改修订设置。您可以使用以下不同的修订设置。
您可以通过添加以下行来更改 WordPress 将自动保存存储为修订版本的频率:
您可以通过添加以下行来更改 WordPress 将自动保存存储为修订的频率:
define('AUTOSAVE_INTERVAL', 120); // in seconds
您网站上的某些文章可能有数十次后期修订,具体取决于撰写它们所需的时间。如果您认为该功能让您烦恼,那么您可以限制每个帖子的修订数量。
您网站上的某些文章可能有数十次修订,具体取决于它们的撰写时间。如果您觉得此功能很烦人,您可以限制每个帖子的修订数量。
define('WP_POST_REVISIONS', 10);
如果由于某种原因,您想完全禁用后期修订功能(完全不推荐),那么您可以使用以下代码来禁用后期修订。
如果由于某种原因您想要完全禁用发布修订功能(根本不建议这样做),您可以使用以下代码来禁用发布修订。
define( 'WP_POST_REVISIONS', false );
8.更改WordPress废纸Settings设置 (8. Changing WordPress Trash Settings)
WordPress 附带了一个称为“垃圾箱”的回收站功能。当用户将帖子发送到垃圾箱时,该帖子在接下来的 30 天内仍会作为垃圾箱存储在您的网站上。在此之后,WordPress 会自动永久删除它们。
WordPress 附带了一个称为“垃圾箱”的回收站功能。当用户将帖子发送到垃圾箱时,该帖子将作为垃圾箱在您网站上保留 30 天。之后,WordPress 会自动将其永久删除。
您可以通过更改要保留垃圾的天数来更改此行为。
您可以通过更改要保留垃圾的天数来更改此行为。
define( 'EMPTY_TRASH_DAYS', 15 ); // 15 days
如果您不喜欢此功能,可以通过添加以下功能来禁用它:
如果您不喜欢此功能,可以通过添加以下内容来禁用它:
define('EMPTY_TRASH_DAYS', 0 );
注意:使用零意味着您的帖子将被永久删除。当您单击“永久删除”时,WordPress 不会要求确认。任何意外点击都可能让您付出代价……
注意:使用零意味着您的帖子将被永久删除。当您单击永久删除时,WordPress 不会要求您确认。任何意外点击都可能让您付出代价……
To learn more, see our article on how to 限制或禁用 WordPress 中的自动清空垃圾箱功能.
要了解更多信息,请参阅有关如何有关限制或禁用 WordPress 中的自动清空垃圾箱功能的文章 。
9.将FTP / SSH常量添加到WordPress配置 (9. Adding FTP/SSH Constants to WordPress Configuration)
默认情况下,WordPress 允许您从管理仪表板升级 WordPress 核心、主题和插件。有些主机在您每次尝试升级或安装新插件时都需要 FTP 或 SSH 连接。
默认情况下,WordPress 允许您从管理仪表板升级 WordPress 核心、主题和插件。每当您尝试升级或安装新插件时,某些主机都需要 FTP 或 SSH 连接。

通过使用这些代码,您可以设置 FTP 或 SSH 常量,而不必再担心它。
通过使用这些代码,您可以设置 FTP 或 SSH 常量,而不必再担心它。
// forces the filesystem method: "direct", "ssh", "ftpext", or "ftpsockets"
define('FS_METHOD', 'ftpext');
// absolute path to root installation directory
define('FTP_BASE', '/path/to/wordpress/');
// absolute path to "wp-content" directory
define('FTP_CONTENT_DIR', '/path/to/wordpress/wp-content/');
// absolute path to "wp-plugins" directory
define('FTP_PLUGIN_DIR ', '/path/to/wordpress/wp-content/plugins/');
// absolute path to your SSH public key
define('FTP_PUBKEY', '/home/username/.ssh/id_rsa.pub');
// absolute path to your SSH private key
define('FTP_PRIVKEY', '/home/username/.ssh/id_rsa');
// either your FTP or SSH username
define('FTP_USER', 'username');
// password for FTP_USER username
define('FTP_PASS', 'password');
// hostname:port combo for your SSH/FTP server
define('FTP_HOST', 'ftp.example.org:21');
注意:不要忘记将 WordPress 路径和 ftp.example.com 替换为您自己的 FTP 主机信息。
注意:不要忘记将 WordPress 路径和 ftp.example.com 替换为您自己的 FTP 主机信息。
10.允许自动数据库修复 (10. Allow Automatic Database Repair)
WordPress comes with a built-in feature to automatically 优化和修复WordPress数据库. However, this feature is turned off by default.
WordPress具有内置功能,可以自动优化和修复WordPress数据库 。 但是,此功能默认情况下处于关闭状态。
要启用此功能,您需要将以下行添加到 WordPress 配置文件中。
要启用此功能,您需要将以下行添加到 WordPress 配置文件中。
define('WP_ALLOW_REPAIR', true);
添加后,需要访问以下网址来优化和修复WordPress数据库
添加后,您需要访问以下网址来优化和修复您的WordPress数据库
http://example.com/wp-admin/maint/repair.php
http://example.com/wp-admin/maint/repair.php
不要忘记将 example.com 替换为您自己的域名。您将看到一个简单的页面,其中包含修复或修复和优化数据库的选项。您无需登录即可访问此页面。
不要忘记将 example.com 替换为您自己的域名。您将看到一个简单的页面,其中包含修复或修复和优化数据库的选项。您无需登录即可访问此页面。

11.增加PHP内存限制 (11. Increase PHP Memory Limit)
Some of the most common WordPress errors are caused by PHP 内存耗尽. You can increase the PHP memory limit through wp-config.php file. Simply paste the code below:
一些最常见的WordPress错误是由于PHP 内存耗尽而引起的。 您可以通过wp-config.php文件增加PHP内存限制。 只需粘贴以下代码:
define('WP_MEMORY_LIMIT', '128M');
12.移动wp-content目录 (12. Moving wp-content Directory)
WordPress 允许您移动 wp-content 目录。一些专家认为它可以帮助加强 WordPress 的安全性。
WordPress 允许您移动 wp-content 目录。一些专家认为它可以帮助加强 WordPress 的安全性。
您需要将以下代码添加到 wp-config.php 文件中:
您需要将以下代码添加到 wp-config.php 文件中:
define( 'WP_CONTENT_DIR', $_SERVER['DOCUMENT_ROOT'] . '/blog/wp-content' );
define( 'WP_CONTENT_URL', 'http://example/blog/wp-content');
define( 'WP_PLUGIN_DIR', $_SERVER['DOCUMENT_ROOT'] . '/blog/wp-content/plugins' );
define( 'WP_PLUGIN_URL', 'http://example/blog/wp-content/plugins');
不要忘记将 example.com 替换为您自己的域名。
不要忘记将 example.com 替换为您自己的域名。
13.使用自定义用户表 (13. Use Custom User Tables)
默认情况下,WordPress 将所有用户数据保存在表 wp_users 和 wp_usermeta 中。通过使用下面的函数,您可以指定要存储用户信息的表。
默认情况下,WordPress 将所有用户数据保存在表 wp_users 和 wp_usermeta 中。通过使用以下函数,您可以指定存储用户信息的表。
define('CUSTOM_USER_TABLE', $table_prefix.'my_users');
define('CUSTOM_USER_META_TABLE', $table_prefix.'my_usermeta');
14.启用多站点网络 (14. Enable Multi-Site Network)
Each WordPress site comes with a built-in multisite feature which allows you to create multiple WordPress sites using the same installation. To learn more, see our complete guide on 如何安装和设置 WordPress 多站点网络.
每个WordPress网站都具有内置的多站点功能,使您可以使用同一安装来创建多个WordPress网站。 要了解更多信息,请参阅有关如何安装和设置 WordPress 多站点网络完整指南。
您可以通过将以下行添加到 WordPress 配置文件来启用多站点功能:
您可以通过将以下行添加到 WordPress 配置文件来启用多站点功能:
define('WP_ALLOW_MULTISITE', true);
15.保护您的WordPress配置文件 (15. Securing Your WordPress Configuration File)
正如您所看到的,wp-config.php 文件包含非常重要的 WordPress 设置。默认情况下,它位于 WordPress 根文件夹中,但您可以移动它。它可以移到 public_html 目录之外,这样用户就无法访问它。如果在 WordPress 根文件夹中找不到文件,WordPress 默认会在其他目录中查找。
如您所见,wp-config.php 文件包含非常重要的 WordPress 设置。默认情况下,它位于 WordPress 根文件夹中,但您可以移动它。可以将其移至 public_html 目录之外,以便用户无法访问它。如果在 WordPress 根文件夹中找不到文件,WordPress 默认会在其他目录中查找。
You can also add the following code to your .htaccess 文件 to limit access to this file.
您还可以将以下代码添加到您的.htaccess 文件,以限制对此文件的访问。
# Protect wp-config.php
<Files wp-config.php>
order allow,deny
deny from all
</Files>
We hope this article helped you learn some useful WordPress configuration tricks that you didn’t know. You may also want to see our mega list of 超过 55 个最受欢迎的 WordPress 提示、技巧和窍门 that you can use on your site.
我们希望本文能帮助您学习一些您不知道的有用的WordPress配置技巧。 您可能还希望查看我们的大型列表,其中包含55+ 个最受欢迎的 WordPress 提示、技巧和技巧 ,您可以在自己的网站上使用它们。
If you liked this article, then please subscribe to our YouTube 频道 for WordPress video tutorials. You can also find us on 推特 and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube 频道 WordPress视频教程。 您也可以在推特和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/useful-wordpress-configuration-tricks-that-you-may-not-know/
WordPress 配置