Skip to content

Instantly share code, notes, and snippets.

@richardtape
Created June 28, 2012 15:34
Show Gist options
  • Save richardtape/3012042 to your computer and use it in GitHub Desktop.
Save richardtape/3012042 to your computer and use it in GitHub Desktop.
Simple WordPress widget for the ResponsiveSlides Slider - uses a post category to load posts
<?php
if( !class_exists( 'friendly_responsiveslides_slider' ) )
{
/**
* Friendly ResponsiveSlider using http://responsive-slides.viljamis.com/
*
* @package Earth
* @author iamfriendly
* @version 1.0
* @since 1.0
*/
class friendly_responsiveslides_slider extends WP_Widget
{
const name = 'Friendly ResponsiveSlides Slider';
const locale = THEMENAME;
const slug = 'friendly_responsiveslides_slider';
/**
* The widget constructor. Specifies the classname and description, instantiates
* the widget, loads localization files, and includes necessary scripts and
* styles.
*
* @package Earth
* @author iamfriendly
* @version 1.0
* @since 1.0
*/
function friendly_responsiveslides_slider()
{
$widget_opts = array (
'classname' => 'friendly_responsiveslides_slider',
'description' => __('Using VilJamis\'s responsive slides slider - images only, responsive slider', self::locale)
);
$control_opts = array(
'width' => 200,
'height' => 400
);
$this->WP_Widget( self::slug, __(self::name, self::locale), $widget_opts, $control_opts );
$this->register_scripts_and_styles();
}/* function friendly_page_post_content_as_row() */
/**
* Outputs the content of the widget.
*
* @package Earth
* @author iamfriendly
* @version 1.0
* @since 1.0
*/
function widget( $args, $instance )
{
//Extract the widget arguments
extract( $args, EXTR_SKIP );
//Begin the widget output
echo $before_widget;
//Retrieve each of the options from the widget
$post_cat = $instance['post_cat'];
$num_to_show = $instance['num_to_show'];
$position = $instance['position'];
$container_id = $instance['container_id'];
$slideshow_interval = $instance['slideshow_interval'];
$bullets = $instance['bullets'];
$arrows = $instance['arrows'];
$position_class = ( $position == 1 ) ? " behind_menu " : " below_menu ";
$post_args = array( 'posts_per_page'=>$num_to_show, 'cat' => $post_cat );
global $style_dir;
query_posts( $post_args );
if( have_posts() ) : ?>
<div id="<?php echo $container_id; ?>" class="<?php echo $position_class; ?> widget andyisalemon">
<ul class="rslides">
<?php while( have_posts() ) : the_post(); global $style_dir; ?>
<li>
<?php if( has_post_thumbnail() ) : ?>
<?php
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src( $image_id, 'slider-fullwidth-tall' );
$image_url = $image_url[0];
$title = get_the_title();
$slide_link = get_post_meta( get_the_ID(), "slide_link", true );
?>
<?php if( $slide_link && $slide_link != "" ) : ?><a href="<?php echo $slide_link; ?>" title=""><?php endif; ?>
<img src="<?php echo $image_url; ?>" alt="<?php echo $title; ?>" title="<?php echo $title; ?>" />
<?php if( $slide_link && $slide_link != "" ) : ?></a><?php endif; ?>
<?php endif; ?>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
</div>
<?php endif;
//End the widget output
echo $after_widget;
add_action( 'friendly_load_responsiveslides_slider_args', $this->friendly_responsiveslides_slider_helper( $container_id, $slideshow_interval, $bullets, $arrows ) );
}/* widget() */
/**
* Processes the widget's options to be saved.
*
* @package Earth
* @author iamfriendly
* @version 1.0
* @since 1.0
*/
function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['post_cat'] = $new_instance['post_cat'];
$instance['num_to_show'] = $new_instance['num_to_show'];
$instance['position'] = $new_instance['position'];
$instance['container_id'] = $new_instance['container_id'];
$instance['slideshow_interval'] = $new_instance['slideshow_interval'];
$instance['bullets'] = $new_instance['bullets'];
$instance['arrows'] = $new_instance['arrows'];
return $instance;
}/* update() */
/**
* Generates the administration form for the widget.
*
* @package PostDesk
* @author iamfriendly
* @version 1.0
* @since 1.0
*/
function form( $instance )
{
//Default Values
$instance = wp_parse_args(
(array)$instance,
array(
'post_cat' => '1',
'num_to_show' => '3',
'slideshow_interval' => '6000',
'position' => '1'
)
);
// Display the admin form
?>
<p>
<label for="<?php echo $this->get_field_id('post_cat'); ?>"><?php _e("Show This Category", THEMENAME); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('post_cat'); ?>" name="<?php echo $this->get_field_name('post_cat'); ?>">
<option value="default"<?php if( "default" == $instance['post_cat'] ) echo 'selected="selected"'; ?>><?php _e("Select Category", THEMENAME); ?></option>
<?php $all_categories = get_categories('hide_empty=0'); foreach ($all_categories as $category) : ?>
<option value="<?php echo $category->term_id; ?>" <?php if( $category->term_id == $instance['post_cat'] ) echo 'selected="selected"'; ?>><?php echo $category->cat_name; ?></option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('num_to_show'); ?>"><?php _e("How many slides?", THEMENAME); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('num_to_show'); ?>" name="<?php echo $this->get_field_name('num_to_show'); ?>">
<option value="1" <?php if( "1" == $instance['num_to_show'] ) echo 'selected="selected"'; ?>>1</option>
<option value="2" <?php if( "2" == $instance['num_to_show'] ) echo 'selected="selected"'; ?>>2</option>
<option value="3" <?php if( "3" == $instance['num_to_show'] ) echo 'selected="selected"'; ?>>3</option>
<option value="4" <?php if( "4" == $instance['num_to_show'] ) echo 'selected="selected"'; ?>>4</option>
<option value="5" <?php if( "5" == $instance['num_to_show'] ) echo 'selected="selected"'; ?>>5</option>
<option value="6" <?php if( "6" == $instance['num_to_show'] ) echo 'selected="selected"'; ?>>6</option>
<option value="7" <?php if( "7" == $instance['num_to_show'] ) echo 'selected="selected"'; ?>>7</option>
<option value="8" <?php if( "8" == $instance['num_to_show'] ) echo 'selected="selected"'; ?>>8</option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('position'); ?>"><?php _e("Position", THEMENAME); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id('position'); ?>" name="<?php echo $this->get_field_name('position'); ?>">
<option value="1" <?php if( "1" == $instance['position'] ) echo 'selected="selected"'; ?>>Behind Menu</option>
<option value="2" <?php if( "2" == $instance['position'] ) echo 'selected="selected"'; ?>>Below Menu</option>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'slideshow_interval' ); ?>"><?php _e( "Slideshow Interval (in ms)", THEMENAME ); ?>:</label>
<input type="text" id="<?php echo $this->get_field_id( 'slideshow_interval' ); ?>" name="<?php echo $this->get_field_name( 'slideshow_interval' ); ?>" value="<?php echo $instance['slideshow_interval']; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'bullets' ); ?>"><?php _e( "Show Bullets?", THEMENAME ); ?>:</label>
<input type="checkbox" id="<?php echo $this->get_field_id( 'bullets' ); ?>" name="<?php echo $this->get_field_name( 'bullets' ); ?>" <?php checked( $instance['bullets'], 1 ); ?> value="1" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'arrows' ); ?>"><?php _e( "Show Arrows?", THEMENAME ); ?>:</label>
<input type="checkbox" id="<?php echo $this->get_field_id( 'arrows' ); ?>" name="<?php echo $this->get_field_name( 'arrows' ); ?>" <?php checked( $instance['arrows'], 1 ); ?> value="1" />
</p>
<input type="text" id="<?php echo $this->get_field_id( 'container_id' ); ?>" name="<?php echo $this->get_field_name( 'container_id' ); ?>" value="<?php echo $this->get_field_id( 'container_id' ); ?>" />
<?php
}/* form() */
/**
* Load front-end and back-end scripts and styles
*
* @package Earth
* @author iamfriendly
* @version 1.0
* @since 1.0
*/
private function register_scripts_and_styles()
{
global $style_dir;
if( is_admin() )
{
}
else
{
$this->load_file( 'friendly_responsiveslides', '/themes/'.THEMENAME.'/theme_assets/js/responsiveslides.min.js', true );
}
}/* register_scripts_and_styles() */
/**
* Helper function for registering and enqueueing scripts and styles.
*
* @name The ID to register with WordPress
* @file_path The path to the actual file
* @is_script Optional argument for if the incoming file_path is a JavaScript source file.
* @package Earth
* @author iamfriendly
* @version 1.0
* @since 1.0
*/
function load_file( $name, $file_path, $is_script = false )
{
$url = content_url( $file_path, __FILE__ );
$file = $file_path;
if( $is_script )
{
wp_register_script( $name, $url, '' , '', true );
wp_enqueue_script( $name );
}
else
{
wp_register_style( $name, $url, '', '', false );
wp_enqueue_style( $name );
}
}/* load_file() */
/**
* Helper Function for the Earth Slider to output the relevant ja
*
* @package Earth
* @author iamfriendly
* @version 1.0
* @since 1.0
*/
function friendly_responsiveslides_slider_helper( $container_id = NULL, $slideshow_interval = 5000, $bullets = 'false', $arrows = 'false')
{
//$container_id is something like "widget-friendly_responsiveslides_slider-3-container_id", need to strip it
$strip_id = explode( "widget-", $container_id );
$strip_id = explode( "-container_id", $strip_id[1] );
if( $autoplay === false ){ $autoplay_res = "false"; }else{ $autoplay_res = "true"; }
if( $bullets == 1 ){ $pager = "true"; }else{ $pager = "false"; }
if( $arrows == 1 ){ $nav = "true"; }else{ $nav = "false"; }
$slideshow_interval = ( $slideshow_interval != "" ) ? $slideshow_interval : "6000";
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
jQuery('.rslides').responsiveSlides({
timeout : <?php echo $slideshow_interval; ?>,
pager: <?php echo $pager; ?>,
nav: <?php echo $nav; ?>,
speed: 700
});
});
</script>
<?php
}/* friendly_responsiveslides_slider_helper() */
}/* class friendly_responsiveslides_slider */
}
add_action( 'widgets_init', create_function( '', 'register_widget("friendly_responsiveslides_slider");' ) );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment