Created
April 26, 2013 11:44
-
-
Save robinnorth/5466824 to your computer and use it in GitHub Desktop.
WordPress: Simple, configurable script to export post data to a CSV file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Export WordPress post data to CSV | |
* Based on <http://stackoverflow.com/a/3474698> and <http://ran.ge/2009/10/27/howto-create-stream-csv-php/> | |
*/ | |
/** | |
********************************************************************* | |
* Configuration | |
********************************************************************* | |
*/ | |
/** | |
* Path to WordPress installation root | |
* Default path './' | |
*/ | |
// define( 'PATH_TO_WORDPRESS', './' ); # Default WordPress install location | |
define( 'PATH_TO_WORDPRESS', './wordpress/' ); # Default WordPress sub-directory install location | |
/** | |
* Export fields: array of strings representing $post properties to output to CSV. | |
*/ | |
$export_fields = array( | |
'post_title', | |
'post_date' | |
); | |
/** | |
* Export query parameters for WP_Query | |
* @link http://codex.wordpress.org/Function_Reference/WP_Query WP_Query parameter reference | |
*/ | |
$export_query = array( | |
'posts_per_page' => -1, | |
'post_status' => 'publish', | |
'post_type' => 'any', | |
); | |
/** | |
********************************************************************* | |
* Data export | |
********************************************************************* | |
*/ | |
// Require WordPress environment | |
require PATH_TO_WORDPRESS . 'wp-load.php'; | |
// Posts query | |
$posts = new WP_Query( $export_query ); | |
$posts = $posts->posts; | |
// Output file stream | |
$output_filename = 'export_' . strftime( '%Y-%m-%d' ) . '.csv'; | |
$output_handle = @fopen( 'php://output', 'w' ); | |
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); | |
header( 'Content-Description: File Transfer' ); | |
header( 'Content-type: text/csv' ); | |
header( 'Content-Disposition: attachment; filename=' . $output_filename ); | |
header( 'Expires: 0' ); | |
header( 'Pragma: public' ); | |
// Output data to file | |
foreach ( $posts as $post ) { | |
// Get post permalink | |
switch ( $post->post_type ) { | |
case 'revision': | |
case 'nav_menu_item': | |
break; | |
case 'page': | |
$permalink = get_page_link( $post->ID ); | |
break; | |
case 'post': | |
$permalink = get_permalink( $post->ID ); | |
break; | |
case 'attachment': | |
$permalink = get_attachment_link( $post->ID ); | |
break; | |
default: | |
$permalink = get_post_permalink( $post->ID ); | |
break; | |
} | |
// Build export array | |
$post_export = array( $permalink ); | |
foreach ( $export_fields as $export_field ) { | |
$post_export[] = $post->$export_field; | |
} | |
// Add row to file | |
fputcsv( $output_handle, $post_export ); | |
} | |
// Close output file stream | |
fclose( $output_handle ); | |
// We're done! | |
exit; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I'm trying to do the same thing but calling this function from a button that is posted inside a plugin. Do you know if is it possible to export this data from a plugin inside the admin panel?
Right now I'm able to show the data of the table in the plugin but is doesn't export anything (like I show you in the picture). As I said I'm using the same code as you in a function that is written in a plugin that shows the data in a table and has a button to export it.