从外部插件编辑wp-config.php



我需要一个方法来访问Wordpress中的wp-config.php文件,并添加一些值。

为了更直接,我想把这些当前值加起来。

define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

但是我想从我的插件中添加它们。是否有默认的Wordpress函数,或者其他的东西来做这件事。

事先谢谢。

插件快速缓存在激活时添加define('WP_CACHE', true);并在停用时删除它。以下是其工作原理的简化版本。

激活时,用代码<?php define(etc)替换<?php:

function wp_config_put( $slash = '' ) {
    $config = file_get_contents (ABSPATH . "wp-config.php");
    $config = preg_replace ("/^([rnt ]*)(<?)(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');
}

最新更新