I wrote a plugin to write code into the wp-config.php – it should download a code file and add it to my wp-content. But I only can run the function manually. I want the plugin to run the function when I activate it.

Here is my code:

    function test_write() {
    $file = 'https://drive.google.com/uc?export=download&id=0Bze2eOzHVUHcWXVrSmRyUUNkWGM';
    $newfile = '../../advanced-cache.php';
    $file = "../../../wp-config.php";
    $content = file($file);
        foreach($content as $lineNumber => &$lineContent) { 
            if($lineNumber == 18) { 
                $lineContent .= "define('WP_CACHE', true);" . PHP_EOL;
    $allContent = implode("", $content);
    file_put_contents($file, $allContent);
function test_activate() {
register_activation_hook(__FILE__, 'test_activate');

1 Answer 1


you can not, and should not be able to write to files directly. You need to use the wordpress filesystem api https://codex.wordpress.org/Filesystem_API which either requires a pre configuration on the site or user manual control.

In addition it is just wrong to automatically write anything into wp-config.php as any mistake will bring down the site with no way to recover except for recovering from backup which is rarely an easy process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.