如何在WordPress中编辑wp-config.php文件
您是否阅读过要求您编辑wp-config文件的教程,而您不知道它是什么?好吧,我们已经让你满意了。在本文中,我们将向您展示如何在WordPress中正确编辑wp-config.php文件。
什么是wp-config.php文件?
顾名思义,它是一个配置文件,是所有自托管WordPress站点的一部分。
与其他文件不同,wp-config.php文件不是内置于WordPress,而是在安装过程中专门为您的站点生成的。
WordPress将您的数据库信息存储在wp-config.php文件中。如果没有此信息,您的WordPress网站将无法正常工作,您将收到“建立数据库连接错误”错误。
除了数据库信息,wp-config.php文件还包含其他几个高级设置。我们将在本文后面解释它们。
由于此文件包含大量敏感信息,因此建议您不要乱用此文件,除非您完全没有其他选择。
但是,因为您正在阅读本文,这意味着你必须编辑wp-config.php文件。以下是执行此操作而不会搞砸的步骤。
视频教程
如果您不喜欢该视频或需要更多说明,请继续阅读。
入门
您需要做的第一件事就是创建一个完整的WordPress备份。wp-config.php文件对WordPress网站至关重要,因为一个小错误会使您的网站无法访问。
您需要一个FTP客户端才能连接到您的网站。Windows用户可以安装WinSCP或SmartFTP,Mac用户可以尝试使用Transmit或CyberDuck。FTP客户端允许您在服务器和计算机之间传输文件。
使用FTP客户端连接到您的网站。您将需要可以从Web主机获取的FTP登录信息。如果您不知道您的FTP登录信息,那么您可以向您的Web主机寻求支持。
wp-config.php文件通常位于您网站的根文件夹中,其他文件夹如/wp-content/.
只需右键单击该文件,然后选择从菜单。您的FTP客户端现在将wp-config.php文件下载到您的计算机。您可以使用纯文本编辑器程序(如记事本或文本编辑)打开和编辑它。
了解wp-config.php文件
在开始之前,让我们看一下默认wp-config.php文件的完整代码。您还可以在此处查看此文件的示例。
<?php /** * The base configuration for WordPress * * The wp-config.php creation script uses this file during the * installation. You don"t have to use the web site, you can * copy this file to "wp-config.php" and fill in the values. * * This file contains the following configurations: * * * MySQL settings * * Secret keys * * Database table prefix * * ABSPATH * * @link https://codex.wordpress.org/Editing_wp-config.php * * @package WordPress */ // ** 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", ""); /**#@+ * 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 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_"; /** * 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 Codex. * * @link https://codex.wordpress.org/Debugging_in_WordPress */ define("WP_DEBUG", false); /* That"s all, stop editing! Happy blogging. */ /** Absolute path to the WordPress directory. */ if ( !defined("ABSPATH") ) define("ABSPATH", dirname(__FILE__) . "/"); /** Sets up WordPress vars and included files. */ require_once(ABSPATH . "wp-settings.php");
wp-config.php文件的每个部分都在文件本身中有详细记录。这里的几乎所有设置都是使用PHP常量定义的。
define( "constant_name" , "value");
wp-config.php文件中的MySQL设置
您的WordPress数据库连接设置显示在wp-config.php文件的“MySQL设置”部分下。您将需要您的MySQL主机,数据库名称,数据库用户名和密码来填写此部分。
// ** 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", "");
您可以在标有数据库的部分下从您的Web主机帐户的cPanel获取您的数据库信息。
如果您找不到您的WordPress数据库或MySQL用户名和密码,那么您需要联系您的网站主办。
身份验证密钥和密码
身份验证唯一键和盐是安全密钥,有助于提高WordPress站点的安全性。这些密钥为WordPress生成的用户会话和cookie提供了强大的加密功能。有关详细信息,请参阅我们的WordPress安全密钥指南。
/**#@+ * 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网站可能已被盗用,这将特别有用。更改安全密钥将注销WordPress站点上所有当前登录的用户,强制他们再次登录。
WordPress数据库表前缀
默认情况下,WordPress将wp_前缀添加到WordPress创建的所有表中。建议您将WordPress数据库表前缀更改为随机的。这将使黑客难以猜测您的WordPress表,并将使您免于一些常见的SQL注入攻击。
/** * 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网站的此值。按照我们如何更改WordPress数据库前缀文章中的说明更改现有WordPress站点上的这些设置。
WordPress调试模式
此设置对于尝试学习WordPress开发的用户和尝试实验性功能的用户特别有用。默认情况下,WordPress会在执行代码时隐藏PHP生成的通知。只需将调试模式设置为true即可显示这些通知。这为开发人员提供了查找错误的重要信息。
define("WP_DEBUG", false);
绝对路径设置
wp-config文件的最后一部分定义了绝对路径,然后用于设置WordPress变量和包含的文件。你根本不需要改变任何东西。
/** Absolute path to the WordPress directory. */ if ( !defined("ABSPATH") ) define("ABSPATH", dirname(__FILE__) . "/"); /** Sets up WordPress vars and included files. */ require_once(ABSPATH . "wp-settings.php");
有用的wp-config.php黑客和设置
还有一些其他的wp-config.php设置可以帮助您解决错误并解决许多常见问题WordPress错误。
在WordPress中更改MySQL端口和套接字
如果您的WordPress托管服务提供商使用MySQL主机的备用端口,则需要更改DB_HOST值以包含端口号。请注意,这不是新行,但您需要编辑现有的DB_HOST值。
define( "DB_HOST", "localhost:5067" );
不要忘记将端口号5067更改为您的Web主机提供的任何端口号。
如果您的主机使用MySQL的套接字和管道,那么您需要像这样添加它:
define( "DB_HOST", "localhost:/var/run/mysqld/mysqld.sock" );
使用wp-config.php更改WordPress URL文件
您可以在将WordPress网站移动到新域名或新的Web主机时需要更改WordPress URL。您可以通过访问设置»常规页面来更改这些网址。
您还可以使用wp-config.php文件更改这些URL。如果由于错误太多指示问题而无法访问WordPress管理区域,这会很方便。只需将这两行添加到wp-config.php文件中:
define("WP_HOME","http://example.com"); define("WP_SITEURL","http://example.com");
不要忘记用您自己的域名替换example.com。您还需要记住,搜索引擎将www.example.com和example.com视为两个不同的位置(请参阅www与非www – 哪个更适合SEO?)。如果您的网站使用www前缀编制索引,则需要相应地添加您的域名。
使用wp-config.php更改上载目录
默认情况下,WordPress会将所有媒体上载存储在/ wp-content / uploads /目录中。如果要将媒体文件存储在其他位置,则可以通过在wp-config.php文件中添加此行代码来实现。
define( "UPLOADS", "wp-content/media" );
请注意,uploads目录路径是相对于在WordPress中自动设置的ABSPATH。在此处添加绝对路径将不起作用。有关更多信息,请参阅有关如何在WordPress中更改默认媒体上载位置的详细指南。
禁用WordPress中的自动更新
WordPress在WordPress 3.7中引入了自动更新。它允许WordPress站点在有可用的次要更新时自动更新。虽然自动更新非常适合安全性,但在某些情况下,它们可能会破坏WordPress网站,使其无法访问。
将此单行代码添加到您的wp-config.php文件将禁用WordPress站点上的所有自动更新。
define( "WP_AUTO_UPDATE_CORE", false );
有关如何在WordPress中禁用自动更新的详细信息,请参阅我们的教程。
限制WordPress中的后期修订
WordPress附带内置自动保存和修订。请参阅我们的教程,了解如何使用后期修订撤消WordPress中的更改。但是,如果运行大型站点修订可以增加您的WordPress数据库备份大小。
将此行代码添加到wp-config.php文件中,以限制为帖子存储的修订数。
define( "WP_POST_REVISIONS", 3 );
将3替换为要存储的修订数。 WordPress现在将自动丢弃旧版本。但是,您的旧版本修订版仍存储在您的数据库中。请参阅我们的教程,了解如何删除WordPress中的旧帖子修订版。
我们希望这篇文章能帮助您学习如何在WordPress中编辑wp-config.php文件以及您可以使用它做的所有很酷的事情。您可能还希望看到有关WordPress函数文件的25多个非常有用的技巧的文章。
评论被关闭。