php - Get input type file value inside widget -
i'am creating widget, following functionality: users can add images , displayed on front-end. have admin.php file, when create input type file form. in my.widget.php main widget file. in function:
public function widget( $args, $instance ) { if ( ! isset ( $args['widget_id'] ) ) $args['widget_id'] = $this->id; extract( $args, extr_skip ); $widget_string = $before_widget; $title = isset( $instance[ 'my-file' ] ) ? $instance[ 'my-file' ] : ''; }
i've tried save input type file "my-file", after clicking "save" button, not saved. i've tried $_files[ 'my-file' ]
, still doesn't save. note: if change input type "text", it's saving, file - not. question is: how save input type file inside widget ?
below full code of create widget , save fields in database.
add code in function.php file
require get_template_directory().'/classes/widget.php'; $widgets = new wpb_widget();
add below code in widget.php file have include in above code.
// creating widget
class wpb_widget extends wp_widget { function __construct() { parent::__construct( // base id of widget 'wpb_widget', // widget name appear in ui __('services widget', 'wpb_widget_domain'), // widget description array( 'description' => __( 'sample widget based on services widget', 'wpb_widget_domain' ), ) ); }
// creating widget front-end
// action happens
public function widget( $args, $instance ) { global $wpdb; $title = apply_filters( 'widget_title', $instance['title'] ); $numberofpost = apply_filters( 'widget_title', $instance['numberofpost'] ); // before , after widget arguments defined themes echo $args['before_widget']; if ( ! empty( $title ) ) echo '<div class="glossymenu"><div class="acor">'; echo $args['before_title']; echo '<h2 class="menuitem submenuheader"><span class="glossyspan">'.$title.'</span></h2>';; echo '<div style="text-align:right"> <a href="" class="prevprog"><img src="'.get_template_directory_uri().'/images/inner_prev.png" alt="inner_prev"></a> <a href="" class="nextprog"><img src="'.get_template_directory_uri().'/images/inner_next.png" alt="inner_next"></a> </div>'; echo $args['after_title']; $query = new wp_query(array('post_type' => 'services','posts_per_page'=>$numberofpost,'orderby'=> 'id','order' => 'desc')); $totalrecord = $query->post_count; echo '<div id="programs">'; $mm = 0; while ($query->have_posts()) : $query->the_post(); $thumb_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), array(127, 123) ); if($mm == 0) { echo '<div class="submenu"><ul>'; } $expert = strip_tags(get_the_excerpt()); if (strlen($expert) > 56) { // truncate string $stringcut = substr($expert, 0, 56); // make sure ends in word assassinate doesn't become ass... $expert = substr($stringcut, 0, strrpos($stringcut, ' ')); } echo '<li> <img width="58" src="'.$thumb_image_url[0].'" alt="'.$totalrecord.'" /> <span class="btext">'.get_the_title().'</span> <span class="stext">'.$expert.'</span></li>'; $mm++; if($mm == 4 or ($totalrecord == $mm)) { echo '</ul></div>'; $mm = 0; } endwhile; wp_reset_query(); echo '</div>'; echo '</div></div>'; echo $args['after_widget']; }
// widget backend
public function form( $instance ) { if ( isset( $instance[ 'title' ] ) ) { $title = $instance[ 'title' ]; } else { $title = __( 'services', 'wpb_widget_domain' ); } if ( isset( $instance[ 'numberofpost' ] ) ) { $numberofpost = $instance[ 'numberofpost' ]; } else { $numberofpost = __( '5', 'wpb_widget_domain' ); } // widget admin form ?> <p> <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'title:' ); ?></label> <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 ); ?>" /> </p> <p> <label for="<?php echo $this->get_field_id( 'numberofpost' ); ?>"><?php _e( 'number of posts show:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'numberofpost' ); ?>" name="<?php echo $this->get_field_name( 'numberofpost' ); ?>" type="text" value="<?php echo esc_attr( $numberofpost ); ?>" /> </p> <?php } // updating widget replacing old instances new public function update( $new_instance, $old_instance ) { $instance = array(); $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : ''; $instance['numberofpost'] = ( ! empty( $new_instance['numberofpost'] ) ) ? strip_tags( $new_instance['numberofpost'] ) : ''; return $instance; } }
// class wpb_widget ends here
Comments
Post a Comment