?

Log in

No account? Create an account

Peter Rogers's Blog
Artist-in-Residence at Chez Firth

Wednesday (3/6/13) 2:44am - ... wherein Peter plays around with WordPress.

Behold, I have made the world's dumbest WordPress plugin...

This is age-of-post.php, which sits in the "plugins" directory and converts [PostAge] to (say) "This post is 3 Weeks old."



<?php
/*
Plugin Name: Age of Post
Plugin URI: http://www.hujhax.com
Description: Returns a useful string giving the age of the current post.
Version: 101
Author: Peter Rogers
Author URI: http://www.hujhax.com
*/

if (!class_exists("AgeOfPoster")) {
    class AgeOfPoster {
        // we'll replace all [PostAge]s with "This post is <x> old."
        function AgeOfPoster()
        {
            add_shortcode('PostAge', array($this,'AgeOfPost'));
        }

        // converts a quantity of seconds to (say) "3 Weeks".
        function SecondsToDuration($seconds)
        {
            // figure out what scale this duration is
            $units = array("Year" => 31536000,
                           "Week" =>  604800,
                           "Day" => 86400,
                           "Hour" =>  3600,
                           "Minute" =>  60,
                           "Second" =>  1);

            foreach ($units as $unitName => $numberOfSeconds)
                if ($seconds >= $numberOfSeconds)
                    break;

            // figure out how many of that unit we have.
            $quantity = round($seconds / $numberOfSeconds);
            $suffix = ($quantity == 1) ? "" : "s";
            $duration = $quantity . " " . $unitName . $suffix;

            return $duration;
        }

        function AgeOfPost( )
        {
            $timestamp= get_post_time('U', true);
            $secondsElapsed= time() - $timestamp;

            $duration = $this->SecondsToDuration($secondsElapsed);

            return "This post is " . $duration . " old.";
        }
    }
}

new AgeOfPoster();



Tags: ,
Mood: [mood icon] amused · Music: none
Previous Entry Share Next Entry

Comments:

[User Picture]
From:happywaffle
Date:Wednesday (3/6/13) 12:57pm
(Link)
Spiffy!
(Reply to this) (Thread)