WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 2 years ago

Last modified 2 years ago

#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)

38614.diff (1.5 KB) - added by soulseekah 2 years ago.
Initial patch with new filters

Download all attachments as: .zip

Change History (9)

#1 @westonruter
3 years ago

  • Keywords close added

You will probably have to create a custom widget extending WP_Widget_Text which then overrides the widget method to output the markup you want.

#2 @henry.wright
3 years ago

@westonruter or do some crazy JavaScript string replacement. But I did open the ticket so these workarounds can be avoided.

@soulseekah
2 years ago

Initial patch with new filters

#3 @soulseekah
2 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>';
} );
Last edited 2 years ago by soulseekah (previous) (diff)

#4 @SergeyBiryukov
2 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 );
Last edited 2 years ago by westonruter (previous) (diff)

#5 follow-up: @westonruter
2 years ago

Instead of 'text-2' !== $widget->id it would probably be better to check 'text' !== $widget->id_base.

#6 @henry.wright
2 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 @westonruter
2 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

#8 in reply to: ↑ 5 @SergeyBiryukov
2 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.

Note: See TracTickets for help on using tickets.