【发布时间】:2014-06-03 22:12:14
【问题描述】:
我需要一种方法来访问 Wordpress 中的 wp-config.php 文件,并添加一些值。
坦率地说,我想添加这个当前值。
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);
但我想从我的插件中添加它们。是否有任何默认的 Wordpress 功能,或用于执行此操作的其他功能。
先谢谢了。
【问题讨论】:
我需要一种方法来访问 Wordpress 中的 wp-config.php 文件,并添加一些值。
坦率地说,我想添加这个当前值。
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);
但我想从我的插件中添加它们。是否有任何默认的 Wordpress 功能,或用于执行此操作的其他功能。
先谢谢了。
【问题讨论】:
插件Quick Cache 在激活时添加define('WP_CACHE', true);,并在停用时将其删除。这是其工作原理的简化版本。
激活时,它将<?php 替换为其代码<?php define(etc):
function wp_config_put( $slash = '' ) {
$config = file_get_contents (ABSPATH . "wp-config.php");
$config = preg_replace ("/^([\r\n\t ]*)(\<\?)(php)?/i", "<?php define('WP_CACHE', true);", $config);
file_put_contents (ABSPATH . $slash . "wp-config.php", $config);
}
if ( file_exists (ABSPATH . "wp-config.php") && is_writable (ABSPATH . "wp-config.php") ){
wp_config_put();
}
else if (file_exists (dirname (ABSPATH) . "/wp-config.php") && is_writable (dirname (ABSPATH) . "/wp-config.php")){
wp_config_put('/');
}
else {
add_warning('Error adding');
}
在停用时,它会使用不包含 <?php 的模式搜索其代码(如果我理解正确的话)并将其删除:
function wp_config_delete( $slash = '' ) {
$config = file_get_contents (ABSPATH . "wp-config.php");
$config = preg_replace ("/( ?)(define)( ?)(\()( ?)(['\"])WP_CACHE(['\"])( ?)(,)( ?)(0|1|true|false)( ?)(\))( ?);/i", "", $config);
file_put_contents (ABSPATH . $slash . "wp-config.php", $config);
}
if (file_exists (ABSPATH . "wp-config.php") && is_writable (ABSPATH . "wp-config.php")) {
wp_config_delete();
}
else if (file_exists (dirname (ABSPATH) . "/wp-config.php") && is_writable (dirname (ABSPATH) . "/wp-config.php")) {
wp_config_delete('/');
}
else if (file_exists (ABSPATH . "wp-config.php") && !is_writable (ABSPATH . "wp-config.php")) {
add_warning('Error removing');
}
else if (file_exists (dirname (ABSPATH) . "/wp-config.php") && !is_writable (dirname (ABSPATH) . "/wp-config.php")) {
add_warning('Error removing');
}
else {
add_warning('Error removing');
}
【讨论】: