Skip to content

Instantly share code, notes, and snippets.

@toekneestuck
Created September 25, 2012 15:29
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 toekneestuck/3782602 to your computer and use it in GitHub Desktop.
Save toekneestuck/3782602 to your computer and use it in GitHub Desktop.
Configurable Facebook Likebox
/**
* Facebook Widget
* - Display the Facebook like box in all of it's configuration glory
*
*/
class GT_Social_Facebook extends WP_Widget {
function GT_Social_Facebook(){
$widget_ops = array( 'classname' => 'facebook-like-box', 'description' => _x('A widget for displaying a Facebook like box.', 'gt') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'gt-facebook-like' );
parent::WP_Widget( false, _x('Facebook Widget', 'gt'), $widget_ops);
}
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => 'Join Us on Facebook', 'url' => '', 'show_faces' => false, 'show_stream' => false, 'show_header' => false, 'show_one_row_faces' => false, 'width' => 280, 'show_title' => 'on' ) );
$wide = 390;
$narrow = 280;
?>
<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title', 'gt'); ?></label> &nbsp; (<label class="nonessential" for="<?php echo $this->get_field_name('show_title'); ?>"><input type="checkbox" name="<?php echo $this->get_field_name('show_title'); ?>" id="<?php echo $this->get_field_id('show_title'); ?>" <?php checked( $instance['show_title'], 'on' ); ?> /> <?php _e('Show', 'gt') ?></label>)<br />
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php if ( isset($instance['title']) ) echo $instance['title']; ?>" class="widefat" />
</p>
<p><label for="<?php echo $this->get_field_id( 'url' ); ?>"><?php _e('Facebook URL', 'gt'); ?></label><br />
<input id="<?php echo $this->get_field_id( 'url' ); ?>" name="<?php echo $this->get_field_name( 'url' ); ?>" value="<?php if ( isset($instance['url']) ) echo $instance['url']; ?>" class="widefat" />
</p>
<p><?php _e('Options', 'gt'); ?></p>
<p><label for="<?php echo $this->get_field_name('show_faces'); ?>"><input type="checkbox" name="<?php echo $this->get_field_name('show_faces'); ?>" id="<?php echo $this->get_field_id('show_faces'); ?>" <?php checked( $instance['show_faces'], 'on' ); ?> /> <?php _e('Show Faces', 'gt') ?></label> (<label for="<?php echo $this->get_field_name('show_one_row_faces'); ?>"><input type="checkbox" name="<?php echo $this->get_field_name('show_one_row_faces'); ?>" id="<?php echo $this->get_field_id('show_one_row_faces'); ?>" <?php checked( $instance['show_one_row_faces'], 'on' ); ?> /> <?php _e('Only 1 Row', 'gt') ?></label>)</p>
<p><label for="<?php echo $this->get_field_name('show_stream'); ?>"><input type="checkbox" name="<?php echo $this->get_field_name('show_stream'); ?>" id="<?php echo $this->get_field_id('show_stream'); ?>" <?php checked( $instance['show_stream'], 'on' ); ?> /> <?php _e('Show Stream', 'gt') ?></label></p>
<p><label for="<?php echo $this->get_field_name('show_header'); ?>"><input type="checkbox" name="<?php echo $this->get_field_name('show_header'); ?>" id="<?php echo $this->get_field_id('show_header'); ?>" <?php checked( $instance['show_header'], 'on' ); ?> /> <?php _e('Show Header', 'gt') ?></label></p>
<p><label for="<?php echo $this->get_field_id( 'width' ); ?>"><?php _e('Width Preset', 'gt'); ?></label><br />
<select name="<?php echo $this->get_field_name( 'width' ); ?>" id="<?php echo $this->get_field_id( 'width' ); ?>">
<option value="<?php echo $wide ?>" <?php selected( $instance['width'], $wide ) ?>><?php _e('Wide', 'gt') ?></option>
<option value="<?php echo $narrow ?>" <?php selected( $instance['width'], $narrow ) ?>><?php _e('Narrow', 'gt') ?></option>
</select></p>
<?php
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Create widget settings instances. */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['url'] = isset($new_instance['url']) ? strip_tags( $new_instance['url'] ) : false;
$instance['show_title'] = isset($new_instance['show_title']) ? strip_tags( $new_instance['show_title'] ) : false;
$instance['show_faces'] = isset($new_instance['show_faces']) ? strip_tags( $new_instance['show_faces'] ) : false;
$instance['show_one_row_faces'] = isset($new_instance['show_one_row_faces']) ? strip_tags( $new_instance['show_one_row_faces'] ) : false;
$instance['show_stream'] = isset($new_instance['show_stream']) ? strip_tags( $new_instance['show_stream'] ) : false;
$instance['show_header'] = isset($new_instance['show_header']) ? strip_tags( $new_instance['show_header'] ) : false;
$instance['width'] = isset($new_instance['width']) ? strip_tags( $new_instance['width'] ) : 280;
return $instance;
}
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', $instance['title'] );
$url = urlencode( $instance['url'] );
$show_title = !empty($instance['show_title']) ? true : false;
$show_faces = !empty($instance['show_faces']) ? true : false;
$show_stream = !empty($instance['show_stream']) ? true : false;
$show_header = !empty($instance['show_header']) ? true : false;
$one_row_faces = !empty($instance['show_one_row_faces']) ? true : false;
// Faces, Stream & Header
if( $show_faces && $show_stream && $show_header )
$height = 556;
// Faces & Stream
elseif( $show_faces && $show_stream && !$show_header )
$height = 558;
// Faces & Header
elseif( $show_faces && !$show_stream && $show_header )
$height = 290;
// Stream & Header
elseif( !$show_faces && $show_stream && $show_header )
$height = 427;
// Header or None Checked
elseif( (!$show_faces && !$show_stream && $show_header) || (!$show_faces && !$show_stream && !$show_header) )
$height = 62;
// Faces & Only One Row
elseif( $one_row_faces && $show_faces && !$show_stream && !$show_header )
$height = 183;
// Faces Only
elseif( $show_faces && !$show_stream && !$show_header )
$height = 256;
// Stream Only
elseif( !$show_faces && $show_stream && !$show_header )
$height = 395;
// Default
else
$height = 62;
$width = $instance['width'] ? $instance['width'] : 280;
$show_faces = $show_faces ? 'true' : 'false';
$show_stream = $show_stream ? 'true' : 'false';
$show_header = $show_header ? 'true' : 'false';
/* Widget themplate */
echo $before_widget;
if( $show_title)
echo $before_title . $title . $after_title;
echo '<iframe src="http://www.facebook.com/plugins/likebox.php?href='. $url . '&amp;width=' . $width . '&amp;colorscheme=light&amp;show_faces=' . $show_faces . '&amp;border_color=white&amp;stream=' . $show_stream . '&amp;header=' . $show_header . '&amp;height=' . $height . '" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:' . $width . 'px; height:' . $height . 'px;" allowTransparency="true"></iframe>';
echo $after_widget;
}
}
/* Register widgets */
function gt_register_widgets() {
register_widget( 'GT_Social_Facebook' );
}
/* Load widget with widget_init function */
add_action( 'widgets_init', 'gt_register_widgets' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment