Useful Snippets

Useful Snippets


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

July 2020
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories


Current year shortcode for printing copyright notice in WordPress

Stanislav KhromovStanislav Khromov

Did you ever want a quick way to print a nice footer with the copyright in years?

Here’s a shortcode for that! Put it in your themes functions.php and use anywhere!

add_shortcode('copyright-year', function($atts, $content)
{
    extract(shortcode_atts(array(
        'sign' => 'true',
        'start' => '',
    ), $atts));

    $current_year = date('Y');
    $print_sign = ($sign === 'true') ? '©' : '';

    if($start === $current_year || $start === '')
        return "{$print_sign} {$current_year}";
    else
        return "{$print_sign} {$start} - {$current_year}";
});

Usage

Basic

[copyright-year]

Result: © 2020

Without copyright sign

[copyright-year sign="false"]    

Result: 2020

With copyright sign and a “start year”.

[copyright-year sign="true" start="2012"]

Result: © 2012 - 2020

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 0
There are currently no comments.