Problem: Create a cron job to export posts to a WordPress XML file on server

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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);
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x