使用wp-config自定义WordPress

As you may already know, you can entirely customize the layout of a WordPress website thanks to the different existing themes, and you can even create your own if you want to have a fully personalized website.

如您所知,由于存在不同的主题,您可以完全自定义WordPress网站的布局,如果您要拥有一个完全个性化的网站,甚至可以创建自己的主题。

That’s a good thing, but did you know that the configuration of WordPress itself can also be modified? That’s the aim of the wp-config file.

这是一件好事,但您知道WordPress本身的配置也可以修改吗? 这就是wp-config文件的目的。

In this article, we will take a look at the wp-config file in order to know what it is, what it contains, and what we can (and must not!) do with it.

在本文中,我们将看一下wp-config文件,以了解它的含义,包含的内容以及我们可以(一定不能!)进行的操作。

什么是wp-config文件? (What Is the wp-config File?)

When I mention wp-config file, I am referring to the file wp-config.php that is located in the root of your WordPress installation. It contains much of the information that WordPress needs to function. If you don’t correctly fill this file, your WordPress installation won’t work. That means you won’t be able to start your website and you could even break your installation by making a mistake in an edit of this file.

当我提到wp-config文件时,我指的是位于WordPress安装根目录中的文件wp-config.php 。 它包含WordPress需要运行的许多信息。 如果您没有正确填写此文件,则您的WordPress安装将无法正常工作。 这意味着您将无法启动网站,甚至可能由于在编辑此文件时出错而中断安装。

To avoid these mistakes, it is important to know exactly what the wp-config file contains. This way, you will be able to modify what you want without taking any risk (but be sure to test your changes locally before sending them on to your server).

为避免这些错误,准确了解wp-config文件包含的内容非常重要。 这样,您将能够修改所需的内容而不会冒任何风险(但请确保在将更改发送到服务器之前在本地测试您的更改)。

Moreover, understanding the wp-config file will allow you to add some parameters, to modify the default behavior of WordPress.

此外,了解wp-config文件将使您可以添加一些参数,以修改WordPress的默认行为。

Note, you don’t have to manually edit the wp-config file, even if WordPress is not yet configured. In fact, the WordPress installation assistant asks you the needed information if it’s not present. This means you’ll only have to edit the wp-config file if you want to customize some settings.

请注意,即使尚未配置WordPress,也不必手动编辑wp-config文件。 实际上,WordPress安装助手会询问您所需的信息(如果不存在)。 这意味着,如果要自定义某些设置,则只需要编辑wp-config文件。

In the next section of this article, we will cover the different settings you’ll have to include in your wp-config file if you want to edit it by yourself.

在本文的下一部分中,我们将介绍您要自行编辑的wp-config文件中必须包含的不同设置。

If you just downloaded the WordPress archive, then you don’t have a wp-config file yet. In its place, you have a file called wp-config-sample.php. If you don’t want to use the installation assistant, edit the wp-config-sample.php file and rename it to wp-config.php.

如果您只是下载WordPress存档,那么您还没有wp-config文件。 取而代之的是一个名为wp-config-sample.php的文件。 如果您不想使用安装助手,请编辑wp-config-sample.php文件并将其重命名为wp-config.php

我们可以在此文件中找到什么? (What Can We Find in This File?)

In this section, we will cover the list of what we can find in the wp-config file in its current state, in WordPress 4.1. At the same time, we will see how to edit these lines if needed.

在本节中,我们将介绍在WordPress 4.1中处于当前状态的wp-config文件中可以找到的内容的列表。 同时,我们将看到如何根据需要编辑这些行。

数据库设置 (Database Settings)

The database settings are the only mandatory settings. You must indicate the right values. If not, WordPress cannot create the tables it needs to work. These settings, as many of the others in the wp-config file, consist in a list of constants.

数据库设置是唯一的必需设置。 您必须指出正确的值。 否则,WordPress无法创建需要工作的表。 这些设置与wp-config文件中的其他设置一样,包含在常量列表中。

To define a constant in PHP, you have to use the define() function. In the first parameter, give the name of the constant and, in the second one, its value, which can be a string, a number, or whatever you want.

要在PHP中定义一个常量,您必须使用define()函数。 在第一个参数中,给出常量的名称,在第二个参数中,给出其值,该值可以是字符串,数字或任何您想要的值。

As you may realise, we will need to indicate the database credentials in order to let WordPress use it. If you don’t know all of the information required, you can search for the details in the admin panel of your web hosting.

正如您可能意识到的那样,我们将需要指示数据库凭据才能让WordPress使用它。 如果您不知道所需的所有信息,则可以在虚拟主机的管理面板中搜索详细信息。

The first constant defining a database setting is DB_NAME. As its name suggests, it must contain the name of the database you want to use to store what is needed by WordPress. The value here should be a string representing the corresponding name.

定义数据库设置的第一个常量是DB_NAME 。 顾名思义,它必须包含要用于存储WordPress所需内容的数据库的名称。 此处的值应该是代表相应名称的字符串。

define('DB_NAME', 'wordpressdatabase');

Following the database name, we find the DB_USER constant, which must contain the name of the user having the right to use the database. This user should have a password indicated in the DB_PASSWORD constant.

在数据库名称之后,我们找到DB_USER常量,该常量必须包含有权使用数据库的用户名。 该用户应具有DB_PASSWORD常数中指示的密码。

define('DB_USER', 'databaseuser');
define('DB_PASSWORD', 'dBpAsSwOrD');

Finally, the latest mandatory database setting is the DB_HOST constant, which must contain the server on which we can find your database. Often, the 'localhost' value is enough, but maybe your web hosting gives you an IP address or even a subdomain.

最后,最新的强制性数据库设置是DB_HOST常量,该常量必须包含我们可以在其中找到您的数据库的服务器。 通常, 'localhost'值就足够了,但也许您的虚拟主机为您提供了IP地址甚至是子域。

define('DB_HOST', 'localhost');

The next two constants are also database-related, but concern the character set used by the WordPress tables. By default, WordPress will use the UTF-8, but if you want to change this, indicate your preference into the DB_CHARSET constant. You can even set your own collation with the DB_COLLATE constant.

接下来的两个常量也与数据库有关,但是与WordPress表使用的字符集有关。 默认情况下,WordPress将使用UTF-8,但是如果您要更改此设置,请在DB_CHARSET常量中指明您的偏好。 您甚至可以使用DB_COLLATE常量设置自己的排序DB_COLLATE

A few lines after these constants definitions, we find the declaration of a variable called $table_prefix. WordPress uses this variable to name the table it creates. By default, its value is wp_, so all the tables created by WordPress will begin with wp_, like wp_posts or wp_options.

在这些常量定义之后的几行,我们找到了一个名为$table_prefix的变量的声明。 WordPress使用此变量来命名它创建的表。 默认情况下,其值为wp_ ,因此WordPress创建的所有表都将以wp_ ,例如wp_postswp_options

验证码 (Authentication Keys)

To automatically log in your users, WordPress uses cookies. Information stored in these cookies are encrypted, and you can get a better encryption thanks to a set of eight constants.

为了自动登录用户,WordPress使用cookie。 存储在这些cookie中的信息被加密,并且由于有八个常量,您可以获得更好的加密。

The AUTH_KEY, SECURE_AUTH_KEY, LOGGED_IN_KEY and NONCE_KEY constants are required if you want a better encryption and security. You can enhance this encryption with salts, thanks to the AUTH_SALT, SECURE_AUTH_SALT, LOGGED_IN_SALT and NONCE_SALT constants.

如果您想要更好的加密和安全性,则需要AUTH_KEYSECURE_AUTH_KEYLOGGED_IN_KEYNONCE_KEY常量。 借助AUTH_SALTSECURE_AUTH_SALTLOGGED_IN_SALTNONCE_SALT常量,您可以使用盐增强此加密。

The value accepted by these constants are strings. These strings should be unique in order to have a better security. Moreover, try to use different and special characters, otherwise your phrases can be guessed.

这些常量接受的值是字符串。 这些字符串应该是唯一的,以便具有更好的安全性。 此外,请尝试使用不同的特殊字符,否则可能会猜出您的短语。

To help you, WordPress provides you with a tool: the online generator. This tool will generate unique phrases, displayed in a code that defines the right constants: all you have to do is copy and paste the result in the place of your current definitions.

为了帮助您,WordPress为您提供了一个工具: 在线生成器 。 该工具将生成唯一的短语,并显示在定义正确的常量的代码中:您要做的就是将结果复制并粘贴到当前定义的位置。

You can change your keys and salts at any time. When you do this, the information stored in your users’ cookies are invalidated: your users will have to log in manually next time.

您可以随时更改钥匙和盐。 当您这样做时,存储在用户Cookie中的信息将无效:您的用户下次必须手动登录。

调试模式 (Debugging Mode)

Right after the declaration of the $tables_prefix variable we find the WP_DEBUG constant, defined to false by default. If you change it to true, WordPress will display some information which can be useful if you develop for this platform.

在声明$tables_prefix变量之后,我们立即找到WP_DEBUG常量,默认情况下将其定义为false 。 如果将其更改为true ,则WordPress将显示一些信息,如果您为此平台开发,它将很有用。

You should avoid this option if this WordPress installation is not only for developing. However, if you activate the debugging mode, then you will be able to add new useful options we will cover in the next section.

如果此WordPress安装不仅用于开发,则应避免使用此选项。 但是,如果您激活调试模式,那么您将能够添加新的有用选项,我们将在下一部分中介绍。

请勿触摸以下内容! (Don’t Touch the Following!)

The WP_DEBUG constant is the last default you can modify. However, after this constant, we find another one called ABSPATH. Do not modify this constant. WordPress uses it to retrieve the absolute path of your WordPress installation.

WP_DEBUG常数是您可以修改的最后一个默认值。 但是,在此常量之后,我们找到了另一个称为ABSPATH 。 不要修改此常数。 WordPress使用它来检索WordPress安装的绝对路径。

Finally, the wp-config file is ending with the inclusion of another file: wp-settings.php, which is located at the root of your installation. This file sets up some constants, variables or functions used by WordPress. Once again, do not touch this file, or the path used in the require_once() function called in the wp-config file.

最后,wp-config文件以另一个文件结尾: wp-settings.php ,它位于安装的根目录下。 该文件设置了WordPress使用的一些常量,变量或函数。 再一次,请勿触摸该文件,也不要触摸wp-config文件中调用的require_once()函数中使用的路径。

我们可以在此文件中添加什么? (What Can We Add in This File?)

Above we explored how to edit the wp-config file, but we can also add some things in this file, often some constants.

上面我们探讨了如何编辑wp-config文件,但是我们也可以在该文件中添加一些内容,通常是一些常量。

To add things in the wp-config file, you have to respect one rule: the additions must be done before the definition of the ABSPATH constant, right after the definition of the WP_DEBUG constant by default.

要在wp-config文件中添加内容,您必须遵守一条规则:添加必须在ABSPATH常量的定义之前完成,默认情况下在WP_DEBUG常量的定义之后。

We will now review some useful additions we can include in the wp-config file, but this list is by no means exhaustive.

现在,我们将回顾一些可以包含在wp-config文件中的有用添加,但是该列表绝不是详尽无遗的。

自动更新 (Automatic Updates)

If you read our article about updating WordPress, you have already seen an example of additions in the wp-config file. In fact, if you want to disable automatic updates, you can add the following constant.

如果您阅读了有关更新WordPress的文章,您已经在wp-config文件中看到了添加示例。 实际上,如果要禁用自动更新,则可以添加以下常量。

define('AUTOMATIC_UPDATER_DISABLED', true);

But you can also activate the automatic updates for the major ones with the following constant.

但是,您也可以使用以下常量激活主要更新的自动更新。

define('WP_AUTO_UPDATE_CORE', true);

移动文件夹 (Moving Folders)

By default, WordPress stores your plugins and themes into the wp-content subdirectory of the root of your installation. If you want, you can change this behavior by defining by yourself the WP_CONTENT_DIR constant in the wp-config file.

默认情况下,WordPress将您的插件和主题存储到安装根目录的wp-content子目录中。 如果需要,可以通过在wp-config文件中自己定义WP_CONTENT_DIR常量来更改此行为。

To find the folder wp-content, you can use the PHP function dirname() to retrieve the path to the directory containing your wp-config file, that is the root of your WordPress installation.

要找到文件夹wp-content ,可以使用PHP函数dirname()检索包含wp-config文件的目录的路径,该文件是WordPress安装的根目录。

define('WP_CONTENT_DIR', dirname(__FILE__) . '/path/to/my/content-dir');

Do not add a trailing slash in this path. If you want to change this directory, you should also change its URL by defining the value of the WP_CONTENT_URL constant, once again without trailing slash.

请勿在此路径中添加斜杠。 如果要更改此目录,则还应该通过定义WP_CONTENT_URL常量的值来更改其URL,再次使用斜杠。

define('WP_CONTENT_URL', 'http://mysite.com/path/to/my/content-dir');

Following the same format, you can also define your own paths for the plugins directory. You can, for example, use the content directory you just defined.

按照相同的格式,您还可以为plugins目录定义自己的路径。 例如,您可以使用刚定义的内容目录。

define('WP_PLUGIN_DIR', WP_CONTENT_DIR . '/myplugins');
define('WP_PLUGIN_URL', WP_CONTENT_URL . '/myplugins');

You can change the uploads directory, too, thanks to the UPLOADS constant.

您也可以更改上载目录,这要归功于UPLOADS常量。

define('UPLOADS', 'my/subdirectory/for/uploads');

Without a trailing slash, this path must not be absolute: it is relative to the ABSPATH constant defined by WordPress.

没有斜杠,此路径不能是绝对路径:它是相对于WordPress定义的ABSPATH常数。

Note that it is not possible to move the themes directory which always must be the themes subdirectory of the content directory defined with the WP_CONTENT_DIR constant.

注意,不可能移动主题目录,该主题目录必须始终是用WP_CONTENT_DIR常量定义的内容目录的themes子目录。

调试 (Debugging)

错误报告 (Error Reporting)

We already saw that we can activate the debugging mode thanks to the WP_DEBUG constant set to true. If you activate this option, WordPress will display all errors by raising the error reporting level to E_ALL and you will know when you use deprecated functions thanks to warnings.

我们已经看到由于WP_DEBUG常量设置为true ,因此可以激活调试模式。 如果激活此选项,WordPress将通过将错误报告级别提高到E_ALL来显示所有错误,并且由于警告,您将知道何时使用不推荐使用的功能。

You can control the way errors are reported to you by playing with the WP_DEBUG_LOG and WP_DEBUG_DISPLAY constants. Set to true, the former will send errors in a log file while the latter will directly display the errors.

您可以通过使用WP_DEBUG_LOGWP_DEBUG_DISPLAY常量来控制向您报告错误的方式。 设置为true ,前者将在日志文件中发送错误,而后者将直接显示错误。

// I don't want to miss any errors!
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', true);
WordPress脚本和样式 (WordPress Scripts and Styles)

Another useful constant is the SCRIPT_DEBUG one. Once again, it’s a boolean set to false by default. By setting it to true, WordPress will change the way it includes its JavaScript and CSS files. By default it includes their minified versions but, when this option is activated, it includes their full versions, which is useful when you want to edit them.

另一个有用的常量是SCRIPT_DEBUG 。 再一次,它是一个默认设置为false的布尔值。 通过将其设置为true ,WordPress将更改其包括JavaScript和CSS文件的方式。 默认情况下,它包括其最小版本,但是,当激活此选项时,它包括其完整版本,这在您要编辑它们时很有用。

define('SCRIPT_DEBUG', true);
查询 (Queries)

If you need to analyze the queries WordPress does during the display of a page, you can set the SAVEQUERIES constant to true.

如果您需要分析WordPress在页面显示期间进行的查询,可以将SAVEQUERIES常量设置为true

define('SAVEQUERIES', true);

That way, you will be able to retrieve all the queries in the queries attribute of the $wpdb object. For example, in the footer, you can display the queries if the current user is an administrator.

这样,你就能以检索所有查询queries中的属性$wpdb对象。 例如,在页脚中,如果当前用户是管理员,则可以显示查询。

if (current_user_can('administrator')) {
	global $wpdb;
	echo '<pre>' . print_r($wpdb->queries, true) . '</pre>';
}

帖子相对常数 (Posts-Relative Constants)

修订版 (Revisions)

As you may know, each time you edit a post or a page, WordPress saves your edit into a ‘revision’. That way, you will be able to retrieve a previous version of your post and cancel some edits if needed.

您可能知道,每次编辑帖子或页面时,WordPress都会将您的编辑保存到“修订”中。 这样,您将能够检索帖子的先前版本,并在需要时取消一些编辑。

If you are sure you don’t want revisions, you can deactivate this behavior by setting the WP_POST_REVISIONS constant to false. By default, its value is true and WordPress will create a new revision each time you edit a post, but you can also limit the number of revisions by giving an integer as a value to the same constant.

如果确定不希望进行修订,则可以通过将WP_POST_REVISIONS常量设置为false来停用此行为。 默认情况下,其值为true并且每次编辑帖子时WordPress都会创建一个新修订,但是您也可以通过将整数作为值赋予相同的常量来限制修订的数量。

// I don't want to create revisions
define('WP_POST_REVISIONS', false);

// I love revisions!
define('WP_POST_REVISIONS', true);

// Too many revisions?
define('WP_POST_REVISIONS', 3);
自动保存 (Automatic Saves)

When you are editing a post (or when you are creating a new one), WordPress doesn’t wait for you to finish your edits to save a draft or a revision: using AJAX, it will save your changes regularly, respecting a delay given by the AUTOSAVE_INTERVAL constant. By default, this delay is 60 seconds but you can change it by giving the number of seconds you want.

当您编辑帖子时(或创建新帖子时),WordPress不会等待您完成编辑以保存草稿或修订:使用AJAX,它将定期保存您的更改,并避免出现延迟通过AUTOSAVE_INTERVAL常量。 默认情况下,此延迟为60秒,但您可以通过指定所需的秒数来更改它。

// Save my changes two times per minute
define('AUTOSAVE_INTERVAL', 30);
垃圾桶 (Trash Bin)

Sometimes, we delete posts. To do that, WordPress provides us a trash bin and it empties this trash bin every 30 days. You can change this delay by setting the number of days you want to the EMPTY_TRASH_DAYS constant. If you set this value to 0, WordPress will disable the trash bin: by deleting a post, it will be removed permanently, without any confirmation.

有时,我们删除帖子。 为此,WordPress为我们提供了一个垃圾桶,它每30天清空一次该垃圾桶。 您可以通过将所需天数设置为EMPTY_TRASH_DAYS常数来更改此延迟。 如果将此值设置为0 ,则WordPress将禁用垃圾箱:通过删除帖子,该帖子将被永久删除,而无需任何确认。

// Delete permanently items from the trash bin every two months
define('EMPTY_TRASH_DAYS', 60);
// Disabling the trash bin
define('EMPTY_TRASH_DAYS', 0);

Note that all trash bins are involved so not only will posts will be deleted permanently, but also pages, comments and attachments.

请注意,所有垃圾桶都包含在内,因此不仅帖子将被永久删除,而且页面,评论和附件也将被删除。

结论 (In Conclusion)

The wp-config file is an important one in any WordPress installation. It defines the database settings needed to store data, but also some useful options when you develop a plugin or a theme for WordPress.

wp-config文件是任何WordPress安装中的重要文件。 它定义了存储数据所需的数据库设置,还定义了为WordPress开发插件或主题时的一些有用选项。

Now you know what can be found in this file, so you can edit it without any concerns. Don’t forget to always test your edits on a local version of WordPress, in order to prevent any potential issues on a live site.

现在,您知道在此文件中可以找到什么,因此您可以编辑它而不必担心。 不要忘记总是在本地版本的WordPress上测试您的编辑,以防止在实时站点上出现任何潜在问题。

As covered above, the list of edits and additions we’ve touched upon in this article is by no means exhaustive. I tried to present you the constants I think are the most useful, but the WordPress Codex gives you even more possibilities for the wp-config file if you want to explore it deeper.

如上所述,我们在本文中涉及的编辑和添加列表绝非详尽无遗。 我试图向您介绍我认为最有用的常量,但是如果您想更深入地研究WordPress Codex,wp-config文件将为您提供更多的可能性

翻译自: https://www.sitepoint.com/using-wp-config-customize-wordpress/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值