At the bottom of every page on my site is a little bit of info declaring copyright: “Copyright © 2017 Mika A. Epstein”

How do I do that and not have to update all my site themes and widgets every year? With code, of course! I have both a function I could use in themes and a shortcode I could use anywhere a shortcode can be used.

The Code

There are two functions, the base code and the shortcode. The concept is that if you don’t put in a year (which is the start year for your copyright) it will only show the current year. If you do put in year, it forces it to be an integer and then does a couple checks. The checks were originally as follows:

  1. Is $year ‘auto’? Force this year.
  2. Is $year this year? Force this year.
  3. Is $year equal to 0? Force this year.
  4. Is $year greater than this year? Oh, silly human. Force this year.
  5. Is $year less than this year? Use the ‘start – end’ format

The reason for this is practical. We’re sanitizing things as early as we can, and then we’re checking for the logical and illogical entries. If someone decides the year is ‘Bob’ then intval() throws a 0 and since 0 isn’t actually a valid year in the Gregorian calendar, then I can do a simple “if 0” check.

But … I’m not so bold as to assume the only people who will want this are using the Gregorian calendar. To be more universal, I changed the code to make the first check for if the year was set to ‘auto’ (which defaults to this year), or if it was a non-number. If it’s not a number, you get forced this year. Otherwise, the code trusts you.

function helf_auto_copyright( $year = 'auto' , $text = '' ){ 
	$year = ( $year == 'auto' || ctype_digit($year) == false )? date('Y') : intval($year);
	$text = ( $text == '' )? '©' : sanitize_text_field( $text );

	if( $year == date('Y') || $year > date('Y') ) $output = date('Y');
	elseif( $year < date('Y') )  $output = $year . ' - ' . date('Y');	

	echo $text . ' ' . $output;

function helf_auto_copyright_shortcode( $atts ) {
    $attributes = shortcode_atts( array(
        'year' => 'auto',
        'text' => '&copy;'
    ), $atts );

    return helf_auto_copyright( sanitize_text_field($attributes['year']), sanitize_text_field($attributes['text']) );
add_shortcode( 'copyright', 'helf_auto_copyright_shortcode' );

The one failing here is I only account for the common era (or ‘AD’ for those who didn’t know we all switched to CE a while back). I’m sure this can be extended to BCE if so desired. Spitballing, I’d just use negative numbers, check for them and output ‘year BCE – year CE’ instead. But that’s a little much for this use case.


As a shortcode: [copyright year=2016 text=Copyright]

As a function: helf_auto_copyright_shortcode( '2016', 'Copyright' );

Both will output the same thing (as of 2017): Copyright 2016 – 2017

And in 2018? It will magically update for you.

Reader Interactions


  1. Hi Mika,

    Is doing this unsafe?:

  2. Sorry, i pasted wrong line. Here it is:
    printf( esc_html__( '© 1993 - ' .date('Y')));

  3. You can use code tags just FYI 😀 <code>

    Sure. That’s fine too, but the point of this was to demonstrate reusable code that could be put into, say, a widget or a post 🙂

  4. Thanks, Mika 🙂

%d bloggers like this: