Year Shortcode

For the functions.php file:

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


Use [year] in your posts.


  1. User Avatar
    J. DeForest

    Thanks! That code works great!!!

  2. User Avatar
    Permalink to comment#

    Nice, works like a charm. Thanks for sharing this useful tip.

  3. User Avatar
    Permalink to comment#

    Copyright © $starty){echo $starty.’-‘.$nowy;}else{echo $nowy;} }else{ echo date(“Y”);} ?> . All Rights Reserved.

  4. User Avatar
    Permalink to comment#

    Copyright © <?php date_default_timezone_set(PRC); $starty=get_option(‘cwpcg_starty’);
    $nowy=date(“Y”); if(get_option(‘cwpcg_starty’)){ if($nowy>$starty){echo $starty.’-‘.$nowy;}else{echo $nowy;} }else{ echo date(“Y”);} ? > <?php bloginfo(‘name’); ? >. All Rights Reserved.

  5. User Avatar
    Permalink to comment#

    Awesome! Just what I was looking for.

  6. User Avatar
    Permalink to comment#
  7. User Avatar
    Permalink to comment#

    If you want to include shortcodes in your widgets, such as [year] in a footer widget for the copyright date, then add this to your functions.php file:

    // Enable shortcodes in text widgets
    add_filter('widget_text', 'do_shortcode');
  8. User Avatar
    Permalink to comment#

    How could you add the month before the year so that the shortcode returned Month Year?

    That would be rad and I’m stumped.

    • User Avatar
      Anthony Hortin
      Permalink to comment#

      You can use any of these characters (below) for formatting your date. So, to include the month, you would replace date('Y'); with date('F Y'); or date('M Y');, depending on whether you want the full textual representation of a month or just the first three letters.

    • User Avatar
      Marcus Thackston
      Permalink to comment#

      Got it. That worked great. I wasn’t sure about the syntax and that manual helped for sure. Appreciate it!

  9. User Avatar
    Justin Mosebach
    Permalink to comment#

    How can I add a shortcode to an Advanced Custom Field?

  10. User Avatar

    For year range, you could use :

    function year_shortcode() {
        $fromYear = 2017;
        $thisYear = (int)date('Y');
        return $fromYear . (($fromYear != $thisYear) ? '-' . $thisYear : '');
    } add_shortcode('year', 'year_shortcode');
  11. User Avatar
    Permalink to comment#

    Thanks, works really nice! But the shortcode displays the month in English even though I set the site language to Swedish under Settings > General. Is there anyway to change that? I’ve used the following code:

    function year_shortcode() { $year = date(‘F Y’); return $year; } add_shortcode(‘year’, ‘year_shortcode’);

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.