Skip to content

Instantly share code, notes, and snippets.

@saroarhossain57
Created September 28, 2016 07:10
Show Gist options
  • Save saroarhossain57/cb65ae8c83320156ffb159753d97af05 to your computer and use it in GitHub Desktop.
Save saroarhossain57/cb65ae8c83320156ffb159753d97af05 to your computer and use it in GitHub Desktop.
if( ! function_exists('function_name') ){
function function_name(){
register_widget('class_name');
}
add_action('widgets_init', 'function_name');
}
class class_name extends WP_Widget{
function __construct(){
parent::__construct(
'class_name',
__('Widget Display Name', 'text-domain'),
array('description' => __('widget description', 'text-domain'))
);
}
public function widget($args, $instance){
extract( $args );
$title = apply_filters( 'widget_title', $instance[ 'title' ] );
$show_tag = $instance['show_tag'];
// The following variable is for a checkbox option type
$post_count = $instance[ 'post_count' ] ? 'true' : 'false';
echo $args['before_widget'];
if ( $title ) {
echo $before_title . $title . $after_title;
}
echo $args['after_widget'];
}
public function form( $instance ) {
$defaults = array( 'title' => __( 'Default Title', 'bestblog' ), 'post_count' => 'on' );
$instance = wp_parse_args( ( array ) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">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( $instance[ 'title' ] ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'show_tag' ); ?>">Number of tags to show:</label>
<input class="tiny-text" id="<?php echo $this->get_field_id( 'show_tag' ); ?>" name="<?php echo $this->get_field_name( 'show_tag' ); ?>" type="text" value="<?php echo esc_attr( $instance[ 'show_tag' ] ); ?>" />
</p>
<!-- The checkbox -->
<p>
<input class="checkbox" type="checkbox" <?php checked( $instance[ 'post_count' ], 'on' ); ?> id="<?php echo $this->get_field_id( 'post_count' ); ?>" name="<?php echo $this->get_field_name( 'post_count' ); ?>" />
<label for="<?php echo $this->get_field_id( 'post_count' ); ?>">Show Tag Post Count</label>
</p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
$instance[ 'show_tag' ] = $new_instance[ 'show_tag' ];
// The update for the variable of the checkbox
$instance[ 'post_count' ] = $new_instance[ 'post_count' ];
return $instance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment