Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.19 KB | None | 0 0
  1. function todayhours_load_widget() {
  2.     register_widget( 'todayhours_widget' );
  3. }
  4. add_action( 'widgets_init', 'todayhours_load_widget' );
  5.  
  6. // Creating the widget
  7. class todayhours_widget extends WP_Widget {
  8.  
  9. function __construct() {
  10. parent::__construct(
  11.  
  12. // Base ID of your widget
  13. 'todayhours_widget',
  14.  
  15. // Widget name will appear in UI
  16. __('Today Hours', 'todayhours_widget_domain'),
  17.  
  18. // Widget description
  19. array( 'description' => __( 'Display today\'s business hours', 'todayhours_widget_domain' ), )
  20. );
  21. }
  22.  
  23. // Creating widget front-end
  24.  
  25. public function widget( $args, $instance ) {
  26. $title = apply_filters( 'widget_title', $instance['title'] );
  27.  
  28. // before and after widget arguments are defined by themes
  29. echo $args['before_widget'];
  30. if ( ! empty( $title ) )
  31. echo $args['before_title'] . $title . $args['after_title'];
  32.  
  33. // This is where you run the code and display the output
  34.  
  35. switch (date("w")) {
  36.     // w = day of week, 0 = sunday, 6 = saturday
  37.     case 0:
  38.         $d = "Closed";
  39.         break;
  40.     case 1:
  41.         $d = "9:00am - 12:00pm, 2:00pm - 6:00pm";
  42.         break;
  43.     case 2:
  44.         $d = "2:00pm - 6:00pm";
  45.         break;
  46.     case 3:
  47.         $d = "9:00am - 12:00pm, 2:00pm - 6:00pm";
  48.         break;
  49.     case 4:
  50.         $d = "Closed";
  51.         break;
  52.     case 5:
  53.         $d = "9:00am - 1:00pm";
  54.         break;
  55.     case 6:
  56.         $d = "Closed";
  57.         break;
  58. }
  59. echo __( $d, 'todayhours_widget_domain' );
  60. echo $args['after_widget'];
  61. }
  62.          
  63. // Widget Backend
  64. public function form( $instance ) {
  65. if ( isset( $instance[ 'title' ] ) ) {
  66. $title = $instance[ 'title' ];
  67. }
  68. else {
  69. $title = __( 'New title', 'todayhours_widget_domain' );
  70. }
  71. // Widget admin form
  72. ?>
  73. <p>
  74. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  75. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
  76. </p>
  77. <?php
  78. }
  79.      
  80. // Updating widget replacing old instances with new
  81. public function update( $new_instance, $old_instance ) {
  82. $instance = array();
  83. $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
  84. return $instance;
  85. }
  86. } // Class wpb_widget ends here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement