4

I want to take the text widget and remove the preceding / trailing markup from the text output currently showing as

<div class="textwidget">Test</div>

The before_widget / after_widget that is assigned with each widget area is under my control but the widget content itself is beyond me.

// Area 4, located in the footer. Empty by default.
    register_sidebar( array(
        'name' => __( 'First Footer Widget Area', 'lakeshoreautoclinic' ),
        'id' => 'first-footer-widget-area',
        'description' => __( 'The first footer widget area', 'lakeshoreautoclinic' ),
        'before_widget' => ' <p>',
        'after_widget' => '</p>',
        'before_title' => '<strong>',
        'after_title' => '</strong>',
    ) );

which outputs this for the surrounding / widget

<p><strong>Test</strong><div class="textwidget">Test</div></p>
2
  • if there's an easier / shorter way to do this aside from creating a "new widget" > that would be ideal.
    – Devise
    Sep 26, 2011 at 1:46
  • Upvote since I'm trying to find this too. Mar 23, 2012 at 22:33

2 Answers 2

1

You have a couple options as I see it:

There's a the_widget() template tag that lets you control the before_widget and after_widget properties. However, it divorces the widget from the Widgets UI.

Alternately, you can try to accomplish whatever you're trying to do via CSS. If you change your before_widget line to this:

'before_widget' => '<p id="%1$s" class="widget %2$s">',

you'll get some classes and IDs that will make each widget style-able.

-2

Text widgets formating can be found in '/wp-includes/default-widgets.php' starting from line 368. In order to remove that formating you would have to change line 382.

<div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>

Just remove divs so you would have

<?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?>

However, that way you are changing wordpress core files, which means it might get changed in next updates, so better way to do it is copying the text widget class, renaming it (and the constructor), and adding it to your themes functions.php file (or a Plugin if you wish).

This is also a way to change position of widget name and so on...

1
  • 1
    Hacking core is just a universally bad idea so not really a solution.
    – mrwweb
    May 5, 2012 at 23:10

Your Answer

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

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