Last updated on October 18, 2023

Add a Custom Copyright Year Dynamically

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Show dynamic copyright year.

A common requirement for websites is to display the current year dynamically in the copyright section. This ensures that the copyright year is always up to date without manual intervention. To achieve this functionality in WordPress, you can use a custom PHP function along with a shortcode.

Here’s an example of how you can add a custom copyright year dynamically using a shortcode:

function wpsnippets_copyright_year() {
    $current_year = date('Y');
    return $current_year;
}
add_shortcode('copyright_year', 'wpsnippets_copyright_year');

In the code snippet above, we define a custom PHP function called wpsnippets_copyright_year(). This function uses the date() function to retrieve the current year and assigns it to the $current_year variable. Finally, the function returns the current year.

To make this functionality available as a shortcode, we use the add_shortcode() function. It takes two parameters: the shortcode name (in this case, ‘copyright_year’) and the callback function name (wpsnippets_copyright_year).

Once you’ve added this code to your theme’s functions.php file or a custom plugin, you can use the [copyright_year] shortcode in any post, page, or widget to display the current year dynamically.

For example, if you add [copyright_year] in the footer section of your website, it will be replaced with the current year when the page is rendered.

This code snippet is useful for any WordPress website that wants to display the current year dynamically in the copyright section. It eliminates the need to manually update the year every year, ensuring that the copyright information is always accurate.

Examples

Example 1: Adding a Custom Copyright Year Dynamically in the Footer

This use case demonstrates how to add a custom copyright year dynamically in the footer of a WordPress website. The code example uses the wpsnippets_get_copyright_year() function to retrieve the current year and display it in the footer.

function wpsnippets_get_copyright_year() {
    $current_year = date('Y');
    $start_year = 2010; // Replace with your website's start year
    if ($current_year > $start_year) {
        return $start_year . ' - ' . $current_year;
    } else {
        return $current_year;
    }
}

Explanation: The wpsnippets_get_copyright_year() function retrieves the current year using the date() function and checks if it is greater than the website’s start year. If it is, it returns the start year followed by a hyphen and the current year. Otherwise, it returns only the current year.

Example 2: Adding a Custom Copyright Year Dynamically in a Theme Template

This use case demonstrates how to add a custom copyright year dynamically in a specific theme template file, such as footer.php. The code example uses the wpsnippets_get_copyright_year() function to retrieve the current year and display it in the template.

<footer>
    <p>&copy; <?php echo wpsnippets_get_copyright_year(); ?> Your Website Name</p>
</footer>

Explanation: The code snippet shows how to include the wpsnippets_get_copyright_year() function within the HTML markup of a theme template file. The function is called using the echo statement to display the dynamic copyright year.

Example 3: Adding a Custom Copyright Year Dynamically in a Widget

This use case demonstrates how to add a custom copyright year dynamically in a WordPress widget. The code example uses the wpsnippets_get_copyright_year() function to retrieve the current year and display it in the widget.

class WPSnippets_Copyright_Widget extends WP_Widget {
    public function widget($args, $instance) {
        echo $args['before_widget'];
        echo $args['before_title'] . 'Copyright' . $args['after_title'];
        echo '<p>&copy; ' . wpsnippets_get_copyright_year() . ' Your Website Name</p>';
        echo $args['after_widget'];
    }
}

Explanation: The code snippet demonstrates how to include the wpsnippets_get_copyright_year() function within a custom widget class. The function is called within the widget() method to display the dynamic copyright year in the widget’s output.

Last updated on October 18, 2023. Originally posted on November 11, 2023.

Leave a Reply