2014-09-02 55 views
1

我正在開發一個在本地安裝WordPress副本的應用程序。在我正在開發的應用程序中,我希望它替換wp-config-sample.php中的database_name_here,以便它具有正確的名稱以及用戶名和密碼。當我將該文件複製到wp-config.php時,信息是正確的。使用WordPress模板從應用程序生成PHP文件

我用

$test = htmlspecialchars(file_get_contents($wp_config_sample_file)) 

要獲得wp-config-sample.php文件的內容,並試圖用

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

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

,並沒有更改。我也嘗試過str_replace,它可以工作,但它似乎忽略了在<?php ?>標籤之間出現的任何匹配。

因此,在總結,我想從wp-config-sample.php創建wp-config.php編程文件包含的主機名,用戶名和密碼,而不是我進去的文件結構,並直接編輯該文件之前,我可以開始開發。我的應用程序是爲了開發目的在本地管理多個WordPress安裝。

回答

0

我發現瞭如何更換基於關聯數組在一根繩子上的物品this example,這是相當簡單:

<?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)); 

可能感興趣的:How to Create a Custom WordPress Install Package?

+0

謝謝你,偉大的工作...... – Martin7jd 2014-09-02 20:51:07