Skip to content

Instantly share code, notes, and snippets.

@goaround
Created July 9, 2020 14:26
Show Gist options
  • Save goaround/ed9388577f4b933bde23587b31c0e4be to your computer and use it in GitHub Desktop.
Save goaround/ed9388577f4b933bde23587b31c0e4be to your computer and use it in GitHub Desktop.
WordPress Snippet for changing the RSS Feed GUID after a significant post update
<?php
/**
* Plugin Name: Change RSS Feed GUID on update
* Plugin URI:
* Description: Change the RSS Feed GUID after a significant post update
* Author: Johannes Kinast <johannes@travel-dealz.de>
* Author URI: https://go-around.de
* Version: 1.0.0
*/
namespace Feed_Guid;
function check_post_update( $post_id, $post_after, $post_before ) {
$timestamp_before = strtotime( $post_before->post_date );
$timestamp_after = strtotime( $post_after->post_date );
// Check if Post Date changed
if ( $timestamp_before >= $timestamp_after ) {
return;
}
// Check is Post Status is publish and if the Post Date changed significant
if ( 'publish' === $post_after->post_status && 60*60*24 < $timestamp_after - $timestamp_before ) {
// Find lates Revision
if ( $latest_revision = array_shift( wp_get_post_revisions( $post_id ) ) ) {
// Update _last_update_id to the latest revision ID
update_post_meta( $post_id, '_last_update_id', $latest_revision->ID );
}
}
}
add_action( 'post_updated', __NAMESPACE__ . '\check_post_update', 10, 3 );
function feed_guid( $guid, $id ) {
// Check if current query is for a feed
if( ! is_feed() ) {
return $guid;
}
// Get the last id
if ( $last_id = get_metadata( 'post', $id, '_last_update_id', true ) ) {
// Add the last id
$guid .= '#' . (int) $last_id;
}
return $guid;
}
add_filter( 'the_guid', __NAMESPACE__ . '\feed_guid', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment