Skip to content

Instantly share code, notes, and snippets.

@bibicadotnet
Last active July 14, 2025 03:02
Show Gist options
  • Select an option

  • Save bibicadotnet/c42ffd2b9577f26ea83619ce32d4e57e to your computer and use it in GitHub Desktop.

Select an option

Save bibicadotnet/c42ffd2b9577f26ea83619ce32d4e57e to your computer and use it in GitHub Desktop.
Nâng cấp Silent Update
<?php
/**
* SilentUpdate WordPress Plugin
*
* @package SilentUpdate
* @author Tomasz Dobrzyński
* @link http://tomasz-dobrzynski.com
* @copyright Copyright © 2015 Tomasz Dobrzyński
*
* Plugin Name: Silent Update
* Plugin URI: http://tomasz-dobrzynski.com/wordpress-silent-update
* Description: Control whether you want to update modification date when updating a post.
* Version: 1.0.0
* Author: Tomasz Dobrzyński
* Author URI: http://tomasz-dobrzynski.com
* Domain Path: /languages/
* Tested up to: 4.2.2
*/
function silent_update_call() {
new SilentUpdate();
}
/**
* Only load when editing posts in the admin area
*/
if ( is_admin() ) {
add_action( 'load-post.php', 'silent_update_call' );
add_action( 'load-post-new.php', 'silent_update_call' );
}
class SilentUpdate {
function __construct() {
load_plugin_textdomain( 'silent-update', false, basename( dirname( __FILE__ ) ) . '/languages' );
// Add to the Publish meta box
add_action( 'post_submitbox_misc_actions', array( $this, 'render_meta_box_content' ), 99 );
add_filter( 'wp_insert_post_data', array( $this, 'filter_handler' ), 99, 2 );
}
/**
* Add content to the Publish meta box.
*/
public function render_meta_box_content() {
global $post;
wp_nonce_field( 'silent_update_custom_box', 'silent_update_custom_box_nonce' );
$datef = __( 'M j, Y @ G:i', 'silent-update' );
$date = date_i18n( $datef, get_post_modified_time( 'U', false, $post ) );
// Checkbox default is checked
$checked = checked( true, true, false );
echo '<div class="misc-pub-section">
<input type="checkbox" id="modification_date" name="modification_date" value="1" ' . $checked . ' />
<label for="modification_date">' . __( 'Do not update modification date', 'silent-update' ) . '</label>
</div>
<div class="misc-pub-section">' . __( 'Modified on:', 'silent-update' ) . ' <b>' . $date . '</b></div>';
}
/**
* Skip updating the modification date if the checkbox is selected.
*/
public function filter_handler( $data, $postarr ) {
// Check if the nonce is set and valid
if ( ! isset( $_POST['silent_update_custom_box_nonce'] ) || ! wp_verify_nonce( $_POST['silent_update_custom_box_nonce'], 'silent_update_custom_box' ) ) {
return $data;
}
// If "Do not update modification date" checkbox is checked
if ( isset( $_POST['modification_date'] ) && $_POST['modification_date'] ) {
$data['post_modified'] = $data['post_date']; // Keep the original post date
$data['post_modified_gmt'] = get_gmt_from_date( $data['post_date'] );
}
// If checkbox is not checked, do nothing - let WordPress handle it automatically
return $data;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment