I released a plugin quite a while ago that was pretty simplistic. It provided two different shortcodes: one for showing content after a given time/date and another for hiding content after a given date/time. That plugin is Show/Hide Content at Set Time.
It was one of my first adventures into WordPress development and thus it’s a distinctively simple plugin, but it meets a very real need, so I released it on the wordpress.org repository and it has been downloaded a few thousand times since.
I have (regrettably) neglected it for a while, but the other day, I got another question about whether the plugin could do something. The answer was no, but it’s a question I’ve been asked before, so it’s clearly something that’s needed. I decided to bite the bullet and get down to coding version 2.0 of the plugin, covering this and a number of other much-needed features, as well as improving and rewriting the codebase.
The new and improved plugin now performs three additional functions that previously weren’t possible:
- You can set a start time and an end time for the same piece of content – previously you could only either start it or end it at a given time, not both.
- You can show and hide content on a recurring schedule. Your current options are daily, weekly and monthly.
- You can nest shortcodes inside, so that your shortcode content renders correctly within the time limits you set.
I have retired the old shortcodes (though they will continue to work), but the new shortcodes now replace them. Another tweak is that the plugin now using the site’s local time, rather than the server time.
Here’s some usage examples, with more details on how to use it on the wordpress.org repository page:
Show content at a give time
[time-restrict on="2014-10-04 08:00:00"]WordCamp Tampa is finally here![/time-restrict]
Hide content after a set time
[time-restrict off="2015-02-19 02:59:59"]I'm still 29 years old![/time-restrict]
Show content for a set period of time
[time-restrict on="2015-02-19 03:00:00" off="2016-02-19 02:59:59"]I am 30 years old.[/time-restrict]
Show content every week between 4pm Friday and 10pm Sunday
[time-restrict-repeat type="weekly" onday="Friday" ontime="16:00:00" offday="Sunday" offtime="22:00:00"]Time to party![/time-restrict-repeat]
Show content every day from 8am to 5pm
[time-restrict-repeat type="daily" ontime="08:00:00" offtime="17:00:00"]Get to work![/time-restrict-repeat]
Show content every month from 8am on the 3rd to 12pm on the 10th
[time-restrict-repeat type="monthly" ondate="03" ontime="08:00:00" offdate="10" offtime="12:00:00"]Something special happens at this time of the month[/time-restrict-repeat]
Added: October 3, 2014
Combine shortcodes to create even more complex schedules
You can nest two or three shortcodes inside one another to create an AND logical relationship, where both/all conditions must be met in order to display the content. Note that you need to add a -2 or a -3 to distinguish between subsequent shortcodes since WordPress does not support nesting the same shortcode.
For example, to only display content on the third Wednesday of the month, you can use the facts that you only want content to display on Wednesdays AND between the 15th and 21st of the month (the third week of the month). Only when both of these conditions are met will the content be shown:
[time-restrict-repeat type="monthly" ondate="15" offdate="21"]
[time-restrict-repeat-2 type="weekly" onday="Wednesday" offday="Wednesday"]
Hurray! It's the third Wednesday of the month, which is clearly worth celebrating!
So, how do you plan to use this plugin to show or hide content? Let me know if the plugin needs yet more capabilities that it doesn’t yet allow: I’d love to build them for you.