Generating a PHP file from an application using a WordPress template

I am developing an application that installs a copy of WordPress locally. In the application I’m developing I want it to replace the database_name_here in the wp-config-sample.php, so that it has the correct name as well as the username and password. And when I copy the file to wp-config.php the info is correct.

I have used

Read More
$test = htmlspecialchars(file_get_contents($wp_config_sample_file))

To get the contents of the wp-config-sample.php file and tried to use

$var = array(
  'database_name_here' => 'fred',
  );

$wp_config = strstr($test, $var); 

and no changes are made. I have also tried str_replace which will work but it seems to ignore any matches that appear between the <?php ?> tags.

So in summary, I’m trying to create a wp-config.php file from wp-config-sample.php programatically that contains the hostname, username and password instead of me going into the file structure and editing the file directly before I can start developing. My application is to manage multiple WordPress installations locally for development purposes.

Related posts

Leave a Reply

1 comment

  1. I found this example on how to replace items on a string based on an associative array, it’s quite simple:

    <?php
    $string = htmlspecialchars( file_get_contents( 'wp-config-sample.php' ) );
    
    $replacements = array(
        'database_name_here' => 'dbname',
        'username_here'      => 'username',
        'password_here'      => 'password'
    );
    
    $wp_config = str_replace( array_keys( $replacements ), $replacements, $string );
    
    printf( '<pre><code>%s</code></pre>', print_r( $wp_config, true ) );
    

    Maybe of interest: How to Create a Custom WordPress Install Package?