WordPress.org

Ready to get started?Download WordPress

Forums

Widget Logic
Conditional date tags? (21 posts)

  1. benz1
    Member
    Posted 3 years ago #

    Is it possible to use the conditional is_date() tag to specify start and end dates for a widget to appear? If so, what would the syntax be?

    Thanks in advance.

    http://wordpress.org/extend/plugins/widget-logic/

  2. alanft
    Member
    Plugin Author

    Posted 3 years ago #

    http://codex.wordpress.org/Conditional_Tags#A_Date_Page seems to say that it's only use is to indicate a date-based archive page (like a year, month or day) list of posts is showing.

    are you actually after a widget for an individual post dated in a specific range, or are you after a specific date-range in the archive list? cos that can be done, just not with is_date

  3. cozbaldwin
    Member
    Posted 3 years ago #

    I think I, too, am looking for what Benz1 wants: A widget that (hopefully via Widget Logic) will only display in the sidebar for a certain period of time and disappear once a date's been reached.

    For instance, a Text widget with a home-made advertisement in it which the client's paid to have displayed until February 14th. I'd love for Widget Logic to know when it's February 14th and stop displaying the content rather than try to remember to log in and deactivate the widget myself.

  4. benz1
    Member
    Posted 3 years ago #

    I've actually gone ahead and had a widget developed to do this. Just beta testing now. Give me a few days and it should be available on the repository.

  5. cozbaldwin
    Member
    Posted 3 years ago #

    Wonderful. Definitely post an update here when it's ready. I'll be using it. But I'd still love to know if Widget Logic can already do this somehow. If not, alanft, consider this a recommendation to add the capability to your most excellent plugin.

  6. zestylemon
    Member
    Posted 3 years ago #

    I too wish dates could be used in Widget Logic. Any word from the developer as to whether this is on the roadmap or not?

    Benz1: Did your widget pass beta testing? Have you added it to the repository?

  7. alanft
    Member
    Plugin Author

    Posted 3 years ago #

    something like

    return mktime(0, 0, 0, 7, 1, 2011) < time() && time() < mktime(0, 0, 0, 7, 31, 2011)

    should only show in July 2011. mktime params are h/m/s/mon/day/year

  8. cozbaldwin
    Member
    Posted 3 years ago #

    Yes! Although, that doesn't work.
    (even with the word "return" taken out, as I don't think that should be there in the first place)

  9. Lynda
    Member
    Posted 3 years ago #

    Use the date function. For example, I programmed a widget to only display if the day is Wednesday:

    date("l")=='Wednesday'

    Any of the PHP date formats should work here.

  10. cozbaldwin
    Member
    Posted 3 years ago #

    Hmmm. Well, not knowing php too well, how would you use the php dates to say "if date is at least May 18 2011 AND is not after May 31 2011"?

  11. Lynda
    Member
    Posted 3 years ago #

    oh, you want to use the date of the post to conditionally display widgets? My use is for the current date to conditionally display widgets.

    I think your way would require another loop to pull out the post date, which sounds messy, if not impossible with this plugin. A better way to do it might be to write some PHP to call for the post date and do the calculations in the header and then pass the resulting variable along to the widget.

    For example, you could grab the post month, day, year and then write an if statement assigning a specific variable value between the dates in question, then use that in the widget box ($myvariable == 'yes')

    That will take some thought though. I'm thinking out loud here in case anyone with more time than I do right now wants to try it.

  12. cozbaldwin
    Member
    Posted 3 years ago #

    No, no. Sorry. To reiterate what I'm looking for, and I think the original poster is looking for: A widget that will only display in the sidebar for a certain period of time and disappear once a date's been reached. (A great tool to have for privately paid ads)

  13. Lynda
    Member
    Posted 3 years ago #

    Ah! Okay, that is doable! This is what you want to put in the widget logic box:

    date("m.d.y") > '05.01.11' && date("m.d.y") < '05.15.11'

    Change 05.01.11 to the start date and 05.15.11 to the end date. Written this way, you need to enter a mm.dd.yy format for the date.

    I hope this helps!

  14. cozbaldwin
    Member
    Posted 3 years ago #

    Well the good news is that solution, like the mktime solution that alanft suggested, doesnt cause any error. The bad news is, neither of them print the widget.

    I even went as far as to use the most complete, specific formatting possible in order to test it:
    date("D M j G:i:s T Y") > 'Wed May 18 14:18:00 EDT 2011' && date("D M j G:i:s T Y") < 'Wed May 18 14:19:00 EDT 2011'

    I changed the time zone to make sure it wouldnt be using my server's time zone. I changed it to daylight time, etc. It just doesn't print anything. I guess something in the widget code itself has to be added to recognize this stuff?

    I just went looking for another logic extender, but I can't find any except for "Dynamic Widgets" -- however from the screenshots, it appears as though you cannot set specific date ranges for specific widgets. The most specific I could get is setting a date range for all Text Widgets on a certain page. :(

    This seems like it would be relatively easy for the Widget Logic programmer to incorporate. I hope he/she does soon.

  15. cozbaldwin
    Member
    Posted 3 years ago #

    I am interested to see if date("l")=='Wednesday' and date("l")=='January 2011'
    will work, but I can't exactly test that without waiting a month.

  16. Lynda
    Member
    Posted 3 years ago #

    The more you include for the date to capture, the more likely it will be that you'll screw up the value so I would definitely recommend less is more.

    I would not recommend testing using a one minute difference, even if you know the server time. It's just too hard to catch the "right time" with so little room for error.

    I tested this on my blog before I replied to you and it worked exactly as expected. What date values did you put in to start with the original code above?

  17. Lynda
    Member
    Posted 3 years ago #

    To test it out, choose dates in the current parameters. If you set one up to display in the month of January (date("l") is not the way to do that as "l" represents weekday) then it shouldn't display when you load your site. If you change that month to May, then it should display when you load your site. If that's true then the code will work as expected.

  18. Lynda
    Member
    Posted 3 years ago #

    Also, do you know what your server time IS? It's possible it's completely off of reality and that's why the values you're entering won't work.

    You should be able to find out your server's time by uploading a file named whatever.php with this line of code in it: <?php phpinfo(); ?>

    If it's wrong, contact your host to fix it.

    date("m.d.y") > '05.01.11' && date("m.d.y") < '05.15.11'

    I tested the code above by setting the end date to 05.20.11, then loaded my page. The widget appeared. Then I set the end date to 05.15.11. The widget disappeared. Worked as expected.

  19. cozbaldwin
    Member
    Posted 3 years ago #

    Ah hah. Ok, ok. Yes that's working! I don't really understand why that one minute testing period didn't work - but changing it to a whole day does.

    So I would recommend to anyone else to use:
    date("m.d.y") > '05.01.11' && date("m.d.y") < '05.15.11'

    Thank you very much!
    (It would still be great if the widget logic programmer added in a little gui to program the start and end dates/times)

  20. Lynda
    Member
    Posted 3 years ago #

    To do the one minute difference, you need to know what your server time is (and I'd still leave out the ":s" and "T" from the date formatting). ultimately, it's just too restrictive to use for testing.

    If you need to display a widget using hour and minute, test it by changing the hour (after you know what your server time is). Once you successfully test it with variables that will produce a "yes" answer and variables that will produce a "no" answer you can set it to whatever hour and minute you want and rest assured that as long as the server time doesn't go wacky it will work.

  21. Lynda
    Member
    Posted 3 years ago #

    So I'm not actually sure if date() returns server time or GMT/UTC. In any case, if an offset is needed, here's an example of how to do it:

    date('m.d.y', strtotime('-4 hours')) > '05.01.11' && date('m.d.y', strtotime('-4 hours')) < '05.15.11'

    The -4 hours should be replaced with your offset to date().

    This is the part that needs to be added to the date function, after the formatting string:

    , strtotime('-4 hours')

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic