Skip to content

Instantly share code, notes, and snippets.

@johnymachine
Created August 1, 2017 19:49
Show Gist options
  • Select an option

  • Save johnymachine/418b4e04d3950c9204bafafa18b88bb3 to your computer and use it in GitHub Desktop.

Select an option

Save johnymachine/418b4e04d3950c9204bafafa18b88bb3 to your computer and use it in GitHub Desktop.
WP - Bootstrap alert
<?php
/**
* Custom Alert Text Widget
*
* @package Barletta
*/
class barletta_alert_text extends WP_Widget {
public function __construct(){
$widget_ops = array('classname' => 'barletta-alert-text','description' => esc_html__( "Barletta Alert Text Widget", 'barletta') );
parent::__construct('barletta_alert_text', esc_html__('Barletta Alert Text Widget','barletta'), $widget_ops);
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$widget_text = ! empty( $instance['text'] ) ? $instance['text'] : '';
$alert_type = ! empty( $instance['type'] ) ? 'alert-'.$instance['type'] : '';
$text = apply_filters( 'widget_text', $widget_text, $instance, $this );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<div class="alertwidget">
<?php echo '<div class="alert '.$alert_type.'">'; ?>
<?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>
</div>
</div>
<?php
echo $args['after_widget'];
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['type'] = sanitize_text_field( $new_instance['type'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
} else {
$instance['text'] = wp_kses_post( $new_instance['text'] );
}
$instance['filter'] = ! empty( $new_instance['filter'] );
return $instance;
}
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '', 'type' => '' ) );
$filter = isset( $instance['filter'] ) ? $instance['filter'] : 0;
$title = sanitize_text_field( $instance['title'] );
$text = esc_textarea( $instance['text'] );
$type = sanitize_text_field( $instance['type'] );
?>
<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( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
<textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea></p>
<p><label for="<?php echo $this->get_field_id('type'); ?>"><?php _e('Alert type:'); ?></label>
<select class='widefat' id="<?php echo $this->get_field_id('type'); ?>" name="<?php echo $this->get_field_name('type'); ?>">
<option value='success'<?php echo ($type=='success')?'selected':''; ?>><?php _e('Success (Green)'); ?></option>
<option value='info'<?php echo ($type=='info')?'selected':''; ?>><?php _e('Info (Blue)'); ?></option>
<option value='warning'<?php echo ($type=='warning')?'selected':''; ?>><?php _e('Warning (Yellow)'); ?></option>
<option value='danger'<?php echo ($type=='danger')?'selected':''; ?>><?php _e('Danger (Red)'); ?></option>
</select>
</p>
<p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox"<?php checked( $filter ); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment