Skip to content

Instantly share code, notes, and snippets.

@shaharhesse
Last active February 18, 2016 15:12
Show Gist options
  • Save shaharhesse/647ded2fbae284cd1aa9 to your computer and use it in GitHub Desktop.
Save shaharhesse/647ded2fbae284cd1aa9 to your computer and use it in GitHub Desktop.
Add a 'minor edit' checkbox to Wordpress posts, pages and custom posts to save without updating the modified date
<?php
/*
* Add a 'minor edit' checkbox to Wordpress posts, pages and custom posts
* to save without updating the 'modified' date of the post.
* Saved as postmeta for each post
*/
/* add checkbox to Publish box
--------------------------------------------------------------------------*/
add_action( 'post_submitbox_misc_actions', 'minor_edit_option' );
function minor_edit_option(){
global $post;
$val = get_post_meta( $post -> ID, 'minor_edit', true );
wp_nonce_field( basename( __FILE__ ), 'minor_edit_nonce' );
echo '<div class="misc-pub-section">';
echo '<p>';
echo '<input type="hidden" name="minor_edit" value="n" />';
echo '<input type="checkbox" id="minor_edit" name="minor_edit" value="y" ' . checked( $val, 'y', false ) . ' /> ';
echo '<label for="minor_edit"><strong>Minor Edit</strong></label>';
echo '<p>';
echo '<p>';
echo '<small>Check to leave the \'modified\' date unaltered</small>';
echo '<p>';
echo '</div>';
}
/* On save, update minor edit option
--------------------------------------------------------------------------*/
add_action( 'save_post', 'minor_edit_save_postmeta', 10, 3 );
function minor_edit_save_postmeta( $post_id, $post_data, $action ){
if ( !isset( $_POST['minor_edit_nonce'] ) || !wp_verify_nonce( $_POST['minor_edit_nonce'], basename( __FILE__ ) ) ):
return;
endif;
$val = isset( $_POST['minor_edit'] ) ? $_POST['minor_edit'] : 'n';
update_post_meta( $post_id, 'minor_edit', $val );
}
/* Keep previous modified date for post
--------------------------------------------------------------------------*/
add_action( 'post_updated', 'minor_edit', 10, 3 );
function minor_edit( $post_id, $post_after, $post_before ){
global $wpdb;
$minor_edit = get_post_meta( $post_id, 'minor_edit', true );
if( $minor_edit == 'y' ):
$wpdb -> update(
$wpdb -> prefix . 'posts',
array(
'post_modified' => $post_before -> post_modified,
'post_modified_gmt' => $post_before -> post_modified_gmt
),
array( 'ID' => $post_id ),
array(
'%s',
'%s'
),
array( '%d' )
);
endif;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment