Skip to content

Instantly share code, notes, and snippets.

@KustomDeveloper
Last active December 4, 2017 20:03
Show Gist options
  • Save KustomDeveloper/36c7b5867502fa414fbb4b215d2647b2 to your computer and use it in GitHub Desktop.
Save KustomDeveloper/36c7b5867502fa414fbb4b215d2647b2 to your computer and use it in GitHub Desktop.
Wordpress custom widget extending wp-widget class
/**
*
* Banner Widget
* Note* - The WP_Widget class is located in wp-includes/class-wp-widget.php.
*/
class Banner_Widget extends WP_Widget {
/**
* Register Widget With Wordpress
*/
public function __construct() {
parent::__construct(
'banner_widget', // Base ID
__('Banner Widget', 'text_domain'), // Widget name
array( 'description' => __( 'Greg Reid Banner Widget', 'text_domain' ), )
);
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
// if text contain a value, save it to $link
if ( isset( $instance[ 'link' ] ) ) {
$link = $instance[ 'link' ];
}
// if text contain a value, save it to $text
if ( isset( $instance[ 'text' ] ) ) {
$text = $instance[ 'text' ];
}
// if text contain a value, save it to $subtitle
if ( isset( $instance[ 'subtitle' ] ) ) {
$subtitle = $instance[ 'subtitle' ];
}
// if title contain a value, save it to $title
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
// else set the $title to string - Title
$title = __( 'Title', 'text_domain' );
}
?>
<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>
<p>
<label for="<?php echo $this->get_field_id( 'subtitle' ); ?>"><?php _e( 'Sub-Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'subtitle' ); ?>" name="<?php echo $this->get_field_name( 'subtitle' ); ?>" type="text" value="<?php echo esc_attr( $subtitle ); ?>">
</p>
<p>
<label for="<?php echo $this->get_field_id( 'text' ); ?>"><?php _e( 'Description:' ); ?></label>
<textarea rows="8" cols="40" class="widefat" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo esc_attr( $text ); ?>
</textarea>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'Link:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>">
</p>
<?php
}
// array $instance Stores values from database.
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . '<h1 class=">' . 'bw-banner-title">' . $title . '</h1>' . $args['after_title'];
echo '<p class=' . 'bw-subtitle>' . ' <b>' . $instance['subtitle'] . '</b></p>';
echo '<div class=' . 'bw-banner-textarea' . '>' . $instance['text'];
echo $args['after_widget'];
echo '<a class="' . 'bw-link" ' . 'href="' . $instance['link'] . '"' . '>' . '<button>INVITE GREG TO SPEAK</button>'. '</p>';
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['subtitle'] = ( ! empty( $new_instance['subtitle'] ) ) ? strip_tags( $new_instance['subtitle'] ) : '';
$instance['text'] = ( ! empty( $new_instance['text'] ) ) ? strip_tags( $new_instance['text'] ) : '';
$instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
return $instance;
}
} // Banner Widget Class
add_action( 'widgets_init', function(){
register_widget( 'Banner_Widget' );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment