Shortcode for the Year

I wanted to get the current year using a shortcode. In addition to that, I wanted it to be usable in a text widget. This will allow me to put it in the footer with a text widget, which is much less troublesome than modifying the entire footer file to use a page.

The code for creating the shortcode is very simple and borrowed from the Thematic theme.

function shortcode_year() {
    return '<span class="the-year">' . date( 'Y' ) . '</span>';
}
add_shortcode('the-year', 'shortcode_year');

This tutorial on DigWP explains how to get shortcodes to work in widgets. It’s a very simple solution which involves filtering the widget text.

add_filter('widget_text', 'do_shortcode');

That’s it, putting [the-year] in a text widget will now result in the current year being displayed in the browser.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s