Copyright Footer Plugin Update

Added Date Range

After releasing the Copyright Footer plugin, a user on Twitter, @HeadsUpWP suggested changing the Copyright date from just the current year, to a range of dates. This is intended to cover all content from the beginning to current.

Nailing the Current Year for the range was not a problem. But I had a few options to find out the Beginning Year. I could loop through and find the first blog post, and get the first date. That seemed like it could be a little expensive if the site had a ton of posts, and if the site didn’t actually use a blog, there was a chance that this option wouldn’t work.

What I decided to use was Users. Every site has at least one user, and I could loop through to find the first user. I didn’t want to hard code the User ID, as there’s a potential for the original Admin to be deleted.
I felt that looping through the existing users, and finding out the oldest registered user on the site gave me the most sensible approach to finding the Beginning Year for the Copyright Date Range.

PHP
    // Loop through users, and get the earliest date.
    $userDates = [];
    $users = get_users();
    foreach( $users as $user ) {
        $udata = get_userdata( $user->ID );
        $userDates[] = $udata->user_registered;
    }
    $startYear = date( "Y", strtotime( min($userDates) ) );

    // Create a date range if $startDate is less than currentDate.
    $dateRange = '';
    if ( $startYear < $currentYear ) {
        $dateRange = $startYear . ' - ' . $currentYear;
    } else {
        $dateRange = $currentYear;
    }

You can see in lines 2-8, I create an array, loop through all users, add the registered dates to the array, and ultimately format the dates into Year, and set $startYear as the smallest number in that array.

Yes, I camel cased my PHP, because I’m an idiot that works in JS and PHP. I’ll fix it back to snake case on an future update.

Speaking of Javascript…

What about Edit.js?

Getting the user regsitration via PHP is pretty straightforward. There’s obviously the get_userdata() function to retrieve that info. But what about in Javascript?

Well, just like when we got the Site Title initially using the Core Data Module, we can use that to get User Info as well.

JSX
	// Get list of users and make a date range
	const users = wp.data.select("core").getUsers()
	let userStartDates = []
	let startYear = ""
	let dateRange = ""

	if (null !== users) {
		users.map((user) => {
			userStartDates.push(new Date(user.registered_date).getFullYear())
		})
		startYear = Math.min(parseInt(userStartDates))

		if (startYear < year) {
			dateRange = `${startYear} - ${year}`
		} else {
			dateRange = year
		}
	}

You can see on line 2 above, I get all Users by calling wp.data.select("core").getUsers() then I essentially recreate what I did with my PHP, but in Javascript. Mapping over all users and adding the years into an array then grabbing the smallest (earliest year) from that array.

Block Error: Unexpected or Invalid Content

Yes, the dreaded error block developers encounter all the time while working on or updating their blocks.

Fortunately because there is nothing changing on the Save.js file, users will not encounter this error. The frontend text will still be replaced via the render_block hook, and likewise in the Admin, the JS will load that live.
The Save.js file remains the same. So users don’t get that error.

Go get it!

So after you’ve read all that, it’s time to go get it and add the Copyright Footer to your FSE theme. It’s officially a 1.0 release, and on the WordPress plugin repo.

Leave a Reply

Your email address will not be published. Required fields are marked *