3

When you add a text widget to a sidebar in Wordpress the widget includes this code

<div class="textwidget">

How do I remove this without editing the core?

FYI - I think this line is located here: wp-includes/default-widgets.php (line 383)

7
  • Why would you want to do that? The widgets can have specific ID's.
    – Wyck
    Commented Jun 12, 2012 at 16:31
  • I use a jquery box with some cool effects together with that text app, so the div messes up the css a bit. If there was an easy way to remove it it would save me alot of time :)
    – Demilio
    Commented Jun 12, 2012 at 17:14
  • 1
    Well the easiest way to remove it would be to use jquery's .remove.
    – Wyck
    Commented Jun 12, 2012 at 17:34
  • And could you explain how? :)
    – Demilio
    Commented Jun 12, 2012 at 22:28
  • Better using jQuery to remove a div after the fact that re-making a clean widget with the exact functionality you need?
    – MZAweb
    Commented Jun 13, 2012 at 1:21

5 Answers 5

9

OK, attempting to remove the with jQuery is counter intuitive.

Modifying it via output buffering is inefficient.

And we shouldn't edit the actual widget code itself, since this will revert back next time we update WordPress.

I would suggest either creating your own text widget, or simply extend and modify the existing WordPress widget as follows (put the following code in your functions.php file):

add_action( 'widgets_init', 'register_my_widgets' );


function register_my_widgets() {
    ...
    register_widget( 'My_Text_Widget' );
}

class My_Text_Widget extends WP_Widget_Text {
    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; } ?>
            <?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>
        <?php
        echo $after_widget;
    }
}
0

If you're looking to remove the actual div with the textwidget class applied to it but not it's content, this is a simple method using jQuery:

$('.textwidget').replaceWith($('.textwidget').contents());
2
  • 1
    This is absolutely nothing one should do in the frontend…
    – feeela
    Commented Jun 22, 2015 at 9:59
  • @feela could you please elaborate on your comment and better explain what is the issue with the answer?
    – Rarst
    Commented Jun 22, 2015 at 20:13
-1

As per the comment, in the case it is probably better to just remove or replace the class name using jQuery then a php function, since that class is hardcoded anyhow.

To remove it.

jQuery('.textwidget').remove();

To set the css to hide it.

jQuery('.textwidget').css('display', 'none');

To change the class name

jQuery('.textwidget').attr('class', 'newName');
1
  • This will probably remove the class right? But the problem here is the div
    – Demilio
    Commented Jun 16, 2012 at 22:38
-3

You can use output buffering (http://php.net/manual/es/book.outcontrol.php) to get the whole generated page in a variable and remove the offending before echoing.

But it seems overly complicated for this case. You can just copy all the widget code and create a new widget (in a plugin or your functions.php) without that hardcoded div. It's really not that much code.

Also, any reason to remove that div that can not be solved in css?

1
  • Why the downvotes? any better solution?
    – MZAweb
    Commented Jun 13, 2012 at 1:20
-3
  1. Edit file: /wp-includes/widgets/class-wp-widget-text.php
  2. Find, Delete: <div class="textwidget"> and </div>
  3. Done!
2
  • 4. Watch your changes being overwritten after the very next WordPress update. 5. Understand why you should not change core files.
    – fuxia
    Commented Jan 15, 2017 at 16:26
  • You should never modify WordPress core files if you can avoid it.
    – Jay
    Commented Oct 22, 2017 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.