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.

I need a way to automatically export all WordPress posts from a specific date and have it output the file on the server that can be downloaded daily.

The reason the XML format is needed is because the site is part of a large network of blogs and the parent site does not use WordPress but indexes the content in its search results. The parent company uses an XML parser that can't handle increments. It needs the full content of posts all at one time.

My approach is to create a cron job using wp_schedule_event that fires export_wp and outputs the buffers into a file. The problem is that the file created is empty.

My current code is:

register_activation_hook(__FILE__, 'c3m_my_activation');
add_action('c3m_export_daily', 'c3m_export_xml');

function c3m_my_activation() {
    wp_schedule_event(time(), 'daily', 'c3m_export_daily');
}

function c3m_export_xml() {
    $ob_file = fopen('server_path_to_my_file.xml','w');

    $args=array(
        'content' => 'posts',
        'start_date' => 'october 2008',
        'status' => 'published');

        function ob_file_callback($buffer)
        {
          global $ob_file;
          fwrite($ob_file,$buffer);
        }

    ob_start('ob_file_callback');
    export_wp($args);

    ob_end_flush();
}

I have also tried it without adding any $args to export_wp but the file is still empty. I'm hoping this can be accomplished with export_wp so the whole thing doesn't have to be written from scratch.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Your problem is that ob_file ain't global. You only define it in c3m_export_xml(). Setting global $ob_file in ob_file_callback() gives you an empty file handle. Try this instead:

function c3m_export_xml() {     
    $args=array(
        'content' => 'posts',
        'start_date' => 'october 2008',
        'status' => 'published');

    ob_start();
    export_wp($args);
    $xml = ob_get_clean();

    file_put_contents('server_path_to_my_file.xml', $xml);
}
share|improve this answer
    
Finally had a chance to rebuild the plugin. Works great. –  Chris_O Apr 5 '11 at 2:05
1  
Where would this function be used? Could I drop it in the functions.php file in the theme directory? I would love to use this code. –  Studioleland Feb 22 '12 at 18:59
    
You need to require it first, so you have the method available, see wordpress.stackexchange.com/questions/102056/…. –  Jasper Kennis Jun 6 '13 at 12:05

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.