SHARE
TWEET

Untitled

5ally Aug 25th, 2018 118 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class My_Widget extends WP_Widget {
  3.  
  4.     public function __construct() {
  5.         $widget_ops = array(
  6.             'classname' => 'my_widget',
  7.             'description' => __( 'Description.' ),
  8.             'customize_selective_refresh' => true,
  9.         );
  10.         $control_ops = array(
  11.             'width' => 400,
  12.             'height' => 350,
  13.         );
  14.         parent::__construct( 'my_widget', __( 'My Widget' ), $widget_ops, $control_ops );
  15.     }
  16.  
  17.     public function widget( $args, $instance ) {
  18.       extract( $args );
  19.       $checkbox1 = ! empty( $instance['checkbox1'] ) ? $instance['checkbox1'] : false;
  20.       $checkbox2 = ! empty( $instance['checkbox2'] ) ? $instance['checkbox2'] : false;
  21.       $checkbox3 = ! empty( $instance['checkbox3'] ) ? $instance['checkbox3'] : false;
  22.  
  23.       echo $args['before_widget'];
  24.       echo "Hello world, just testing this out.";
  25.       echo $args['after_widget'];
  26.     }
  27.  
  28.     public function form( $instance ) {
  29.  
  30.       $defaults = array(
  31.         'checkbox1' => false, // not checked by default
  32.         'checkbox2' => true,  // checked by default
  33.         'checkbox3' => true,  // checked by default
  34.       );
  35.  
  36.       $instance = wp_parse_args( (array) $instance, $defaults );
  37.       ?>
  38.  
  39.     <p>
  40.       <input
  41.         class="checkbox"
  42.         id="<?php echo esc_attr( $this->get_field_id( 'checkbox1' ) ); ?>"
  43.         name="<?php echo esc_attr( $this->get_field_name( 'checkbox1' ) ); ?>"
  44.         type="checkbox"
  45.         <?php checked( '1', $instance['checkbox1'] ); ?>
  46.         value="1">
  47.  
  48.       <label for="<?php echo esc_attr( $this->get_field_id( 'checkbox1' ) ); ?>">
  49.         <?php _e( 'Checkbox #1', 'test' ); ?>
  50.       </label>
  51.     </p>
  52.  
  53.     <p>
  54.       <input
  55.         class="checkbox"
  56.         id="<?php echo esc_attr( $this->get_field_id( 'checkbox2' ) ); ?>"
  57.         name="<?php echo esc_attr( $this->get_field_name( 'checkbox2' ) ); ?>"
  58.         type="checkbox"
  59.         <?php checked( '1', $instance['checkbox2'] ); ?>
  60.         value="1">
  61.  
  62.       <label for="<?php echo esc_attr( $this->get_field_id( 'checkbox2' ) ); ?>">
  63.         <?php _e( 'Checkbox #2', 'test' ); ?>
  64.       </label>
  65.     </p>
  66.  
  67.     <p>
  68.       <input
  69.         class="checkbox"
  70.         id="<?php echo esc_attr( $this->get_field_id( 'checkbox3' ) ); ?>"
  71.         name="<?php echo esc_attr( $this->get_field_name( 'checkbox3' ) ); ?>"
  72.         type="checkbox"
  73.         <?php checked( '1', $instance['checkbox3'] ); ?>
  74.         value="1">
  75.  
  76.       <label for="<?php echo esc_attr( $this->get_field_id( 'checkbox3' ) ); ?>">
  77.         <?php _e( 'Checkbox #2', 'test' ); ?>
  78.       </label>
  79.     </p>
  80.  
  81.       <?php
  82.     }
  83.  
  84.     public function update( $new_instance, $old_instance ) {
  85.  
  86.       $instance = $old_instance;
  87.  
  88.       $instance['checkbox1'] = isset( $new_instance['checkbox1'] ) ? 1 : false;
  89.       $instance['checkbox2'] = isset( $new_instance['checkbox2'] ) ? 1 : false;
  90.       $instance['checkbox3'] = isset( $new_instance['checkbox3'] ) ? 1 : false;
  91.  
  92.       return $instance;
  93.     }
  94. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top