WPSurfer.com

SEO: Freezing or Locking the Last Modified Date of your Posts

Published on November 2, 2024 | Updated on November 3, 2024

I stopped using SEO Plugins like a year ago or so.

SEOPress Premium was the last one I used

I did it due to four main reasons:

  • I am obsessed with having the least amount of plugins possible.
  • I wanted to challenge myself and see If I could create mu-plugins who could take of SEO Basics.
  • I didn’t want to pay for premium plugins for a tiny premium feature here and there.
  • SEO Plugins tend to get bloated as they become popular.

When I started creating mu-plugins for my websites, I chose to included published and modified date since that was a common practice.

Lots of publishers did that after listening influential people in the industry telling everyone that Google rewarded fresh content.

For instance, one of my mu-plugins outputted this into the head section of my HTML

<meta name="published-date" content="2022-12-20">
<meta name="modified-date" content="2024-10-23">

I also added “published on” and “updated on” to the hero section of my posts and pages.

The related-posts section also displayed the last modified date under the post title

And now that I think about the last modified date, it used to be part of the list of posts shown using WordPress Query Loop.



Removing or Not Removing the Dates from Source Code?

When Google hit most of my sites. I removed the dates from the posts lists and related post sections.

I didn’t remove the dates from the head section of my site’s HTML or from the hero section of my posts and pages.

Why didn’t I remove traces of published and modified dates from my sites?

  • Simply because having a last updated or last modified date is not a terrible idea for some searchers.
  • There are some instances in which I truly care when the article was published and or if it was recently updated.
  • Sometimes seeing that an article has been recently updated makes you trust what you read even more.

Having said, I knew that lots of people were recommending to update posts to get a boost in rankings.

This is probably the reason why Google call out publishers who do that:

Are you changing the date of pages to make them seem fresh when the content has not substantially changed?

Creating helpful, reliable, people-first content

Let’s say you are not one of those guys who is trying to game the system and you are genuinely adding bits of information to your posts and pages.

You will probably get hit in the cross fire, since we don’t know what “substantially changed” means for Google.


Freezing Modified Dates

When reading about the impact that modified dates can have on SEO, I found out that some plugins are locking or freezing modified dates and published dates.

  1. Rank Math provides a guide on How to Lock the Modified Date of Your Posts.
  2. All in One SEO also has the option to prevent the “post modified” date from being changed.

There are also plugins that let’s you have control over the modified date of your post.

#1Change Last Modified Date
#2WP Last Modified Info

Mu-Plugin to Freeze or Lock Modified Dates

If you are not a big fan of using SEO Plugin or adding a specific plugin to have this feature, you probably want to do this via a mu-plugin.

What are Mu-Plugins?

What are Mu-Plugins?

MU-plugins, or "Must Use" plugins, are a feature of WordPress that allows certain plugins to be automatically activated without requiring manual activation.

To use this mu-plugin, you just have to create a custom field:

This plugin will lock the modified date by default and you will have to choose Unfreeze from the custom fields to make sure changes in the modified date are visible on the source code or on your pages.

<?php
/**
 * Plugin Name: Lock Modified Date
 * Description: Adds a checkbox to lock the modified date of posts and custom post types
 * Version: 1.1
 * Author: TicoLibre
 */
// Add meta box to post editor
function add_lock_date_meta_box() {
    $post_types = get_post_types(['public' => true]);
    unset($post_types['attachment']);
    unset($post_types['revision']);
    foreach ($post_types as $post_type) {
        add_meta_box(
            'lock_modified_date',
            'Lock Modified Date',
            'render_lock_date_meta_box',
            $post_type,
            'side',
            'high'
        );
    }
}
add_action('add_meta_boxes', 'add_lock_date_meta_box');
// Render the meta box content
function render_lock_date_meta_box($post) {
    $is_locked = get_post_meta($post->ID, 'locked_modified_date', true);
    wp_nonce_field('lock_modified_date_nonce', 'lock_modified_date_nonce');
    ?>
    <label>
        <input type="checkbox" name="lock_modified_date" <?php checked($is_locked, 'yes'); ?> />
        Lock modified date
    </label>
    <p class="description">When checked, the modified date will not update when saving.</p>
    <?php
    // Only show status to administrators
    if (current_user_can('manage_options')) {
        echo '<div style="margin-top: 10px; padding: 10px; border-top: 1px solid #ddd; background: #f8f8f8;">';
        echo '<small><strong>Status:</strong><br>';
        echo 'Lock status: ' . ($is_locked ?: 'no') . '<br>';
        echo 'Last modified: ' . get_the_modified_date('Y-m-d H:i:s', $post->ID) . '</small>';
        echo '</div>';
    }
}
// Save meta box data
function save_lock_date_meta($post_id) {
    if (!isset($_POST['lock_modified_date_nonce']) || 
        !wp_verify_nonce($_POST['lock_modified_date_nonce'], 'lock_modified_date_nonce')) {
        return;
    }
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if (!current_user_can('edit_post', $post_id)) {
        return;
    }
    $is_locked = isset($_POST['lock_modified_date']) ? 'yes' : 'no';
    update_post_meta($post_id, 'locked_modified_date', $is_locked);
    // Add debug log
    error_log('Lock Modified Date: Setting lock status to ' . $is_locked . ' for post ' . $post_id);
}
add_action('save_post', 'save_lock_date_meta');
// Prevent modified date from updating
function prevent_modified_date_update($data, $postarr) {
    if (isset($postarr['ID'])) {
        $is_locked = get_post_meta($postarr['ID'], 'locked_modified_date', true);
        if ($is_locked === 'yes') {
            $post = get_post($postarr['ID']);
            if ($post) {
                $data['post_modified'] = $post->post_modified;
                $data['post_modified_gmt'] = $post->post_modified_gmt;
                // Add debug log
                error_log('Lock Modified Date: Preventing update for post ' . $postarr['ID'] . ' - Keeping date: ' . $post->post_modified);
            }
        }
    }
    return $data;
}
add_filter('wp_insert_post_data', 'prevent_modified_date_update', 10, 2);


Mu-Plugin Breakdown

“Lock Modified Date”, provides functionality to prevent the modified date of posts and custom post types from updating when the post is saved or updated.

Add Meta Box to Post Editor:

The plugin adds a custom meta box called “Lock Modified Date” to the post editor page.

This meta box contains a checkbox that allows the user to choose whether the modified date of the post should be locked.


Checkbox to Lock Modified Date:

When checked, the modified date will not be updated when the post is saved.

The status of the checkbox is stored in the post’s metadata (locked_modified_date).

Administrators can view the current lock status and the last modified date in the post editor.


Save Meta Data:

When the post is saved, the plugin saves whether the checkbox is checked or unchecked as a metadata entry for that post.

This is done using the save_lock_date_meta function, which saves the metadata when the post is updated.


Prevent Modified Date Update:

The core functionality is implemented in the prevent_modified_date_update function, which uses a filter on the post save process to check if the checkbox is checked.

If the checkbox is checked (i.e., the modified date is locked), it prevents the post_modified and post_modified_gmt fields from being updated, thus preserving the original modified date.


Manuel Campos

Manuel Campos

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.