Skip to content

Instantly share code, notes, and snippets.

@anneallen
Created May 4, 2014 00:14
Show Gist options
  • Save anneallen/b2d77f8661070cc8fe86 to your computer and use it in GitHub Desktop.
Save anneallen/b2d77f8661070cc8fe86 to your computer and use it in GitHub Desktop.
Clicky Box Widget
//Clicky Boxes
/**
*This creates a widget area for text, and the whole box is clickable
*
*/
/** Register Widget **/
function lob_boxes_load_widgets() {
register_widget( 'Lob_Boxes_Widget' );
}
add_action( 'widgets_init', 'lob_boxes_load_widgets' );
/** Define the Widget as an extension of WP_Widget **/
class Lob_Boxes_Widget extends WP_Widget {
function Lob_Boxes_Widget() {
/* Widget settings. */
$widget_ops = array(
'classname' => 'widget_lob_boxes',
'description' => 'Used for Homepage and Sidebar Land of Brand Boxes'
);
/* Widget control settings. */
$control_ops = array( 'id_base' => 'lob-boxes-widget' );
/* Create the widget. */
$this->WP_Widget( 'lob-boxes-widget', 'LOB Widget', $widget_ops, $control_ops );
}
function widget( $args, $instance ) {
extract( $args );
/**these are the widget options*/
$title = apply_filters('widget_title', $instance['title']);
$text = $instance['text'];
$textarea = $instance['textarea'];
$link_text = $instance['link_text'];
$lobwidget_class = $instance['lobwidget_class'];
echo $before_widget;
// Display the widget
//Check if class is set
echo '<div class="click" onclick="location.href=\'' .$text. '\';" style="cursor:pointer;">';
// Check if title is set
if ( $title ) {
echo $before_title . $title . $after_title;
}
// Check if textarea is set
if( $textarea ) {
echo wpautop($textarea);
}
// Check if text is set
if( $text ) {
echo '<a class="lob" href="'.$text.'">'.$link_text.'</a>';
}
echo '</div>';
echo $after_widget;
}
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
// Fields
$instance['title'] = strip_tags($new_instance['title'],'<br />');
if ( current_user_can('unfiltered_html') )
$instance['textarea'] = $new_instance['textarea'];
else
$instance['textarea'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['textarea']) ) );
$instance['text'] = strip_tags($new_instance['text']);
$instance['link_text'] = strip_tags($new_instance['link_text']);
$instance['lobwidget_class'] = strip_tags($new_instance['lobwidget_class']);
return $instance;
}
function form( $instance ) {
// Check values
if( $instance) {
$title = esc_attr($instance['title']);
$text = esc_attr($instance['text']);
$textarea = esc_textarea($instance['textarea']);
$link_text = esc_textarea($instance['link_text']);
$lobwidget_class = esc_textarea($instance['lobwidget_class']);
}
else {
$title = '';
$text = '';
$textarea = '';
$link_text='';
$lobwidget_class='';
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wp_widget_plugin'); ?></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('textarea'); ?>"><?php _e('Content:', 'wp_widget_plugin'); ?></label>
<textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('textarea'); ?>" name="<?php echo $this->get_field_name('textarea'); ?>"><?php echo $textarea; ?></textarea>
</p>
<p>
<label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('Link Address:', 'wp_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>" type="text" value="<?php echo $text; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('link_text'); ?>"><?php _e('Link Text:', 'wp_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('link_text'); ?>" name="<?php echo $this->get_field_name('link_text'); ?>" type="text" value="<?php echo $link_text; ?>" />
</p>
<label for="<?php echo $this->get_field_id('text'); ?>"><?php _e('Widget Class:', 'wp_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('lobwidget_class'); ?>" name="<?php echo $this->get_field_name('lobwidget_class'); ?>" type="text" value="<?php echo $lobwidget_class; ?>" />
</p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment