Home / Archive / Copyright & Current Year Shortcodes
Duplicate Snippet

Embed Snippet on Your Site

Copyright & Current Year Shortcodes

Add this snippet to the functions.php file in your theme or plugin.

Type [year] in any text block to return the current year.
Type [copyright] in any text block to return the name of your website + the copyright symbol and current year.

Example output: "My Website Title © 2024."

Code Preview
php
<?php
/**
 * Returns the current year.
 *
 * @return string Current year in YYYY format.
 */
function get_current_year() {
    return date('Y');
}
/**
 * Shortcode to display the current year.
 *
 * @return string HTML span with current year.
 */
function shortcode_current_year() {
    return "<span class='date'>" . get_current_year() . "</span>";
    // Type [year] in any text block to return the current year.
    // Style in CSS with .date
}
add_shortcode('year', 'shortcode_current_year');
/**
 * Shortcode to display dynamic copyright information.
 *
 * @return string HTML span with site name and current year.
 */
function dynamic_copyright_shortcode($atts) {
    $site_name = get_bloginfo('name');
    return "<span class='copyright'>$site_name &copy; " . get_current_year() . "</span>";
    // Type [copyright] in any text block to return copyright with the current year.
    // Style in CSS with .copyright
    // This will return "My Website Title 2024."
}
add_shortcode('copyright', 'dynamic_copyright_shortcode');

Comments

Add a Comment