Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Extending the text widget. Always a paragraph. WordPress

  • SOLVED

Hello,

I guess this time it's only a minor modification.

Below is a minor modification shown to the default WP text widget.

<?php
/**
* Text widget class.
*/
class WP_Text_Widget extends WP_Widget {
function __construct() {
$widget_ops = array(
'classname' => 'widget_text',
'description' => __('Arbitrary text or HTML')
);
$control_ops = array(
'width' => 400,
'height' => 350
);
parent::__construct('text', __('Text'), $widget_ops, $control_ops);
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
$text = apply_filters('widget_text', empty($instance['text']) ? '' : $instance['text'], $instance);
echo $before_widget;
if (!empty($title)) {
echo $before_title . $title . $after_title;
}
echo !empty($instance['filter']) ? wpautop($text) : $text;
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
if (current_user_can('unfiltered_html'))
$instance['text'] = $new_instance['text'];
else
$instance['text'] = stripslashes(wp_filter_post_kses(addslashes($new_instance['text'])));
$instance['filter'] = isset($new_instance['filter']);
return $instance;
}
function form($instance) {
$instance = wp_parse_args((array) $instance, array(
'title' => '',
'text' => ''
));
$title = strip_tags($instance['title']);
$text = esc_textarea($instance['text']);
?>
<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>
<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>
<input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />
&nbsp;
<label for="<?php echo $this->get_field_id('filter'); ?>">
<?php _e('Automatically add paragraphs'); ?>
</label>
</p>
<?php
}
}
add_action('widgets_init', create_function('', 'return register_widget("WP_Text_Widget");'));


If possible, I'd like to have it always shown as a paragraph. Meaning, removing the WP default check box, and always including <p></p>

Could you make this possible perhaps?

Answers (2)

2011-12-07

Mike Van Winkle answers:

Try this:
http://pastebin.com/yZ74ZsVa


cor comments:

Hello Mike,

It works like a charm. Just voted (:

2011-12-07

Francisco Javier Carazo Gil answers:

I know it is not the best way, but you can do with jQuery directly in the DOM. Something like this:

var cnt = $(".textwidget").contents()
$(".textwidget").replaceWith(cnt);


cor comments:

Hi Francisco,

Thank you for your reply. Though I have to admit, I've just voted...

As *your search engine* almost doesn't understand JavaScript I try to avoid jQuery in cases like this.

Still, more than appreciating your reply!