Skip to content

Instantly share code, notes, and snippets.

@LinzardMac
Created February 25, 2015 22:16
Show Gist options
  • Save LinzardMac/b6263de18e0d25ce4527 to your computer and use it in GitHub Desktop.
Save LinzardMac/b6263de18e0d25ce4527 to your computer and use it in GitHub Desktop.
Pods Framework widget that gets a list of pods with or without a "featured" checkbox
class Custom_Widget_Pods_Testimonials extends WP_Widget {
public function __construct() {
$widget_ops = array('classname' => 'widget_pods_testimonials', 'description' => __( "Featured Testimonials.") );
parent::__construct('pods_testimonials', __('Featured Testimonials'), $widget_ops);
$this->alt_option_name = 'widget_pods_testimonials';
add_action( 'save_post', array($this, 'flush_widget_cache') );
add_action( 'deleted_post', array($this, 'flush_widget_cache') );
add_action( 'switch_theme', array($this, 'flush_widget_cache') );
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['numberOfTestimonials'] = strip_tags($new_instance['numberOfTestimonials']);
$instance['allTestimonials']= strip_tags($new_instance['allTestimonials']);
return $instance;
}
function form($instance) {
if( $instance) {
$title = esc_attr($instance['title']);
$numberOfTestimonials = esc_attr($instance['numberOfTestimonials']);
$allTestimonials = esc_attr($instance['allTestimonials']);
} else {
$title = '';
$numberOfTestimonials = '';
$allTestimonials = '';
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'widget_pods_testimonials'); ?></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 $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('allTestimonials'); ?>"><?php _e('Featured Only?', 'widget_pods_testimonials'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('allTestimonials'); ?>" name="<?php echo $this->get_field_name('allTestimonials'); ?>" type="checkbox" <?php if($allTestimonials == 'on'){ echo 'checked';} ?> />
</p>
<p>
<label for="<?php echo $this->get_field_id('numberOfTestimonials'); ?>"><?php _e('Number of Listings:', 'widget_pods_testimonials'); ?></label>
<select id="<?php echo $this->get_field_id('numberOfTestimonials'); ?>" name="<?php echo $this->get_field_name('numberOfTestimonials'); ?>">
<?php for($x=1;$x<=10;$x++): ?>
<option <?php echo $x == $numberOfTestimonials ? 'selected="selected"' : '';?> value="<?php echo $x;?>"><?php echo $x; ?></option>
<?php endfor;?>
</select>
</p>
<?php
}
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
$numberOfTestimonials = $instance['numberOfTestimonials'];
$allTestimonials = $instance['allTestimonials'];
echo $before_widget;
if ( $title ) {
echo $before_title . $title . $after_title;
}
$this->getPodsTestimonials($numberOfTestimonials, $allTestimonials);
echo $after_widget;
}
function getPodsTestimonials($numberOfTestimonials, $allTestimonials) { //html
$testimonials = pods('testimonial');
if($allTestimonials=='on'){
$whereParams = "featured.meta_value = '1'";
}else{
$whereParams = '';
}
$params = array(
'search' => false,
'pagination' => false,
'where' => $whereParams ,
'limit' => $numberOfTestimonials,
'orderby' =>'name ASC'
);
$testimonials->find($params);
$total_testimonials = $testimonials->total();
if( $total_testimonials > 0 ) {
while ( $testimonials->fetch() ) {
$testimonial_name = $testimonials->field('name');
$testimonial_content = $testimonials->field('testimonial');
echo $testimonial_content . '</br">';
echo $testimonial_name;
}
}else{
echo '<p style="padding:25px;">No listing found</p>';
}
}
} // end class build widget
function testimonials_register_widgets() {
register_widget( 'Custom_Widget_Pods_Testimonials' );
}
add_action( 'widgets_init', 'testimonials_register_widgets' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment