Overview.
This guide provides detailed instructions on creating a WordPress plugin that updates the post-modified date only when the post title or content is changed and does not set a modified date when a non-content-related custom field is set or modified, such as moving a post to a new category or adding a new custom field.
Follow these steps carefully to build the plugin.
Example Prompt.
Help me create a WordPress plugin that ensures the post modified date is updated only when the post title or content is changed, not when custom fields or meta fields are updated. The plugin should hook into the post update action and compare the previous and new values of the title and content, restoring the original modified date if there are no changes in these fields.
Prerequisites
- Basic knowledge of PHP and WordPress plugin development.
- Access to a WordPress installation for testing.
Step 1: Set Up Your Plugin Folder
- Create a Plugin Directory
- Navigate to your WordPress installation directory.
- Go to wp-content/plugins.
- Create a new folder named easycoding-strict-post-modified-date-control.
Step 2: Create the Main Plugin File
- Create the Main Plugin File
- Inside the easycoding-strict-post-modified-date-control folder, create a PHP file named easycoding-strict-post-modified-date-control.php.
- Add Plugin Header Information and Functionality
- Open the easycoding-strict-post-modified-date-control.php file and add the following code:
<?php
/**
* Plugin Name: EasyCodingWithAI - Strict Post Modified Date Control
* Description: Updates the post modified date only when the post title or content is changed, not when meta fields are updated.
* Version: 1.3
* Author: EasyCodingWithAI
*/
function easycoding_strict_preserve_modified_date($post_id, $post_after, $post_before) {
// Only run for posts (you can adjust this if needed)
if ($post_after->post_type !== 'post') {
return;
}
// Check if title or content has changed
$title_changed = $post_before->post_title !== $post_after->post_title;
$content_changed = $post_before->post_content !== $post_after->post_content;
// If neither title nor content has changed, restore the original modified dates
if (!$title_changed && !$content_changed) {
global $wpdb;
// Update the post_modified and post_modified_gmt fields in the database directly
$wpdb->update(
$wpdb->posts,
array(
'post_modified' => $post_before->post_modified,
'post_modified_gmt' => $post_before->post_modified_gmt,
),
array('ID' => $post_id)
);
}
}
add_action('post_updated', 'easycoding_strict_preserve_modified_date', 10, 3);
Step 3: Test the Plugin
- Activate the Plugin
- Go to your WordPress admin dashboard.
- Navigate to Plugins > Installed Plugins.
- Find EasyCodingWithAI – Strict Post Modified Date Control and click Activate.
- Verify Functionality
- Create a new post or edit an existing one.
- Change the title or content and save the post.
- Verify that the modified date updates correctly.
- Change meta fields without altering the title or content and check that the modified date remains unchanged.
Step 4: Finalize and Document
- Document Your Code
- Ensure all functions are well-commented.
- Add any additional documentation necessary for future reference or other developers.
Disclaimer
The coding tips and guides provided on this website are intended for informational and educational purposes only. While we strive to offer accurate and helpful content, these tips are meant as a starting point for your own coding projects and should not be considered professional advice.
We do not guarantee the effectiveness, security, or safety of any code or techniques discussed on this site. Implementing these tips is done at your own risk, and we encourage you to thoroughly test and evaluate any code before deploying it on your own website or application.
By using this site, you acknowledge that we are not responsible for any issues, damages, or losses that may arise from your use of the information provided herein.