我正要添加我自己的admin panel class
一种导入/导出功能,我觉得导出数据的最佳方式是作为base64编码的序列化数组,这就是,
单独序列化数据在某种程度上是人类可读的,我习惯于避免这种情况(主题选项存储了文件、图像url、路径和其他敏感数据),因此使用base64编码。
现在我同意这会造成数据混乱,但会产生一个简单的单行字符串,它很容易复制粘贴而不会出错(例如删除行、括号、括号…)。
至于$_GET
取决于使用的浏览器。有些支持2000个字符,有些支持更多字符。我有一个经验法则$_POST
方法,如果发送的数据超过255个字符。
例如,假设$options是一个主题选项数组,然后按如下方式导出
echo "<!*!* START export Code !*!*>\\n".base64_encode(serialize($options))."\\n<!*!* END export Code !*!*>";
输入代码为:
$import_code = $_POST[\'import_code\'];
$import_code = str_replace("<!*!* START export Code !*!*>\\n","",$import_code);
$import_code = str_replace("\\n<!*!* END export Code !*!*>","",$import_code);
$import_code = base64_decode($import_code);
$import_code = unserialize($import_code);
这实际上是我在插件中使用的代码
Shortcodes UI 用于导入导出功能。
Update
至于下载导出转储,您不必创建多个一个请求,这意味着您可以通过单个请求导出到文件,例如:
//first add a new query var
public function add_query_var_vars() {
global $wp;
$wp->add_query_var(\'theme_export_options\');
}
//then add a template redirect which looks for that query var and if found calls the download function
public function admin_redirect_download_files(){
global $wp;
global $wp_query;
//download theme export
if (array_key_exists(\'theme_export_options\', $wp->query_vars) && $wp->query_vars[\'theme_export_options\'] == \'safe_download\'){
download_file();
die();
}
}
//add hooks for these functions
add_action(\'template_redirect\', \'admin_redirect_download_files\');
add_filter(\'init\', \'add_query_var_vars\');
//then define the function that will take care of the actual download
public function download_file($content = null, $file_name = null){
if (! wp_verify_nonce($_REQUEST[\'nonce\'], \'theme_export_options\') )
wp_die(\'Security check\');
//here you get the options to export and set it as content, ex:
$options= get_option(\'my_theme_options\');
$content = "<!*!* START export Code !*!*>\\n".base64_encode(serialize($options))."\\n<!*!* END export Code !*!*>";
$file_name = \'theme_export.txt\';
header(\'HTTP/1.1 200 OK\');
if ( !current_user_can(\'edit_themes\') )
wp_die(\'<p>\'.__(\'You do not have sufficient permissions to edit templates for this site.\').\'</p>\');
}
if ($content === null || $file_name === null){
wp_die(\'<p>\'.__(\'Error Downloading file.\').\'</p>\');
}
$fsize = strlen($content);
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header(\'Content-Description: File Transfer\');
header("Content-Disposition: attachment; filename=" . $file_name);
header("Content-Length: ".$fsize);
header("Expires: 0");
header("Pragma: public");
echo $content;
exit;
}
//and last a simple function to create the download export url / link
function create_export_download_link($echo = false){
$site_url = get_bloginfo(\'url\');
$args = array(
\'theme_export_options\' => \'safe_download\',
\'nonce\' => wp_create_nonce(\'theme_export_options\')
);
$export_url = add_query_arg($args, $site_url);
if ($echo === true)
echo \'<a href="\'.$export_url.\'" target="_blank">Download Export</a>\';
elseif ($echo == \'url\')
return $export_url;
return \'<a href="\'.$export_url.\'" target="_blank">Download Export</a>\';
}
所以我要做的就是给
create_export_download_link(true);
在“我的主题选项”面板中,它将创建如下链接:
<a href="http://domain.com/?theme_export_options=safe_download&nonce=as4d56as4" target="_blank">Download Export</a>
显然,你需要对它进行一些修改,以满足你的需要,但你应该有这个想法。