#38614 closed enhancement (wontfix)
Text widget markup is not removable
Reported by: | henry.wright | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.6.1 |
Component: | Widgets | Keywords: | close |
Focuses: | Cc: |
Description
When using a text widget, a div with a "textwidget" class is used:
<div class="textwidget"><!-- Stuff --></div>
There's no easy way to remove or filter this mark up. In my case this div
isn't needed.
Attachments (1)
Change History (9)
#2
@
8 years ago
@westonruter or do some crazy JavaScript string replacement. But I did open the ticket so these workarounds can be avoided.
#3
@
8 years ago
Added a patch. Usage example:
<?php add_filter( 'widget_text_wrapper_before', function( $wrapper ) { return '<div class="custom stuff">['; } ); add_filter( 'widget_text_wrapper_after', function( $wrapper ) { return ']</div>'; } );
#4
@
8 years ago
This can be done using the existing widget_display_callback
filter:
<?php function wp38614_replace_text_widget_wrapper( $instance, $widget, $args ) { // Bail if not displaying the widget to be filtered if ( 'text-2' !== $widget->id ) { return $instance; } $title = apply_filters( 'widget_title', $instance['title'], $instance, $widget->id_base ); $text = apply_filters( 'widget_text', $instance['text'], $instance, $widget ); echo $args['before_widget']; if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; } echo '<div class="custom stuff">['; echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; echo ']</div>'; echo $args['after_widget']; return false; } add_filter( 'widget_display_callback', 'wp38614_replace_text_widget_wrapper', 10, 3 );
#5
follow-up:
↓ 8
@
8 years ago
Instead of 'text-2' !== $widget->id
it would probably be better to check 'text' !== $widget->id_base
.
#6
@
8 years ago
Nice find @SergeyBiryukov. I didn't know that filter existed. And thanks @westonruter for the heads up about the id_base property. I guess this ticket can be closed?
#7
@
8 years ago
- Milestone Awaiting Review deleted
- Resolution set to wontfix
- Status changed from new to closed
#8
in reply to:
↑ 5
@
8 years ago
Replying to westonruter:
Instead of
'text-2' !== $widget->id
it would probably be better to check'text' !== $widget->id_base
.
Good point, I was thinking of filtering one particular widget. For all text widgets, the latter should work.
You will probably have to create a custom widget extending
WP_Widget_Text
which then overrides thewidget
method to output the markup you want.