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.

In WordPress, are there any supported 'script plugins', Classes, and/or a WordPress function that will allow a plugin to use to export/import its own data from the database to a txt/json file? A way to assist in handling data for a back-up feature in a plugin.

Original the code I have worked on a localhost, but not when I went live, and for various reasons. Initially when this was posted, as Oli had pointed out in his answer, "You cannot do AJAX file uploads. They're not supported but you can fake it." How can I upload files asynchronously with jQuery?. Since then, some more answers have been added.

Most of the issues surface when using a form and an ajax function. Especially when a file ($_FILE) is involved. To explain, I'm trying to take the following route HTTP->JS->PHP(AJAX)->JS(AJAX)->PHP(iFrame) instead of HTTP->JS->PHP(iFrame) by canceling the form submit so that ajax can take over and then have an iFrame created and targeted (btw, is this a design flaw?).

Basically I'm in search of something, preferably WordPress supported (script/class/function), to handle data between JS to PHP and PHP to JS.

share|improve this question

1 Answer 1

The only native import/export facility is the one under Tools in the admin. And even that needs an extension (plugin) for importing from different formats.

It's pretty straightforward to build one yourself. Something along the lines of:

$options = get_my_options();
header( 'Content-disposition: attachment; filename=my_export.txt' );
header( 'Content-Type: text/plain' );
echo json_encode( $options );
exit;

And conversely:

if ( ! empty( $_FILES['my_import']['tmp_name'] ) ) {
   if ( $import = file_get_contents( $_FILES['my_import']['tmp_name'] ) ) {
       if ( $options =@ json_decode( $import ) ) {
           save_my_options( $options );
       }
   }
}
share|improve this answer

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.