我需要一种方法来访问 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');
}
关于wordpress - 从外部插件编辑 wp-config.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24025849/