Skip to content

Instantly share code, notes, and snippets.

@bennettmcelwee
Created July 27, 2015 04:05
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 bennettmcelwee/7f46ef9f22544022a772 to your computer and use it in GitHub Desktop.
Save bennettmcelwee/7f46ef9f22544022a772 to your computer and use it in GitHub Desktop.
WordPress plugin that adds a Quick Date Picker to the New/Edit Post screen
<?php
/**
* Add a popup menu to the post screen that allows you to quickly choose a date within the last two weeks.
*
* @wordpress-plugin
* Plugin Name: Date Quick Pick
* Plugin URI: http://thunderguy.com/bennett/
* Description: Quickly choose a post date for posts.
* Version: 1.0.0
* Author: Bennett McElwee
* Author URI: http://thunderguy.com/bennett/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
add_action('post_submitbox_misc_actions', function () {
echo <<<'EOD'
<script>
(function($) {
$(function() {
var dayNames = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];
var monthNames = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
$(".edit-timestamp").after("<div><select class='day-picker'><option>Quick pick...</option></select></div>");
var sysTime = new Date().getTime();
for (var i = 0; i < 15; ++i) {
var date = new Date(sysTime - i * 24 * 60 * 60 * 1000);
var day = date.getDate();
var month = date.getMonth() + 1; if (month < 10) { month = "0" + month; }
var year = date.getFullYear();
var text = dayNames[date.getDay()] + " " + day + " " + monthNames[date.getMonth()] + " " + year;
if (i == 0) { text = text + " - today"; }
else if (i == 1) { text = text + " - yesterday"; }
$(".day-picker").append("<option value='"+year+"-"+month+"-"+day+"'>"+text+"</option>")
}
$(".day-picker").change(function() {
var date = $(this).val().split("-");
$("#aa").val(date[0]);
$("#mm").val(date[1]);
$("#jj").val(date[2]);
$(this).val("");
$(".save-timestamp").click();
});
});
})(window.jQuery);
</script>
EOD;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment