Editing wp-config.php from external plugin

I need a way to access to the wp-config.php file in WordPress, and add some values.

To be straight, I want to add this current values.

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

But I want to add them from my plugin. Are there any default WordPress functions, or something else for doing this.

Thanks beforehand.

Related posts

Leave a Reply

1 comment

  1. The plugin Quick Cache adds define('WP_CACHE', true); when activated and removes it on deactivation. Here’s a simplified version of how it works.

    On activation, it replaces <?php with its code <?php define(etc):

    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');
    }
    

    On deactivation, it searches for its code using a pattern that does not includes <?php (if I’m understanding it correctly) and removes it:

    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');
    }