update
下面描述的会话选项似乎非常不安全!进一步的调查表明,request\\u filesystem\\u凭据返回的数组包含纯文本形式的凭据。因此,将其存储在会话中似乎是个坏主意。
注意:在发布表单时,通过非安全连接(http)发送凭据似乎也是个坏主意。最新的问题可以通过将凭据添加到wp\\u config来解决。php。执行此操作时,请确保chmod 600您的wp\\u配置。php(参见:http://codex.wordpress.org/Changing_File_Permissions).
在wp config中设置凭据。php示例:
define(\'FS_METHOD\', \'ftpext\');
define(\'FTP_HOST\', \'localhost\');
define(\'FTP_USER\', \'ftpuser\');
define(\'FTP_PASS\', \'ftpuser\');
define(\'FTP_BASE\', \'/home/username/http_docs/\');
完成此操作后,我们可以将它们(临时)保存到数据库中,以供定制者使用。这似乎会导致wp config中的设置。php也保存到数据库中。保存到数据库将使用
set_theme_mod
灵感来源:
https://stackoverflow.com/questions/14802251/hook-into-the-wordpress-theme-customizer-save-action/16679837完成所有这些之后,最后一段代码使我能够使用wp\\U文件系统将自定义程序设置存储在文件中:
function lesscustomize($setting)
{
$updatecss = WP_LESS_to_CSS::$instance;
$updatecss->wpless2csssavecss(unserialize(get_theme_mod(\'customizercredits\')));
}
add_action( \'customize_save_after\', \'lesscustomize\' );
function storecedits( $wp_customize ) {
$in = true;
$url = \'customize.php\';
if (false === ($creds = request_filesystem_credentials($url, \'\', false, false,null) ) ) {
$in = false;
exit;
}
if ($in && ! WP_Filesystem($creds) ) {
// our credentials were no good, ask the user for them again
request_filesystem_credentials($url, \'\', true, false,null);
$in = false;
exit;
}
set_theme_mod(\'customizercredits\', serialize($creds));
}
add_action(\'customize_controls_init\', \'storecedits\', 1);
----------------结束更新----------------------
我发现我可以通过会话从理论上解决这个问题。不幸的是,这带来了有关安全性的新问题。First将启用会话(可在此处找到https://stackoverflow.com/a/4769449/1596547) 造成其他麻烦吗?如果没有,在会话中存储文件凭据的风险是什么?
function kana_init_session()
{
session_start();
}
add_action(\'init\', \'kana_init_session\', 1);
function updatefiles( $wp_customize ) {
WP_Filesystem($_SESSION[\'creds\']);
global $wp_filesystem;
$contentdir = trailingslashit( $wp_filesystem->wp_content_dir() );
echo $contentdir;
$wp_filesystem->mkdir( $contentdir. \'cbe\' );
if ( ! $wp_filesystem->put_contents( $contentdir . \'cbe/test.txt\', \'Test file contents\', FS_CHMOD_FILE) )
{
echo "error saving file!";
}
}
add_action(\'customize_save\', \'updatefiles\', 1);
function storecredentials( $wp_customize )
{
$in = true;
$url = \'customize.php\';
if (false === ($creds = request_filesystem_credentials($url, \'\', false, false,null) ) ) {
$in = false;
exit;
}
if ($in && ! WP_Filesystem($creds) ) {
// our credentials were no good, ask the user for them again
request_filesystem_credentials($url, \'\', true, false,null);
$in = false;
exit;
}
$_SESSION[\'creds\'] = $creds;
}
add_action(\'customize_controls_init\', \'storecredentials\', 1);</strike>