Last modified Schema Output Shortcode

Especially, when you’re writing irregularly or you simply want Google and Co. to exactly know, when your blog was last edited, you can add this code to your Theme’s functions.php, or via a Code Snippet Plugin. Place it at the bottom of your website, f.ex. in the Footer, for a visible “Last Edit” Date output.

To use this, place [last_edit] wherever you want. Once added, the shortcode will generate a Meta Tag to your Header that adds a machine-readable time stamp, when your Website’s has been lastly edited.

// Get last modified date
function get_last_modified_date() {
    global $wpdb;
    $last_post_modified = $wpdb->get_var("SELECT MAX(post_modified) FROM $wpdb->posts WHERE post_status = 'publish'");
    $last_postmeta_modified = $wpdb->get_var("SELECT MAX(meta_value) FROM $wpdb->postmeta WHERE meta_key = '_edit_last'");
    $last_modified = max($last_post_modified, $last_postmeta_modified);
    $formatted_date = date_i18n('l, \d\e\n d.m.Y \u\m H:i', strtotime($last_modified));
    return $formatted_date;
}

// Shortcode for last modified date
function last_edit_shortcode() {
    return get_last_modified_date();
}
add_shortcode('last_edit', 'last_edit_shortcode');

// Add last modified meta tag
function add_last_modified_meta_tag() {
    global $wpdb;
    $last_post_modified = $wpdb->get_var("SELECT MAX(post_modified) FROM $wpdb->posts WHERE post_status = 'publish'");
    $last_postmeta_modified = $wpdb->get_var("SELECT MAX(meta_value) FROM $wpdb->postmeta WHERE meta_key = '_edit_last'");
    $last_modified = max($last_post_modified, $last_postmeta_modified);
    $formatted_meta_date = date('c', strtotime($last_modified));
    echo '' . "\n";
}
add_action('wp_head', 'add_last_modified_meta_tag');

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *