我写了一个插件(https://github.com/bassjobsen/custom-bootstrap-editor) 此插件将样式表写入wp-content/uploads/cbe
.
我将使用下面的代码来执行此操作:
$upload_dir = wp_upload_dir();
$this->folder = trailingslashit($upload_dir[\'basedir\']).\'cbe/\';
if( !is_dir( $this->folder ) ) wp_mkdir_p( $this->folder );
if ( is_writable( $this->folder ) ){
file_put_contents( $this->folder.$this->filename, $css);
}
关于上面的第一个问题,检查wp\\u upload\\u dir()是否可写的最佳位置是什么?是否会对此进行标准检查(加上错误)?阅读后http://ottopress.com/2011/tutorial-using-the-wp_filesystem/ 我还有其他一些问题。
上面提到的帖子告诉我应该使用文件系统API来替换上面的代码。这个观察结果还会是真的吗?
request_filesystem_credentials
似乎工作正常,但每次文件更改都要登录似乎有些过火。我的插件可以跳过登录屏幕吗?
虽然信用卡似乎有效$wp_filesystem->mkdir( $folder )
和$wp_filesystem->put_contents( $folder.\'/\'.$filename, $css, FS_CHMOD_FILE)
似乎总是返回false。如何调试文件系统API调用?
update感谢@otto file writing works now:
if (isset($_POST[\'SaveCBESettings\'])) {
if ( !empty($_POST) && check_admin_referer( \'cbe-nonce\') )
{
$SaveCBESettings = 1;
$in = true;
$url = wp_nonce_url(\'options-general.php?page=filewriting\',\'cbe-nonce\');
if (false === ($creds = request_filesystem_credentials($url, \'\', false, false, array(\'SaveCBESettings\')) ) ) {
$in = false;
}
if ($in && ! WP_Filesystem($creds) ) {
// our credentials were no good, ask the user for them again
request_filesystem_credentials($url, \'\', true, false,array(\'SaveCBESettings\'));
$in = false;
}
if($in)
{
// by this point, the $wp_filesystem global should be working, so let\'s use it to create a file
global $wp_filesystem;
$contentdir = trailingslashit( $wp_filesystem->wp_content_dir() );
$wp_filesystem->mkdir( $contentdir. \'cbe\' );
if ( ! $wp_filesystem->put_contents( $contentdir . \'cbe/test.txt\', \'Test file contents\', FS_CHMOD_FILE) )
{
echo "error saving file!";
}
unset($_POST);
}
}
}