jquery - Wordpress widget dev - undefined index and other problems -


i trying develop first wordpress theme scratch. including widget, should display video in according sidebar starts playing in view. use jquery.inview.js checking if video inview. widget should provide video link options. guess far best practice , clean code appreciated here:

  1. i getting undefined index error, when add widget first time, 2 checkboxes. gone after saving first time. missing here?

  2. how implement <?php checked( $checked, $current, $echo ); ?> displaying checkbox state correctly. general question stuck here.

  3. finally grab video media library , know best way this. tried implement example in wordpress reference bit lost here too.

many thanks, c.

php

<?php class ws_media_widget extends wp_widget {      /**      * register widget wordpress.      */     function __construct() {         parent::__construct(             'ws_media_widget',              esc_html__( 'featured media', 'text_domain' ),              array( 'description' => esc_html__( 'mediaplay autoplay , overlay', 'text_domain' ), )         );     }      /**      * front-end display of widget.      *      * @see wp_widget::widget()      *      * @param array $args     widget arguments.      * @param array $instance saved values database.      */     public function widget( $args, $instance ) {         extract( $args );          $media = $instance['media'];         $poster = $instance['poster'];         $link = $instance['link'];         $title = $instance['title'];         $intro = $instance['intro'];         $loop = $instance['loop'];         $autoplay = $instance['autoplay'];          echo $before_widget;     ?>           <video id="hero-video" class="video"               <?php                  if ($autoplay) echo "autoplay ";                  if ($loop) echo "loop ";           ?>>             <source src="<?php echo $media ?>" type="video/mp4" />             <!-- <source src="media/demo.ogv" type="video/ogg" />             <source src="media/demo.webm" type="video/webm" /> -->         </video>         <div id="video-overlay">             <h2><?php echo $title ?></h2>             <div class="video-intro">                 <p><?php echo $intro ?></p>             </div>             <div class="video-call">                 <a href="<?php echo $link ?>">episode starten</a>             </div>         </div>         <div id="scrollnext" class="animated infinite fadein">                 <a href="#latest-posts"></a>         </div>     <?php         echo $after_widget;     }      /**      * back-end widget form.      *      * @see wp_widget::form()      *       * @param array $instance saved values database.      */     public function form( $instance ) {          $media = ! empty( $instance['media'] ) ? $instance['media'] : esc_html__( 'media', 'text_domain' );         $poster = ! empty( $instance['poster'] ) ? $instance['poster'] : esc_html__( 'alternatve poster', 'text_domain' );         $link = ! empty( $instance['link'] ) ? $instance['link'] : esc_html__( 'links to', 'text_domain' );         $title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'new title', 'text_domain' );         $intro = ! empty( $instance['intro'] ) ? $instance['intro'] : esc_html__( 'intro overlay text', 'text_domain' );         $loop = $instance[ 'loop' ] ? 'true' : 'false';         $autoplay = $instance[ 'autoplay' ] ? 'true' : 'false';         echo $loop. $autoplay;         ?>         <p>             <label for="<?php echo esc_attr( $this->get_field_id( 'media' ) ); ?>"><?php esc_attr_e( 'select video', 'text_domain' ); ?></label>              <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'media' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'media' ) ); ?>" type="text" value="<?php echo esc_attr( $media); ?>">         </p>         <p>             <label for="<?php echo esc_attr( $this->get_field_id( 'poster' ) ); ?>"><?php esc_attr_e( 'select poster', 'text_domain' ); ?></label>              <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'poster' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'poster' ) ); ?>" type="text" value="<?php echo esc_attr( $poster ); ?>">         </p>         <p>             <label for="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>"><?php esc_attr_e( 'link to', 'text_domain' ); ?></label>              <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'link' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link' ) ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>">         </p>         <p>             <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'title:', 'text_domain' ); ?></label>              <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">         </p>         <p>             <label for="<?php echo esc_attr( $this->get_field_id( 'intro' ) ); ?>"><?php esc_attr_e( 'intro text:', 'text_domain' ); ?></label>              <textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'intro' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'intro' ) ); ?>"><?php echo esc_attr( $intro ); ?></textarea>         </p>         <p>             <input id="<?php echo esc_attr( $this->get_field_id( 'loop' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'loop' ) ); ?>" type="checkbox" value="1" <?php checked( $instance[ 'loop' ], 'on' ); ?> />              <label for="<?php echo esc_attr( $this->get_field_id( 'loop' ) ); ?>"><?php esc_attr_e( 'loop video', 'text_domain' ); ?></label>          </p>         <p>             <input id="<?php echo esc_attr( $this->get_field_id( 'autoplay' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'autoplay' ) ); ?>" type="checkbox" value="1" <?php checked( $instance[ 'autoplay' ], 'on' ); ?> />              <label for="<?php echo esc_attr( $this->get_field_id( 'autoplay' ) ); ?>"><?php esc_attr_e( 'autoplay video', 'text_domain' ); ?></label>          </p>          <?php      }      /**      * sanitize widget form values saved.      *      * @see wp_widget::update()      *      * @param array $new_instance values sent saved.      * @param array $old_instance saved values database.      *      * @return array updated safe values saved.      */     public function update( $new_instance, $old_instance ) {         $instance = array();          $instance['media'] = ( ! empty( $new_instance['media'] ) ) ? strip_tags( $new_instance['media'] ) : '';         $instance['poster'] = ( ! empty( $new_instance['poster'] ) ) ? strip_tags( $new_instance['poster'] ) : '';         $instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';         $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';         $instance['intro'] = ( ! empty( $new_instance['intro'] ) ) ? strip_tags( $new_instance['intro'] ) : '';         $instance['loop'] = $new_instance['loop'];         $instance['autoplay'] = $new_instance['autoplay'];           return $instance;     }  }  function register_ws_media_widget() {     register_widget( 'ws_media_widget' ); } add_action( 'widgets_init', 'register_ws_media_widget' ); ?> 

javascript

/*check if videos in view */ $('video').on('inview', function(event, isinview) {      if (isinview && $("video").get(0).autoplay) {         console.log('is in view', event);         $('video').trigger('play');   } else {         console.log('out if view', event);         $('video').trigger('pause');     } }); 

replace code

    <p>         <input id="<?php echo esc_attr( $this->get_field_id( 'loop' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'loop' ) ); ?>" type="checkbox" value="1" <?php checked( $instance[ 'loop' ], '1' ); ?> />          <label for="<?php echo esc_attr( $this->get_field_id( 'loop' ) ); ?>"><?php esc_attr_e( 'loop video', 'text_domain' ); ?></label>      </p>     <p>         <input id="<?php echo esc_attr( $this->get_field_id( 'autoplay' ) ); ?>" class="checkbox" name="<?php echo esc_attr( $this->get_field_name( 'autoplay' ) ); ?>" type="checkbox" value="1" <?php checked( $instance[ 'autoplay' ], '1' ); ?> />          <label for="<?php echo esc_attr( $this->get_field_id( 'autoplay' ) ); ?>"><?php esc_attr_e( 'autoplay video', 'text_domain' ); ?></label>      </p> 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -