Instantly share code, notes, and snippets.
Last active Feb 23, 2018
Set of functions for storing and getting last modified timestamps for WordPress menus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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