Skip to content

Instantly share code, notes, and snippets.

@kellenmace
Last active February 23, 2018 16:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellenmace/8620815f7b5e4dc9127979f456810711 to your computer and use it in GitHub Desktop.
Save kellenmace/8620815f7b5e4dc9127979f456810711 to your computer and use it in GitHub Desktop.
Set of functions for storing and getting last modified timestamps for WordPress menus
<?php
/**
* Save the date modified timestamp for a WordPress menu.
*
* @param int $menu_id Term ID of the menu being created/updated.
*/
function wds_save_menu_modified_timestamp( $menu_id ) {
$modified_timestamps = get_option( '_wds_menu_modified', array() );
$modified_timestamps[ $menu_id ] = current_time( 'timestamp' );
update_option( '_wds_menu_modified', $modified_timestamps, false );
}
add_action( 'wp_create_nav_menu', 'wds_save_menu_modified_timestamp' );
add_action( 'wp_update_nav_menu', 'wds_save_menu_modified_timestamp' );
/**
* Delete the date modified timestamp for a WordPress menu.
*
* @param int $menu_id Term ID of the menu being deleted.
*/
function wds_delete_menu_modified_timestamp( $menu_id ) {
$modified_timestamps = get_option( '_wds_menu_modified', array() );
// Bail if this menu ID doesn't exist in the array of date modified timestamps.
if ( ! isset( $modified_timestamps[ $menu_id ] ) ) {
return;
}
unset( $modified_timestamps[ $menu_id ] );
update_option( '_wds_menu_modified', $modified_timestamps, false );
}
add_action( 'wp_delete_nav_menu', 'wds_delete_menu_modified_timestamp' );
/**
* Get the date modified timestamp for a WordPress menu.
*
* @param int $menu_id Term ID of the menu.
*
* @return int|string The date modified timestamp or empty string, if none.
*/
function wds_get_menu_modified_timestamp( $menu_id ) {
$modified_timestamps = get_option( '_wds_menu_modified', array() );
// Return an emtpy string if this menu ID doesn't exist in the array of date modified timestamps.
if ( ! isset( $modified_timestamps[ $menu_id ] ) ) {
return '';
}
return $modified_timestamps[ $menu_id ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment