Last active
May 14, 2023 13:24
-
-
Save birgire/296dbaf0d75ff7ae9cd6 to your computer and use it in GitHub Desktop.
WordPress Plugin: Enhance the native XML export - with a single day selection
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 | |
/** | |
* Plugin Name: Single Day Posts Export | |
* Plugin URL: http://wordpress.stackexchange.com/a/199527/26350 | |
* Plugin Author: Birgir Erlendsson (birgire) | |
* Version: 0.0.1 | |
*/ | |
/** | |
* Add our extra HTML | |
*/ | |
add_action( 'export_filters', function() { | |
?> | |
<p> | |
<ul id="wpse-post-filters" class="wpse-export-filters"> | |
<li> | |
<label><?php _e( 'Export posts from a single day:' ); ?></label> | |
<select name="wpse_single_day"> | |
<option value="0"><?php _e( 'Select day' ); ?></option> | |
<?php wpse_export_single_day_options(); ?> | |
</select> | |
</li> | |
</ul> | |
</p> | |
<?php | |
}); | |
/** | |
* Modification of the core export_date_options() function | |
*/ | |
function wpse_export_single_day_options( $post_type = 'post' ) { | |
global $wpdb, $wp_locale; | |
$months = $wpdb->get_results( | |
$wpdb->prepare( | |
"SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month, Day( post_date ) as day | |
FROM {$wpdb->posts} | |
WHERE post_type = %s AND post_status != 'auto-draft' | |
ORDER BY post_date DESC", | |
$post_type | |
) | |
); | |
$month_count = count( $months ); | |
if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) ) | |
return; | |
foreach ( $months as $date ) | |
{ | |
if ( 0 == $date->year ) | |
continue; | |
$month = zeroise( $date->month, 2 ); | |
printf( | |
'<option value="%d-%d-%d">%d. %s %d</option>', | |
$date->year, | |
$month, | |
$date->day, | |
$date->day, | |
$wp_locale->get_month( $month ), | |
$date->year | |
); | |
} | |
} | |
/** | |
* Append our HTML to the post filter section | |
*/ | |
add_action( 'admin_head', function(){ | |
?> | |
<script> | |
jQuery(document).ready(function($){ | |
$('#wpse-post-filters').appendTo( $('#post-filters') ); | |
}); | |
</script> | |
<?php | |
}); | |
/** | |
* Modify the database queries, indirectly | |
*/ | |
add_filter( 'export_args', function( $args ) | |
{ | |
// User input | |
$date = filter_input( INPUT_GET, 'wpse_single_day' ); | |
// Let's use DateTime to validate the Y-m-d input, | |
// See here http://stackoverflow.com/a/13194441/2078474 | |
$dt = DateTime::createFromFormat( 'Y-m-d', $date ); | |
// Check if the user input is a valid date: | |
if( method_exists( $dt, 'format' ) && $Ymd = $dt->format( 'Y-m-d' ) ) | |
{ | |
// The from date for the db query: | |
$args['start_date'] = $Ymd; | |
// I think we can modify the end date, in the db query, with this little trick | |
$args['end_date'] = date( | |
'Y-m-d', | |
strtotime( | |
'-1 month', | |
strtotime( | |
'+1 day', | |
strtotime( $Ymd ) | |
) | |
) | |
); | |
} // end if | |
return $args; | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to Include feature images also in export with this script?