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

标签 wordpress ssl plugins

我需要一种方法来访问 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/

相关文章:

MySQL查询不是什么

php - WordPress 全 Angular 页脚

c# - 使用 .NET sslstream 了解服务器/代理/客户端证书

wordpress - 第一个 child :first-letter not working in wordpress

css - 主导航分隔线查询

haskell - 如何向 http-client-tls 提供客户端证书?

.htaccess 子域的通配符 SSL

javascript - 在 Moodle 插件中使用外部 JavaScript 库的函数

version-control - Maven Perforce 插件突然需要密码?

c# - 接口(interface) C# 中的可选空隙