Skip to content

Instantly share code, notes, and snippets.

@ovizii
Last active January 7, 2018 22:50
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 ovizii/3891aff7c1db38b96349 to your computer and use it in GitHub Desktop.
Save ovizii/3891aff7c1db38b96349 to your computer and use it in GitHub Desktop.
Create discreet widget - only shows if not empty
//add discreet text widget
add_action('widgets_init', 'custom_discreet_text_widget');
function custom_discreet_text_widget() {
class DiscreetTextWidget extends WP_Widget_Text {
function DiscreetTextWidget() {
$widget_ops = array('classname' => 'discreet_text_widget', 'description' => __('Arbitrary text or HTML, only shown if not empty'));
$control_ops = array('width' => 400, 'height' => 350);
$this->WP_Widget('discrete_text', __('Discreet Text'), $widget_ops, $control_ops);
}
function widget( $args, $instance ) {
extract($args, EXTR_SKIP);
$text = apply_filters( 'widget_text', $instance['text'] );
if (empty($text)) return;
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
echo $before_widget;
if (!empty($title)) { echo $before_title.$title.$after_title;} ?>
<div class="textwidget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>
<?php
echo $after_widget;
}
}
return register_widget("DiscreetTextWidget");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment