Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

wondering if anyone could explain why the author used ob_start() and ob_clean() in this code to build an import/export page in WordPress?

Since I'd like to integrate this code into another plugin, and I want to separate the code into different files, where would I need to place ob_start() for this to work?

<?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: leephamj@gmail.com</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');
share|improve this question
    
What did the author of the plugin say when you asked? If you did not ask him, why? –  Tom J Nowell Mar 4 '13 at 10:40
add comment

1 Answer

up vote 1 down vote accepted

you should read HERE or HERE , HERE (and lots of other places ) more about output buffering .

If you output some html or javascript code or use echo or print within php, this information is being sent DIRECTLY to the browser in chuncks as the PHP script is processing through the script.

but If you use the output buffer , this information is first STORED on the server internal cache (Like a variable) until either the script has finished executing or you performed some kind of action on the buffer.

In the case above (which, BTW - I believe is a premium content , but it is off the point ) The author wanted to send some additional HEADERS with the output ,but it also has other advantages like a faster processing and the possibility to perform certain operations (like string operations) on the WHOLE page as one unit .

Sending additional headers AFTER a normal output (like print or echo) will normally produce an error . But using output Buffering will make it work just fine,

share|improve this answer
    
Thanks, this is not premium content, its a tutorial freely available online, and this code is actually from a GPL plugin from the WordPress plugin repository. My question is if I implement that code within another plugin, the add_submenu calls are in a separate file than the rest of this code, and if the 'Import/Export' page is not accessed there will be an error since the output buffer will have been started but not closed. How do I go around that? –  drtanz Mar 4 '13 at 4:06
    
That will greatly depend on how you structure your own plugin code and its internal filesystem, But generally speaking , OB can work across multiple files. (you can also use include) –  krembo99 Mar 4 '13 at 4:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.