Skip to content

Instantly share code, notes, and snippets.

@bhubbard
Created August 10, 2023 20:26
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 bhubbard/7f041210984d9e4f775665e93f74f261 to your computer and use it in GitHub Desktop.
Save bhubbard/7f041210984d9e4f775665e93f74f261 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Custom Unpublish Tool
Description: Add an Unpublish tool with a date picker in the Gutenberg editor.
*/
// Enqueue the date picker script
function custom_unpublish_enqueue_scripts() {
wp_enqueue_script('jquery-ui-datepicker');
}
add_action('enqueue_block_editor_assets', 'custom_unpublish_enqueue_scripts');
// Add custom Unpublish tool
function custom_unpublish_tool() {
$post = get_post();
$unpublish_date = get_post_meta($post->ID, '_unpublish_date', true);
?>
<div class="unpublish-tool">
<label for="unpublish-date">Unpublish Date:</label>
<input type="text" id="unpublish-date" name="unpublish-date" value="<?php echo esc_attr($unpublish_date); ?>" />
</div>
<script>
jQuery(document).ready(function($) {
$('#unpublish-date').datepicker({
dateFormat: 'yy-mm-dd',
changeMonth: true,
changeYear: true
});
});
</script>
<?php
}
add_action('admin_enqueue_scripts', 'custom_unpublish_enqueue_scripts');
add_action('edit_form_after_title', 'custom_unpublish_tool');
// Save Unpublish date
function custom_save_unpublish_date($post_id) {
if (isset($_POST['unpublish-date'])) {
update_post_meta($post_id, '_unpublish_date', sanitize_text_field($_POST['unpublish-date']));
}
}
add_action('save_post', 'custom_save_unpublish_date');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment