我爲我的WordPress網站編寫了一個自定義插件,該插件依賴於從插件文件夾中的xml數據文件讀取/寫入。當我測試這個用於文件讀/寫的標準PHP代碼時,它將允許我創建/寫入位於wp-admin/level的文件,但不能在插件文件夾中創建文件,儘管它可以從兩者讀取。從我的WordPress插件寫入文件
$file = 'test.xml'; (Can write to this file)
$file = plugins_url()."/my-plugin/test.xml"; (Can read but not write to this file)
// Open the file to get existing content
$current = file_get_contents($file);
echo $current;
// Append a new person to the file
$current .= "<person>John Smith</person>\n";
// Write the contents back to the file
file_put_contents($file, $current);
我得到以下調試錯誤:
Warning: file_put_contents(http://localhost/wp_mysite/wp-content/plugins/my-plugin/test.xml) [function.file-put-contents]: failed to open stream: HTTP wrapper does not support writeable connections in /Applications/MAMP/htdocs/wp_mysite/wp-content/plugins/my-plugin/my-plugin.php on line 53
我目前正在運行的這掀起了本地MAMP服務器,而是想辦法解決,讓我打包和發佈任何WordPress的服務器上的插件。什麼是正確的方法?
謝謝,
@hakre任何想法,如果類似的東西存在的主題?像'WP_THEME_DIR'> – Blowsie
@Blowsie:沒有這樣的常量,但類似的東西。看到這裏的「大圖片」:http://codex.wordpress.org/Determining_Plugin_and_Content_Directories – hakre