Quick Programing Tip: Year Shortcode

No Comments
Quick Tips

You (or had someone like Atomic Coffee Media) create a great site.  In the footer at the bottom of every page, you probably have a copyright startment like so (©2021 Atomic Coffee Media. All rights reserved.).  However, each year you notice the year doesn’t change automatically and you have to manually go in and edit the footer.

Not so bad, but tedious, and you probably will forget about something this small, and people might think your site is out of date.  So what to do?  A great thing would be if WordPress included a shortcode for something as simple as a year, but they don’t.

A shortcode is basically a small little placeholder that you put in your editor when adding text to your site.  The shortcode is basically a shortcut to display some code and its output right on your site.

Now, if you read above you might have noticed I mentioned code, which in turn means if we are going to make our own shortcode, there is some programming needed.  But fear not, in this article I’ll show you how to simply past 5 lines of code in the right place to make your own shortcode! (you can then brag to your friends that you are a programmer as well 🙂

Let’s Get Coding!

WordPress understands that you might want to add some custom code to your site and created an editor to do so. You can get to this by going to “Appearance” -> “Theme File Editor”.

Add Date Function in WordPress Theme File Editor

Once here, you want to make sure to select your function file.  The function file is where you can add code that will be used with your site.  A few things to note here however.

If you theme came with a “Child” theme, edit the function file of that.  A child theme basically allows you to add stuff to your site without modifying the actual theme itself.  This is important when you update a theme as it would overwrite your code.  If you theme didn’t come with one, WordPress has an article here: https://developer.wordpress.org/themes/advanced-topics/child-themes/

Also, please be careful when editing theme files.  A simple mistake can break your site.  The codes below can be copied directly to help you make sure that you have everything in place correctly!

So… the first thing we will need to do is add a block of code to your functions file.  It’s actually only 5 lines of code!

function year_shortcode () {
$year = date_i18n ('Y');
return $year;
add_shortcode ('year', 'year_shortcode');

Once you copied the above text make sure you save your changes.  This little piece of code will allow you to now add a shortcode to display the current year.  Any place you want to add it, simply put the shortcode below into your text editor.

Previous Post
Updating WordPress, Themes, and Plugins
Next Post
Hiring a website developer step 1: Creating a sitemap of your site
You must be logged in to post a comment.