Skip to content

Instantly share code, notes, and snippets.

@bhalash
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save bhalash/de30e47d66f6dddbde9f to your computer and use it in GitHub Desktop.

Select an option

Save bhalash/de30e47d66f6dddbde9f to your computer and use it in GitHub Desktop.
An expiry timer, for Miley <3
<?php
/*
* Get, Set and Check Twerking time
* -----------------------------------------------------------------------------
* @param string $content Post content.
* @return string $content Post content.
*/
function for_miley_cyrus($content) {
// Relevant meta key.
$meta_key = 'countdown_to_twerking';
// In format readable by strtotime();
$time_diff = '+30 days';
// Post ID.
$post_id = get_the_ID();
// Retrieve meta value.
$twerk_time = get_twerk_time($post_id, $meta_key);
if (!$twerk_time) {
/* Set time if it hasn't already. You probably won't do this, but I
* needed a hook to show how to do this. */
add_post_meta($post_id, $meta_key, generate_twerk_time($post_id, '+30 days'), true);
$twerk_time = get_twerk_time($post_id, $meta_key);
}
if (is_it_twerk_time($twerk_time)) {
// We all love Miley, right?
printf('\o/ Come in like a wreckling ball! \o/');
}
// Content is passed through without being affected.
return $content;
}
/*
* Get Twerking Time
* -----------------------------------------------------------------------------
* @param int $post_id
* @param string $meta_key
* @return string Post meta key value.
*/
function get_twerk_time($post_id, $meta_key) {
return get_post_meta($post_id, $meta_key, true);
}
/*
* Set Twerking Time
* -----------------------------------------------------------------------------
* @param int $post_id
* @param string $difference Absolute expiry date
* @return string Expiry time in Unix format, with absolute start point (publish date).
*/
function generate_twerk_time($post_id, $difference) {
return strtotime($difference, get_the_date($post_id));
}
/*
* Determine if it is Time to Twerk
* -----------------------------------------------------------------------------
* @param int $target_date Date of post.
* @return bool Target time and date has passed, true or false.
*/
function is_it_twerk_time($target_date) {
return ($target_date - date('U') <= 0);
}
/* This is a useful way to attach an action to a single post: Check whether the
* deadline has passed when post content is loaded. You can attach it this action
* anywhere else that might be useful. This is just an example! */
add_filter('the_content', 'for_miley_cyrus');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment