Skip to content

Instantly share code, notes, and snippets.

@islandjoe
Created October 15, 2016 16:23
Show Gist options
  • Save islandjoe/802d942f49c36db1266692799585a6ba to your computer and use it in GitHub Desktop.
Save islandjoe/802d942f49c36db1266692799585a6ba to your computer and use it in GitHub Desktop.
WordPress sidebar widget for Review custom post type
<?php
/*
Plugin Name: Review Types Sidebar Widget
Plugin URI: http://wanderlusting.me/dia
Description: Describe what your plugin does and why you created it.
Version: 1.0
Author: Arthur Kho
Author URI: https://github.com/islandjoe
*/
// Creating the widget
class ReviewTypes_Widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'reviewtypes_widget',
// Widget name will appear in UI
__('Review Types Widget', 'wpme-lifeph'),
// Widget description
array( 'description' => __( 'Display the review types', 'wpme-lifeph' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
$args = [
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list',
'show_count' => 0,
'hide_empty' => 1,
'use_desc_for_title' => 0,
'child_of' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'exclude' => '',
'exclude_tree' => '',
'include' => '',
'hierarchical' => 0,
'title_li' => __( '' ),
'show_option_none' => __( 'No reviews' ),
'number' => null,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'review-type',
'walker' => null ];
echo '<ul>';
wp_list_categories( $args );
echo '</ul>';
echo "</aside>";
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title'];
}
else {
$title = __( 'New title', 'wpme-lifeph' );
}
// 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>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class wpb_widget ends here
// Register and load the widget
function wpb_load_widget() {
register_widget( 'reviewtypes_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment