WordPress 最重要的文件之一就是 wp-config.php 文件,是 WordPress的配置文件,该文件位于 WordPress 文件目录的根目录中,包含了数据库连接信息、基本配置及高级配置信息等等。本文就来介绍下 WordPress 配置文件 wp-config.php 的说明及各类高级使用方法。

wp-config.php 文件在哪里?

首次下载 WordPress 时,默认是不包含 wp-config.php 文件的,会在安装 WordPress 的时候自动创建。也可以在根目录中找到名为 wp-config-sample.php 这个文件,另存为 wp-config.php,然后根据需要对 wp-config.php 进行编辑。

建议使用的专门的编辑器软件来修改 wp-config.php,例如 Sublime Text、Atom、Notepad++等,不要使用系统自带的记事本来编辑。

wp-config.php 文件说明

默认的 wp-config.php 文件主要包含以下几段内容。

数据库设定部分

代码如下

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define( 'DB_NAME', 'database_name_here' );

/** MySQL database username */
define( 'DB_USER', 'username_here' );

/** MySQL database password */
define( 'DB_PASSWORD', 'password_here' );

/** MySQL hostname */
define( 'DB_HOST', 'localhost' );

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8' );

/** The Database Collate type. Don't change this if in doubt. */
define( 'DB_COLLATE', '' );
WordPress wp config php Databse
WordPress 数据库设置

唯一身份密钥部分

这部分可以通过 WordPress 官方工具生成,https://api.wordpress.org/secret-key/1.1/salt/,每次打开都是随机的内容,生成后直接覆盖掉原来的部分即可。

代码如下

/**#@+
 * Authentication Unique Keys and Salts.
 *
 * Change these to different unique phrases!
 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
 *
 * @since 2.6.0
 */
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' );

/**#@-*/

数据库前缀部分

数据库前缀的设置对提高你的站点安全以及在同一数据库中安装多个WordPress站点都非常有用。

代码如下

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each
 * a unique prefix. Only numbers, letters, and underscores please!
 */
$table_prefix = 'wp_';

WordPress 调试模式部分

默认是关闭的,代码如下。

/**
 * For developers: WordPress debugging mode.
 *
 * Change this to true to enable the display of notices during development.
 * It is strongly recommended that plugin and theme developers use WP_DEBUG
 * in their development environments.
 *
 * For information on other constants that can be used for debugging,
 * visit the documentation.
 *
 * @link https://wordpress.org/support/article/debugging-in-wordpress/
 */
define( 'WP_DEBUG', false );

如需开启 WordPress 调试模式,把上面代码中的 false 替换成 true 即可。

目录设置部分

代码如下

/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
	define( 'ABSPATH', __DIR__ . '/' );
}

/** Sets up WordPress vars and included files. */
require_once ABSPATH . 'wp-settings.php';

wp-config.php 高级使用方法

通过修改 wp-config.php,增加自定义代码,可以开启 WordPress 的高级功能,在下图位置插入自定义代码。

WordPress wp config php Advanced Options
插入自定义代码

后台强制开启 https 加密登陆

强制开启后台 https 加密登陆,可以有效的保护数据传输。

/** 后台强制开启 https 加密登陆 */
$_SERVER['HTTPS'] = 'on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

管理修订版本数量

文章修订版本 WordPress 默认是开启的,以便于在编写文章时进行新老文章的差异对比,也可以对历史修订版本进行恢复,如下为保存3份修订版本。

/** 仅保留3份修订版本 */
define( 'WP_POST_REVISIONS', 3 );

禁用修订版本

/** 禁用修订版本 */
define( 'WP_POST_REVISIONS', false );

修改自动保存时间间隔

在创建和编辑 WordPress 文章的时候,会自动将内容保存到修订版中,默认值为60秒,可以通过以下代码修改 WordPress 自动保存的时间间隔。

/** 修改自动保存时间间隔 */
define( 'AUTOSAVE_INTERVAL', 160);

禁用自动更新

WordPress 在默认情况下是开启自动更新的,如果需要禁用 WordPress 的自动更新,可以通过以下代码实现。

/** 禁用 WordPress 所有自动更新 */
define( 'AUTOMATIC_UPDATER_DISABLED', true );

这里是禁用所有的自动更新,如果需要仅禁用内核的自动更新,可通过以下代码实现。

/** 禁用 WordPress 内核自动更新 */
define( 'WP_AUTO_UPDATE_CORE', false );

WP_AUTO_UPDATE_CORE 有三个值可以定义,每个值会产生不同的行为,如下所述。

  • ture:启用次要和主要更新
  • false:禁用主要和次要更新
  • minor:只启用主要更新

禁用插件和主题编辑

当把 WordPress 交付给客户使用的时候,建议禁用插件和主题编辑来保证网站的稳定性,通过以下代码实现。

/** 禁止主题和插件的编辑功能 */
define( 'DISALLOW_FILE_EDIT', true );
 
/** 禁止主题和插件的编辑及更新 */
define( 'DISALLOW_FILE_MODS', true );

自动清理回收站

可以使用以下代码实现定时自动情况回收站。

/** 自动清理回收站 */
define( 'EMPTY_TRASH_DAYS', 7 );

上面的数字30是天数,如果设置为0,则会禁用回收站功能,当你删除文章的时候回直接永久删除。

开启 WordPress 多站点功能

通过使用一套源代码来实现多个站点使用,通过以下代码来实现 WordPress 的多站点功能。

/** WP开启多站点模式 */
define( 'WP_ALLOW_MULTISITE', true );

参考资料

5/5 - (8 votes)