增强本机XML导出—通过一天的选择,这里有一种方法可以使用选择框修改本机导出,包括所有可用的日期:
这里我们假设没有“巨大”的天数可供选择;-)否则,我们可以使用不同类型的用户界面。
步骤#1
首先,我们可以使用
export_filters
挂钩:
/**
* Add our extra HTML
*/
add_action( \'export_filters\', function() {
?>
<p><ul id="wpse-post-filters" class="wpse-export-filters">
<li>
<label><?php _e( \'Single day:\' ); ?></label>
<select name="wpse_single_day">
<option value="0"><?php _e( \'Select a day\' ); ?></option>
<?php wpse_export_single_day_options(); ?>
</select>
</li>
</ul></p>
<?php
});
下一步,我们需要构建
wpse_export_single_day_options()
作用我们可以简单地使用
export_date_options()
核心功能:
/**
* 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
);
}
}
步骤3要将HTML放在后过滤器部分,我们必须使用jQuery将其移动到正确的位置:
/**
* Append our HTML to the post filter section
*/
add_action( \'admin_head\', function()
{?>
<script type="text/javascript">
jQuery(document).ready(function($){
$(\'#wpse-post-filters\').appendTo( $(\'#post-filters\') );
});
</script>
<?php });
然后我们必须处理数据库查询。没有明确的过滤器,但我们可以使用
export_args
过滤,有一个小技巧:
/**
* 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 )
)
)
);
}
return $args;
});
演示插件我已经添加了整个插件
here on GitHub.