Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mam4dali/4e517f62a4691b5ee3e8c14413dfbfe9 to your computer and use it in GitHub Desktop.
Save mam4dali/4e517f62a4691b5ee3e8c14413dfbfe9 to your computer and use it in GitHub Desktop.
<?php
add_action( 'post_submitbox_misc_actions', 'without_update_post_modified_datetime' );
function without_update_post_modified_datetime($post)
{
echo '<div class="misc-pub-section misc-pub-section-last"><span><label><input type="checkbox" value="yes" name="without_update_post_modified_datetime" /> Stop updating post modified date</label></span></div>';
echo '<div class="misc-pub-section misc-pub-section-last"><span><label>Last modified: '.get_the_modified_time('Y/m/d H:i:s', $post).'</label></span></div>';
}
function without_update_post_modified_datetime_save_postdata($data, $postarr)
{
$postid = (!empty($postarr['ID']) && intval($postarr['ID'] > 0) ? intval($postarr['ID']) : (!empty($data['ID']) && intval($data['ID']) > 0 ? intval($data['ID']) : null));
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return $data;
if(empty($postid) || get_post_type($postid) != 'post' ) return $data;
if ( !current_user_can( 'edit_page', $postid ) ) return $data;
if(sanitize_text_field($_POST['action']) === 'editpost'
&& isset($_POST['without_update_post_modified_datetime'])
&& sanitize_text_field($_POST['without_update_post_modified_datetime']) === 'yes'){
$old_data = get_post($postid);
if(!is_wp_error($old_data)){
$old_post_modified = $old_data->post_modified;
$old_post_modified_gmt = $old_data->post_modified_gmt;
if(!empty($old_post_modified) && !empty($old_post_modified_gmt)){
$data['post_modified'] = $old_post_modified;
$data['post_modified_gmt'] = $old_post_modified_gmt;
}
}
}
return $data;
}
add_action('wp_insert_post_data', 'without_update_post_modified_datetime_save_postdata', '99', 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment