Install
Upload it under Plugins → Add New Plugin → Upload Plugin, then activate.
Install straight from GitHub with WP-CLI:
wp plugin install https://github.com/radesrb/sho-current-year/releases/download/v1.0.0/sho-current-year-1.0.0.zipA small WordPress plugin that keeps the year in your footer (or anywhere else) from going stale.
Select some text in the block editor, click Current Year in the toolbar, and that text gets swapped for the current year when the page renders. No shortcodes, no regex, no settings page.
Why I made this
Every client site I've worked on has a "© 2021" sitting in a footer somewhere. Someone always forgets, and then someone else notices in March and sends an email about it.
The usual fixes are a shortcode, which editors have to remember and type, or a custom block, which is a lot of machinery for four digits. I wanted something that feels like bolding a word: highlight it, click a button, done. WordPress's Format API is built for exactly that kind of thing, so that's what this uses.
It was also a good excuse to use WP_HTML_Tag_Processor for a real job instead of just reading about it.
How it works
There are two halves.
In the editor, a small script registers a format type called sho/current-year. It adds a toolbar button that wraps the selected text in <span class="sho-current-year">. A dotted underline shows up under that text in the editor so you can tell it's dynamic. The underline is editor-only and never appears on the front end.
On the front end, a filter on render_block looks through the block's HTML for that span and replaces the text inside it with the current year. It uses WP_HTML_Tag_Processor to walk the markup, so there's no regex guessing at HTML. If the marker class isn't in the HTML at all, the function exits right away with a plain str_contains() check, so blocks that don't use the feature pay almost nothing.
The year comes from wp_date( 'Y' ), so it follows the timezone set in Settings → General and flips at your local midnight, not UTC midnight.
Requirements
- WordPress 6.7 or newer (
set_modifiable_text()on the Tag Processor arrived in 6.7) - PHP 8.3 or newer
Installation
From the zip file
- Download
sho-current-year-1.0.0.zipfrom the Releases page. - In your WordPress dashboard, go to Plugins → Add New Plugin.
- Click Upload Plugin, choose the zip file, and click Install Now.
- Click Activate Plugin.
Manual install
- Unzip the download and copy the
sho-current-yearfolder intowp-content/plugins/. - Activate it under Plugins.
There's no build step. The JavaScript is plain ES5 that uses the wp.* globals, so there's nothing to compile.
Usage
Open any post, select some text, and click the calendar icon in the block toolbar. The text will show a dotted underline in the editor and turn into the current year on the front end.
Files
sho-current-year/
├── sho-current-year.php # plugin header, enqueues, the render_block filter
├── js/editor.js # registers the format and the toolbar button
└── css/editor.css # the dotted underline, editor only
Known limitations
I'd rather list these than have you find them :)
- Nested spans end the match early. The Tag Processor doesn't track nesting depth, so any
</span>closes the marker. With core formats this almost never comes up, but a custom format that also outputs a<span>inside the marked text could trigger it. - Page caching. If a caching plugin or CDN serves a stored copy of the page, the year only updates when that cache is cleared. For a footer year this is usually fine, but it will surprise you right after New Year.
- Block editor only. There's no Classic Editor button and no shortcode fallback, on purpose.
License
GPL-2.0-or-later, same as WordPress.
Author
Rade Eric Studio Hornet
Releases
1 release. Each count is every asset in that release; expand a row for the breakdown.