Putting the title after the WordPress Text Widget

A testimonial on massageweymouth.co.uk
I recently had a project where I needed to include some testimonials into a WordPress theme. The client would also need to add in additional testimonials at a later date.
I immediately thought about using the Text Widget available by default in WordPress and using the title for the credit and the body text for the testimonial.
However, it turns out that “simply” swapping the title to show underneath the widget body copy isn’t that simple!
The way around it is to create a second widget (based on the Text Widget) with the required modifications, like so…
<?php
// Title AFTER Content in Text Widget
class WP_Widget_Text2 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('text2', __('Text2'), $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', $instance['text'], $instance );
echo $before_widget;
?>
<div class="textwidget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>
<?php
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; }
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']) ) ); // wp_filter_post_kses() expects slashed
$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); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
}
}
register_widget('WP_Widget_Text2');
?>
Paste that in to your functions.php file and when you next visit ‘Widgets’ in the backend you’ll have a Text2 Widget you can use for testimonials (or any other instance where you need the title to come after the main text)

The result of the Text Widget
Powered by Facebook Comments