Skip to content

Instantly share code, notes, and snippets.

@mattkatz
Created June 20, 2012 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattkatz/2959178 to your computer and use it in GitHub Desktop.
Save mattkatz/2959178 to your computer and use it in GitHub Desktop.
Exporting data from wordpress.
<?php
// Set the headers so the file downloads
header('Content-type: application/xml+opml');
header('Content-Disposition: attachment; filename="Wordprss-OPML-Export.xml"');
//oh PHP. Why you gotta be so difficult about escaping things?
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
?>
<opml version="1.0" >
<head>
<dateCreated>
<?php echo date("r", time())?>
</dateCreated>
<title>
WordpRSS OPML Feed Export
</title>
</head>
<body>
<?php
//this file requires $wpdb and the like.
require_once 'backend.php';
// this should take in a param.
// the param should be a userid.
// if the userid == current user, we export everything but private
// otherwise, just export the public stuff.
$feeds = WprssFeeds::get();
foreach($feeds as $feed){
?>
<outline text="<?php echo $feed->feed_name?>" title="<?php echo $feed->feed_name?>" type="rss" xmlUrl="<?php echo $feed->feed_url?>" htmlUrl="<?php echo $feed->site_url?>"/>
<?php
}
?>
</body>
</opml>
<?php
exit;
?>
//in the main plugin file
//We want to make a query variable called export_opml
add_filter('query_vars','plugin_add_trigger');
function plugin_add_trigger($vars) {
$vars[] = 'export_opml';
return $vars;
}
//and if we see that query variable, we include the file we want to send
add_action('template_redirect', 'plugin_trigger_check');
function plugin_trigger_check() {
if(intval(get_query_var('export_opml')) == wp_get_current_user()->ID) {
require_once 'export_opml.php';
//exit, because we don't want to send any of the rest of WP - no menus, posts, etc.
exit;
}
}
<a href="<?echo site_url() ?>?export_opml=<?php echo wp_get_current_user()->ID ?>">Export OPML</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment