Latest Tech and Tips

How to Add a Dynamic Copyright Date in WordPress Footer

1

Often you will notice a web site that has an outdated copyright date which is fairly annoying. There are additionally websites that solely present the present 12 months for his or her copyright date which is much more annoying since you gained’t know the way previous the location is. There is a easy PHP resolution to this that the majority builders would know, however there’s a extra elegant manner that we’ll present you. In this text, we’ll share a operate that can routinely generate a copyright date primarily based on the revealed date of your oldest and latest submit.

Simple PHP Solution for Dynamic Copyright Date

For this methodology, you’ll want to add some code to your WordPress theme recordsdata. If you haven’t completed this earlier than then checkout our information on how to copy and paste code in WordPress.

The most typical manner to show a dynamic copyright date is by enhancing your WordPress theme’s footer.php file. Simply copy and paste the next code on the line the place you need to show the copyright discover.

<p>© 2020 – <?php echo date('Y'); ?> YourSite.com</p>

The downside with this code is that it can not dynamically fetch your web site’s begin date and your web site has to be no less than an year-old earlier than you need to use it.

Elegant WordPress Solution for Dynamic Copyright Date

While browsing the net, we noticed a extra elegant resolution advised by @frumph of CompicPress Theme.

This code will generate a dynamic copyright date primarily based on the revealed date of your oldest submit and your latest submit. If it’s the first 12 months of your web site, then this operate will solely show the present 12 months.

To add this, first you want to add the next code to your theme’s capabilities.php file or a site-specific plugin.

operate comicpress_copyright() {
international $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}

After that, you want to add the next code to your theme’s footer.php file the place you need to show the date:

<?php echo comicpress_copyright(); ?>

This operate will add the next textual content:

© 2009 – 2021

Don’t maintain your copyright dates outdated. Take benefit of this system in your present and future WordPress websites.