Halloween Sale 15% off Ends in See the plans
PHP

"Minor Edit" Option That Keeps the Last Updated Date

Adds a "Minor edit" checkbox to the block editor; when ticked, fixing a typo does not change the post's last modified date, so "Updated on" labels and sitemaps only change for real updates.

Don’t have WPIDE yet? Install it on your site

Code

add_action( 'init', function () {
    register_post_meta( 'post', '_wpide_minor_edit', array(
        'type'          => 'boolean',
        'single'        => true,
        'default'       => false,
        'show_in_rest'  => true,
        'auth_callback' => function ( $allowed, $key, $post_id ) {
            return current_user_can( 'edit_post', $post_id );
        },
    ) );
} );

// Remember the choice from the REST request before the post is saved.
add_filter( 'rest_pre_insert_post', function ( $prepared, $request ) {
    $meta = $request->get_param( 'meta' );
    $GLOBALS['wpide_minor_edit'] = is_array( $meta ) && ! empty( $meta['_wpide_minor_edit'] ) && ! empty( $prepared->ID );
    return $prepared;
}, 10, 2 );

add_filter( 'wp_insert_post_data', function ( $data, $postarr ) {
    if ( empty( $GLOBALS['wpide_minor_edit'] ) || empty( $postarr['ID'] ) ) {
        return $data;
    }
    $current = get_post( (int) $postarr['ID'] );
    if ( $current && 'publish' === $current->post_status ) {
        $data['post_modified']     = $current->post_modified;
        $data['post_modified_gmt'] = $current->post_modified_gmt;
    }
    return $data;
}, 99, 2 );

// Untick the box after saving.
add_action( 'rest_after_insert_post', function ( $post ) {
    if ( ! empty( $GLOBALS['wpide_minor_edit'] ) ) {
        update_post_meta( $post->ID, '_wpide_minor_edit', false );
        $GLOBALS['wpide_minor_edit'] = false;
    }
} );

add_action( 'enqueue_block_editor_assets', function () {
    $screen = get_current_screen();
    if ( ! $screen || 'post' !== $screen->base || 'post' !== $screen->post_type ) {
        return;
    }

    wp_register_script( 'wpide-minor-edit', false, array( 'wp-plugins', 'wp-element', 'wp-components', 'wp-data', 'wp-editor' ), '1.0', true );
    wp_enqueue_script( 'wpide-minor-edit' );
    wp_add_inline_script( 'wpide-minor-edit', <<<'JS'
( function ( wp ) {
    var el = wp.element.createElement;
    var Panel = ( wp.editor && wp.editor.PluginPostStatusInfo ) || ( wp.editPost && wp.editPost.PluginPostStatusInfo );
    if ( ! Panel ) { return; }

    function MinorEdit() {
        var data = wp.data.useSelect( function ( select ) {
            var editor = select( 'core/editor' );
            return { published: editor.isCurrentPostPublished(), minor: !! ( editor.getEditedPostAttribute( 'meta' ) || {} )._wpide_minor_edit };
        }, [] );
        var editPost = wp.data.useDispatch( 'core/editor' ).editPost;
        if ( ! data.published ) { return null; }

        return el( Panel, null,
            el( wp.components.CheckboxControl, {
                label: 'Minor edit (keep the "last updated" date)',
                checked: data.minor,
                onChange: function ( value ) { editPost( { meta: { _wpide_minor_edit: value } } ); }
            } )
        );
    }

    wp.plugins.registerPlugin( 'wpide-minor-edit', { render: MinorEdit } );
} )( window.wp );
JS
    );
} );

Notes

The checkbox resets after each save; works with the block editor and the REST API.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.