Year Shortcode

For the functions.php file:

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

Usage

Use [year] in your posts.

Comments

  1. User Avatar
    J. DeForest

    Thanks! That code works great!!!

  2. User Avatar
    Jamaima
    Permalink to comment#

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

  3. User Avatar
    JackLee
    Permalink to comment#

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

  4. User Avatar
    JackLee
    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
    Kevin
    Permalink to comment#

    Awesome! Just what I was looking for.

  6. User Avatar
    Matheus
    Permalink to comment#
  7. User Avatar
    Paul
    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
    Marcus
    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.
      http://php.net/manual/en/function.date.php

Submit 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.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag