How to show and hide content at set times

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 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 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!
[/time-restrict-repeat-2] [/time-restrict-repeat]

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.

Get the plugin

15 thoughts on “How to show and hide content at set times”

  1. Toni says:

    You do realize your example here is wrong. On= and Off= should be show= and hide=.

    1. I didn’t, but thank goodness for eagle-eyed readers like you! Fixed.


    2. Hi Toni,

      Just wanted to add that I made the decision to change the attributes for the [time-restrict] shortcode to on and off, to be more in line with the rest of the plugin, which uses attributes like onday, offday, ondate, offtime etc. The old show and hide attributes will continue to work however.

  2. Joonas says:

    How I can show some content on every second Monday?

    1. Currently, that kind of a schedule is not supported.

      Actually, here’s an idea… The second Monday will always be somewhere between the 8th and 14th of the month. You could nest two shortcodes to achieve the same effect. Try this and let me know if it works:

      [time-restrict-repeat type="monthly" ondate="08" offdate="14"]
      [time-restrict-repeat type="weekly" onday="Monday" offday="Monday"]
      Your content here

  3. I am trying to use this for a restaurant website. I want their lunch specials to only show Monday-Friday, from 7am-5 pm, but not on Saturday or Sunday. Here is my code so far: [time-restrict-repeat type=”daily” onday=”Monday” ontime=”14:00:00″ offday=”Friday” offtime=”23:59:00″] – Is there a type called weekdays? – I imagine I could imbed two in a sort of cascading logic. Anyway… Thanks for this great script. I looked and looked, but this is the only thing that did exactly what I wanted. (Specials Page)

    1. Hi Robert, the easiest way to achieve this is to combine two shortcodes and nest them together. So:

      Condition 1: Show only on weekdays: [time-restrict-repeat type=”weekly” onday=”Monday” offday=”Friday”]
      Condition 2: Show only between 7am and 5pm: [time-restrict-repeat type=”daily” ontime=”07:00:00″ offtime=”17:00:00″]

      Only when both of these are met do you want the content to show, so by combining them in a nested format, you can achieve exactly that:

      [time-restrict-repeat type="weekly" onday="Monday" offday="Friday"]
      [time-restrict-repeat-2 type="daily" ontime="07:00:00" offtime="17:00:00"]
      Lunch specials information here

      Hope that helps!

  4. What if your site uses caching? Won’t this interfere with the correct display?

    1. That is correct. The output will only be correct when the page is generated, so if you bypass creating pages on the fly by caching, then your content won’t accurately reflect your show/hide settings

  5. Got it Dave. I’ve recommended to users, in my own site’s review of your plug-in, that they disable caching for the pages using your shortcodes. It’s not perfect, but (for me) the simplicity of your plug-in outweighs the caching ‘niggle’. Thanks.

    1. Perfect Graham. And thanks for the write-up. Much appreciated!

  6. Lori says:

    Hi Dave,

    Would you be willing to help me customize a code that not only “hides” the content, but actually RESTRICTS it, after a certain amount of time per day? Because with your plugin, all they have to do is refresh the page, to restart the timer.

    I need something that will allow my free members to only be allowed to view certain content of the site, for a maximum amount of minutes per day. .And if they want to take the time limits off, they need to upgrade their membership.

    It baffles me how there is not ANY plugins that support this.

    If you know of any please let me know. Otherwise I am also willing to pay you to customize this for me if it won’t be too much work and therefore expensive on my end.


    1. Hi Lori,

      I’m afraid that this is a very different application than this plugin and I am unable to develop such a plugin at this time due to other commitments. I’d be interested if someone does develop such a plugin though.

  7. natep1833 says:

    Hello There!

    I have been using your plugin and it has been working quite well. However, after I updated my php it doesn’t seem to work anymore. Is there a way we can fix this?

    1. What did you update to?

Leave a Reply