Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Created August 2, 2024 15:14
Show Gist options
  • Save GaryJones/367f6c0963710fd79f20d58bbb1ef7ff to your computer and use it in GitHub Desktop.
Save GaryJones/367f6c0963710fd79f20d58bbb1ef7ff to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Day-of-the-Month Filter
* Description: Adds a day-of-the-month filter to the posts list table in wp-admin.
* Version: 1.0.0
* Author: Gary Jones
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_action( 'restrict_manage_posts', 'gmj_day_of_month_filter' );
/**
* Add custom day-of-the-month filter to the posts list table.
*/
function gmj_day_of_month_filter() {
global $wp_locale;
// Ensure this is the posts list table.
if ( ! function_exists( 'get_current_screen' ) || 'edit-post' !== get_current_screen()->id ) {
return;
}
// Get selected day from query string.
$selected_day = isset( $_GET['day'] ) ? intval( $_GET['day'] ) : '';
// Output day-of-the-month filter HTML.
?>
<label class="screen-reader-text" for="day_of_month"><?php esc_html_e( 'Day', 'day-of-the-month-filter' ); ?></label>
<select name="day" id="day_of_month">
<option value=""><?php esc_html_e( 'All Days', 'day-of-the-month-filter' ); ?></option>
<?php
// Generate options for all 31 days.
for ( $day = 1; $day <= 31; $day++ ) {
printf(
'<option value="%s" %s>%s</option>',
$day,
selected( $day, $selected_day, false ),
$day
);
}
?>
</select>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment