Skip to content

Instantly share code, notes, and snippets.

@Sanabria
Created October 17, 2015 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sanabria/af4f51c24d2f8f7ce9d0 to your computer and use it in GitHub Desktop.
Save Sanabria/af4f51c24d2f8f7ce9d0 to your computer and use it in GitHub Desktop.
Product Tag List Widget for WooCommerce by Webbgaraget.
<?php
/**
* Plugin Name: Product Tag List Widget
* Plugin URI:
* Description: A sidebar Widget for displaying the product tags in a list.
* Version: 1.0
* Author: Webbgaraget
* Author URI: http://www.webbgaraget.se
* Tags: plugin, product, tag, list, widget
* License: GPL
*/
/* Add our function to the widgets_init hook. */
add_action( 'widgets_init', array('Tag_List_Widget', 'register_widget') );
class Tag_List_Widget extends WP_Widget {
/* Function that registers our widget. */
public static function register_widget() {
register_widget( 'Tag_List_Widget' );
}
public function __construct() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'tags-list', 'description' => 'A widget that displays a list with the product tags.' );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'tags-list-widget' );
/* Create the widget. */
parent::__construct( 'tags-list-widget', 'Product Tags List Widget', $widget_ops, $control_ops );
}
// Widget output
public function widget( $args, $instance ) {
global $post;
extract( $args );
/* User-selected settings. */
$title = apply_filters('widget_title', $instance['title'] );
/* Before widget (defined by themes). */
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title; ?>
<?php
$terms = get_terms( 'product_tag');
if ($terms) { ?>
<select id="artists" name="artists">
<option value="">Select artist</option>
<? foreach ($terms as $term) { ?>
<option value="/artist/<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
<?php } ?>
</select>
<?php }
/* After widget (defined by themes). */
echo $after_widget;
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags (if needed) and update the widget settings. */
$instance['title'] = strip_tags( $new_instance['title'] );
return $instance;
}
// Widget in admin area
public function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array( 'title' => 'Artists' );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:80%;" />
</p>
<?php
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment