Skip to content

Instantly share code, notes, and snippets.

@vdrover
Last active April 17, 2024 12:29
Show Gist options
  • Save vdrover/ebdbf65f5f4e9ee5545255e9c36bd422 to your computer and use it in GitHub Desktop.
Save vdrover/ebdbf65f5f4e9ee5545255e9c36bd422 to your computer and use it in GitHub Desktop.
Wordpress - Backdate scheduled posts
// Adds a meta box to the post editing screen for changing the post's publication
// date and time after it has initially been published.
// Only visible to administrators.
// Place in functions.php file.
function cdap_add_meta_box() {
// Check if the current user is an administrator
if (current_user_can('administrator')) {
add_meta_box(
'cdap_meta_box', // ID of the meta box
'Backdate Scheduled Posts', // Title of the meta box
'cdap_meta_box_callback', // Callback function
'post', // Post type
'side' // Context
);
}
}
add_action('add_meta_boxes', 'cdap_add_meta_box');
// Outputs the HTML for the meta box.
function cdap_meta_box_callback($post) {
// Add a nonce field so we can check for it later.
wp_nonce_field('cdap_save_meta_box_data', 'cdap_meta_box_nonce');
// Get the current value, if there is one.
$value = get_post_meta($post->ID, '_cdap_new_date', true);
// Output the datetime-local field
echo '<label for="cdap_new_date">New Publication Date and Time:</label> ';
echo '<input type="datetime-local" id="cdap_new_date" name="cdap_new_date" value="' . esc_attr($value ? date('Y-m-d\TH:i', strtotime($value)) : '') . '" style="width: 100%;">';
}
// Saves the custom date and time selected in the meta box.
function cdap_save_meta_box_data($post_id) {
// Check if our nonce is set.
if (!isset($_POST['cdap_meta_box_nonce'])) {
return;
}
// Verify that the nonce is valid.
if (!wp_verify_nonce($_POST['cdap_meta_box_nonce'], 'cdap_save_meta_box_data')) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Check the user's permissions.
if (isset($_POST['post_type']) && 'page' === $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id)) {
return;
}
} else {
if (!current_user_can('edit_post', $post_id)) {
return;
}
}
// Make sure that it is set.
if (isset($_POST['cdap_new_date'])) {
// Update the meta field in the database.
update_post_meta($post_id, '_cdap_new_date', sanitize_text_field($_POST['cdap_new_date']));
}
}
add_action('save_post', 'cdap_save_meta_box_data');
// Updates the post's publish date and time after it is published, based on the
// selected date and time in the meta box.
function cdap_update_post_date($new_status, $old_status, $post) {
if ('publish' === $new_status && 'publish' !== $old_status && $new_date = get_post_meta($post->ID, '_cdap_new_date', true)) {
// Check if the new date is set and valid
if (!empty($new_date)) {
$postarr = array(
'ID' => $post->ID,
'post_date' => $new_date,
'post_date_gmt' => get_gmt_from_date($new_date)
);
// Update the post
wp_update_post($postarr);
}
}
}
add_action('transition_post_status', 'cdap_update_post_date', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment