Pizza order time – content with weekly opening hours

This wordpress.org forum thread:

http://wordpress.org/support/topic/how-to-close-pizza-order-site-or-page-outside-business-hours

gave me an idea of a shortcode that can display content during given opening hours in the week and can handle shortcodes inside it.

Maybe this will grow into a plugin in the near future ;-)

The shortcode is

[openinghours] ... some content only available during opening hours ... [/openinghours]

and here is an example of the supported (optional) attributes:

sun="11:00-22:30" // the opening hours every sunday
mon="09:00-23:00" 
tue="09:00-15:00" 
wed="09:00-23:00" 
thu="10:15-14:59" 
fri="09:00-23:00" 
sat="09:00-24:00" 
debug="0" // debug="1" will display debug information
sorry="Sorry, we are closed now. Try again during the opening hours." // text to display when it's closed
holidays="2012-07-04,2012-12-24"  // YYYY-MM-DD, YYYY-MM-DD,...

Here is a full working example (closed every sunday and saturday and open from 09-17 mon-fri. Holidays are 2012-07-04 and 2012-12-24):

[openinghours mon="09:00-17:00" tue="09:00-17:00" wed="09:00-17:00" thu="09:00-17:00" fri="09:00-17:00"  debug="0" holidays="2012-07-04,2012-12-24" sorry="Sorry, we are closed now. Try again during the opening hours."] please order now! [foobar]
[/openinghours]

Here is the code to paste into the functions.php file:

//
// shortcode to display content only on given weekly opening hours
// usage: [openinghours][/openinghours]
// author: Birgir E.
// url: http://xlino.com
// created: 2012-10-18
// version: 1.1
//
function my_openinghours($atts,$content=""){
        extract( shortcode_atts( array(
                'sun' => '', // the opening hours every sunday
                'mon' => '', // the opening hours every monday
                'tue' => '', 
                'wed' => '',
                'thu' => '',
                'fri' => '',
                'sat' => '',
                'sorry' => '', // text to display when it's closed
                'debug' => '0', // debug="1" will display debug information
                'holidays' => '', // fx: holidays="2012-07-04,2012-12-24", the format is YYYY-MM-DD, YYYY-MM-DD,...
        ), $atts ) );

        // current time
        $currT=time();
        $currYMD=date("Y-m-d",$currT);
        $currYMDHIS=date("Y-m-d H:i:s",$currT);
        $currWD=date("w",$currT);

        // get the opening hours for today
        switch ($currWD){
                case 0:
                        $s=$sun;
                        break;
                case 1:
                        $s=$mon;
                        break;
                case 2:
                        $s=$tue;
                        break;
                case 3:
                        $s=$wed;
                        break;
                case 4:
                        $s=$thu;
                        break;
                case 5:
                        $s=$fri;
                        break;
                case 6:
                        $s=$sat;
                        break;
                default:
                        $s="";
        }

        // format input
        $out=esc_attr($sorry);
        $holidays=esc_attr($holidays);
        $s=esc_attr(trim($s));
        $debug=(int)$debug;
 
        // do the time checks
        if(strlen($s)>0){
                $a=explode("-",$s);
                if(count($a)==2){
                        $from = $currYMD." ".$a[0].":00";
                        $to = $currYMD." ".$a[1].":00";
                        $fromT = strtotime($from);
                        $toT = strtotime($to);
                        if($currT>$fromT && $currT<$toT && strpos($holidays,$currYMD)===false){
                                $out=do_shortcode($content);
                        }
                }
        }
        // check if we have debug mode on
        if($debug=="1"){
                $out.="<div style='background-color:#ccc;padding:10px;border:1px solid #333;margin:10px;'>Debug: <br/>";
                $out.="from:".$from."<br/> current:".$currYMDHIS."<br/> to:".$to."</div>";
        }
        return $out;
}

add_shortcode("openinghours","my_openinghours");

bon appetit ;-)

changelog:

version 1.1
added holidays

version 1.0
inital version (Props: @lemonadejoex, @keesiemeijer)

2 thoughts on “Pizza order time – content with weekly opening hours

  1. thanks @keesiemeijer

    I thought this format would be handy:

    holidays=”2012-07-04,2012-12-24″

    where the form is

    YYYY-MM-DD, YYYY-MM-DD,…

    i.e. comma seperated string.

    I use this code to check

    strpos($holidays,$currYMD)===false

    Any other ideas are welcome ;-)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*