How To Highlight New Content For Returning Visitors
The What’s New or What’s Hot page was an early web essential for showing visitors the latest content for a website.
Those pages have all but disappeared but there’s still merit in the idea of telling visitors what has been published since their last visit.
In this WordPress Weekend Project, let’s mix nostalgia with sophistication and highlight new content for returning visitors.
We’re not actually going to build a What’s New page rather we are going to add a tag to the title of those posts that have been published since the last visit.
The tag is a simple span with a class of nslv (new since last visit) and you’ll need to style it accordingly, either updating your theme’s style.css or using a custom CSS plugin.
For testing, I just used the following:
border: 1px solid #0000ff;
First, though, you need to download the plugin from GitHub and install it.
With the plugin installed, new posts will be tagged. On an initial visit nothing will be tagged as the cookie needs to be set, so the easiest way to test is to visit a site, then publish a new post, then refresh the page.
All being well, you should see something like this:
How It Works
This simple plugin uses a cookie to store the date of the visitor’s last visit and compares that date to the publish date of each post as the title is output. If the post has been published since the last visit then the “new tag” is added.
The cookie is set by hooking a custom function to the init action which happens before any output is sent to the browser (otherwise we’d get a “headers already sent” error).
The function uses an expiry date of 7 days and you may want to change this according to how often you publish: just change the 7 at the end of +60+60+24*7 to your preferred number of days.
Just be aware that the cookie will be set to the current datetime on the visit’s landing page, so subsequent views won’t show the tag.
To add the tag, a custom function is added to the the_title filter which gets called every time a title is output.
The function first checks that the call is from within The Loop, that it is not a single post type being displayed and that it is not a page.
It then compares the lastvisit cookie value with the publish_date of the post and if the post has been published since the last visit it adds the new tag.
Relive The Web’s Early Days
This is a simple plugin that will enable those visiting your homepage, in particular, to see what’s been published since their last visit and, with a bit of luck, will encourage them to explore more of your content.
It’s not quite the What’s New page but it’s enough to make us get misty-eyed, isn’t it?