Community links: Useful graphics edition

WordPress graphs

This week brought us plenty of news in themes, Plugins, blogs posts, and tutorials for your weekend WordPress reading pleasures.

In WordPress themes this week:

In tutorials this week:

More after the break.

Notable blog posts this week include:

The last seven days of Plugins have brought us:

Community links: Useful graphics edition

WordPress graphs

This week brought us plenty of news in themes, Plugins, blogs posts, and tutorials for your weekend WordPress reading pleasures.

In WordPress themes this week:

In tutorials this week:

More after the break.

Notable blog posts this week include:

The last seven days of Plugins have brought us:

The WPCandy WordPress Podcast, episode 001

The very first WPCandy WordPress Podcast

Congratulations on being one of the first listeners to the WPCandy WordPress Podcast! Episode 001 features myself (Ryan) and JD Bentley (@jdbentley, WPCandy News Editor) discussing the new website, the events of the week, and what to expect from the podcast going forward.

A few programming notes:

  • The podcast will be available on iTunes shortly, and this post will be updated with subscription information. Until then, enjoy the player in post, or the MP3 download link.
  • If you would like your email read on a future WPCandy podcast, send an email to podcast@wpcandy.com with your question, comment, or thought and we’ll do our best to read it on the show.

It’s both exciting and frightening to start a new podcast. Exciting because it’s new and creating something where nothing was before. It’s frightening for the same reason, and because the first of anything is always the hardest to finish. So the hard part is over!

Please let us know your thoughts, how we can improve, and the sorts of things you would like to hear about.

Also: Download the MP3 Playing time: 28 minutes, 11 seconds

How to Display Human Readable Post Dates in WordPress

Human Readable Post Dates

After the URLs as sentences tutorial earlier this week, I thought the topic of human readable websites would be worth touching on again. This time, we’ll look at post dates.

While it’s dead simple to include post dates inside WordPress permalinks (WordPress does this in its default post permalink structure) it’s also helpful to keep the date listed within the post.

Instead of displaying dates like this:

Posted August 19, 2010

We’re going to display dates like this:

Posted yesterday

If you want to jump straight to the functionality, you can do that. Grab the Date in a Nice Tone Plugin, and use their template tag. But this is a tutorial, right? So let’s dig in and look at how this Plugin works.

Before looking at how this Plugin works, let’s think through it conceptually. Our end goal is to display a human readable post date that uses terms like yesterday and last month. So we’re going to need to build a PHP function that will do a few things:

  1. Find the current time.
  2. Find the time of the post.
  3. Find the difference between the two.
  4. If the difference equals x, display y.

It’s actually a pretty simple process. Let’s begin stepping through the wp-date-in-a-nice-tone.php file that comes with the Plugin. Feel free to download the Plugin from the link above and follow along.

The first few lines of the function look like this:

function wp_date_in_a_nice_tone() {

     $postTime = get_the_time("U");
     $currentTime = time();
     $timeDifference = $currentTime - $postTime;

     $minInSecs = 60;
     $hourInSecs = 3600;
     $dayInSecs = 86400;
     $monthInSecs = $dayInSecs * 31;
     $yearInSecs = $dayInSecs * 366;

This whole block of code sets up the variables for the rest of the function. First we get the time the post was published, using get_the_time("U");. This is a WordPress function that, along with the parameter provided by PHP, grabs the post’s date in seconds.

Next, the variable $currentTime is set to the current time, which is determined using the PHP function time();.

With the last line of the first block of code, the $timeDifference is found by subtracting the $postTime from the $currentTime. This gives us the difference, which we can use in the rest of the function to assign certain phrases to certain lengths of time.

     $minInSecs = 60;
     $hourInSecs = 3600;
     $dayInSecs = 86400;
     $monthInSecs = $dayInSecs * 31;
     $yearInSecs = $dayInSecs * 366;

This second block of PHP is simple, in that it’s assigning variables to lengths of time so that we don’t have to litter the function with so many numbers. We can’t refer to an hour as “an hour” within PHP, we have to refer to it as the number of seconds. So by creating the $hourInSecs variable, we can refer to it almost as conveniently.

     // if over 3 years
     if ($timeDifference > ($yearInSecs * 3)) {
          $dateWithNiceTone = "quite a long while ago...";

     // if over 2 years
     } else if ($timeDifference > ($yearInSecs * 2)) {
          $dateWithNiceTone = "over two years ago";

     . . .

     // if over 28 days ago
     } else if ($timeDifference > ($dayinSecs * 28)) {
          $dateWithNiceTone = "around a month ago";

     // if equal to or more than 8 days ago
     } else if ($timeDifference >= ($dayInSecs * 8)) {
          $dateWithNiceTone = "in the last month";

Finally, we get into the meat of the function; and it’s actually quite straightforward. Beginning with the oldest dates (in this case 3 years, though there’s no reason you couldn’t start it sooner), the time difference is checked against what would be the equivalent in 3 years of seconds. If that is the case, it sets the $dateWithNiceTone variable to “quite a long time ago…” and if that’s not the case, it continues to the next step. A pretty simple if…else if sequence.

Where this Plugin doesn’t have an administrative interface, you can see that it wouldn’t be difficult to change the words used, or to add in your own statement based on a new scenario.

If we wanted to show if a post was added yesterday, we could add this toward the bottom:

     // if equal to 24 hours or more
     } else if ($timeDifference > ($hourInSecs * 24)) {
          $dateWithNiceTone = "yesterday";

This is one of the statements that we added for WPCandy, except in our case we went with 18 rather than 24, because we found it to be more accurate in practice.

Finally, the Plugin echoes the variable:

     echo $dateWithNiceTone;

Which is, of course, to be expected.

I’m sure if you are interested in this functionality you will grab the Plugin. After all, that’s what I did. But hopefully now you not only use it, you also understand how it works. If you wanted to, perhaps you could write the function yourself now. While this is a simple Plugin with a simple purpose, having this experience will make it easier to understand, reverse engineer, and truly understand more complicated Plugins in the future.

WPCandy Giveaway: Win 1 of 4 WordPress books!

The four books you'll get to choose from. Photo by Ashley, @arae on Twitter

It has been an exciting week on the site. We appreciate the praise, the encouragement, and that you’re all still reading after all this time. To make sure we start things off on the right foot, we would like to give away a WordPress book—pages and pages of fresh WordPress knowledge—to a a lucky member of the WPCandy community, just for commenting.

If you win you will be asked to choose one of four books to receive, shipped to you free courtesy of WPCandy. The four available books are:

Rules on how to win are after the jump.

What is exciting to you right now?

To be entered to win, all you have to do is leave a comment telling us what it is about WordPress, as a whole, you are most excited about right now. It can be a new WordPress feature, a commercial Plugin or theme, a particular service, or something within the community as a whole. Just tell us about it.

One commenter will be chosen randomly on Friday to win. Only one comment per person will be counted.

I’ll start things off. Right now I’m most excited about custom taxonomies in WordPress. I’ve created a number within WPCandy, and the nerdy librarian blogger in me just loves thinking through the right way to categorize and organize content. Most of that is behind the scenes right now, but I’ll be sure to post and talk a bunch about it when it goes live.

Alright, your turn! We’ll draw the winner Friday afternoon.