想知道是否有人可以解释为什么作者在这段代码中使用ob\\u start()和ob\\u clean()在WordPress中构建导入/导出页面?
因为我想将此代码集成到另一个插件中,并且我想将代码分离到不同的文件中,所以我需要将ob\\u start()放在哪里才能工作?
<?php
/*
Plugin Name: I/E Option
Plugin URI: http://wp.tutsplus.com/
Description: This is a sample plugin with backup and restore options feature.
Author: Lee Pham
Version: 1.0
Author URI: https://twitter.com/leephamj
*/
function register_ie_option() {
ob_start();
add_menu_page(\'IE Option Page\', \'IE Option\', \'activate_plugins\', \'ie-option\', \'ie_option_page\', \'\');
add_submenu_page(\'ie-option\', \'Import\', \'Import\', \'activate_plugins\', \'ie-import-option\', \'ie_import_option_page\');
add_submenu_page(\'ie-option\', \'Export\', \'Export\', \'activate_plugins\', \'ie-export-option\', \'ie_export_option_page\');
}
/*
* Main page
*/
function ie_option_page() {
?>
<div class="wrap">
<div id="icon-tools" class="icon32"><br /></div>
<h2>Backup & Restore </h2>
<p>This is only a sample plugin that demonstrate how to create a simple import/export feature for your own templates or plugins.</p>
<p>Hope it useful for you!</p>
<p>Get in touch with me:</p>
<ul>
<li>E-mail: [email protected]</li>
<li>Twitter: <a href="http://twitter.com/leephamj">@leephamj</a></li>
<li>Facebook: <a href="http://facebook.com/tungchen93">Lee Pham</a></li>
</ul>
</div>
<?php
}
/*
* Import feature
*/
function ie_import_option_page() {
?>
<div class="wrap">
<div id="icon-tools" class="icon32"><br /></div>
<h2>Import</h2>
<?php
if (isset($_FILES[\'import\']) && check_admin_referer(\'ie-import\')) {
if ($_FILES[\'import\'][\'error\'] > 0)
wp_die("Error happens");
else {
$file_name = $_FILES[\'import\'][\'name\'];
$file_ext = strtolower(end(explode(".", $file_name)));
$file_size = $_FILES[\'import\'][\'size\'];
if (($file_ext == "json") && ($file_size < 500000)) {
$encode_options = file_get_contents($_FILES[\'import\'][\'tmp_name\']);
$options = json_decode($encode_options, true);
foreach ($options as $key => $value) {
update_option($key, $value);
}
echo "<div class=\'updated\'><p>All options are restored successfully.</p></div>";
}
else
echo "<div class=\'error\'><p>Invalid file or file size too big.</p></div>";
}
}
?>
<p>Click Browse button and choose a json file that you backup before.</p>
<p>Press Restore button, Wordpress do the rest for you.</p>
<form method=\'post\' enctype=\'multipart/form-data\'>
<p class="submit">
<?php wp_nonce_field(\'ie-import\'); ?>
<input type=\'file\' name=\'import\' />
<input type=\'submit\' name=\'submit\' value=\'Restore\'/>
</p>
</form>
</div>
<?php
}
/*
* Export feature
*/
function ie_export_option_page() {
if (!isset($_POST[\'export\'])) {
?>
<div class="wrap">
<div id="icon-tools" class="icon32"><br /></div>
<h2>Export</h2>
<p>When you click <tt>Backup all options</tt> button, system will generate a JSON file for you to save on your computer.</p>
<p>This backup file contains all configution and setting options on our website. Note that it do <b>NOT</b> contain posts, pages, or any relevant data, just your all options.</p>
<p>After exporting, you can either use the backup file to restore your settings on this site again or another Wordpress site.</p>
<form method=\'post\'>
<p class="submit">
<?php wp_nonce_field(\'ie-export\'); ?>
<input type=\'submit\' name=\'export\' value=\'Backup all options\'/>
</p>
</form>
</div>
<?php
} elseif (check_admin_referer(\'ie-export\')) {
$blogname = str_replace(" ", "", get_option(\'blogname\'));
$date = date("m-d-Y");
$json_name = $blogname."-".$date; // Namming the filename will be generated.
$options = get_alloptions(); // Get all options data, return array
foreach ($options as $key => $value) {
$value = maybe_unserialize($value);
$need_options[$key] = $value;
}
$json_file = json_encode($need_options); // Encode data into json data
ob_clean();
echo $json_file;
header("Content-Type: text/json; charset=" . get_option( \'blog_charset\'));
header("Content-Disposition: attachment; filename=$json_name.json");
exit();
}
}
add_action(\'admin_menu\', \'register_ie_option\');
最合适的回答,由SO网友:krembo99 整理而成
你应该阅读HERE 或HERE , HERE (还有很多other places ) 有关输出缓冲的详细信息。
如果您输出一些html或javascript代码,或者在php中使用echo或print,则当php脚本通过脚本进行处理时,这些信息将直接发送到chuncks中的浏览器。
但如果使用输出缓冲区,则该信息首先存储在服务器内部缓存中(像变量一样),直到脚本完成执行或对缓冲区执行某种操作为止。
在上述情况下(顺便说一句,我认为这是一个高级内容,但它偏离了主题),作者希望在输出中发送一些额外的标题,但它还有其他优点,例如处理速度更快,可以在整个页面上作为一个单元执行某些操作(如字符串操作)。
在正常输出(如打印或回显)后发送其他标题通常会产生错误。但使用输出缓冲将使其正常工作,