1

I am trying to alter the output of the default WordPress text widget. Since the widget is written in OOP I hoped it could be a simple deal of extending the class and overwriting one method – but it turns out to be not that easy.

The difficult thing is that I don't want to create a new widget that essentially is just a copy of the text widget with a new name – I really want to alter the ouput of the current text widget.

I've written a small plugin for this, containing two files: the main plugin file and the actual class. In the main plugin file I simply do:

add_action('widgets_init', 'include_my_class');
function include_my_class() {
    include("custom_class.php");
}

If I don't do it this way, I get a fatal error that the class I am trying to extend does not exist. Still, I am of course not sure if it's the right way to do it.

My custom class looks like this:

class CustomTextWidget extends WP_Widget
{

    public function __construct() {
        parent::__construct();
    }

    public function widget( $args, $instance ) {

        /** This filter is documented in wp-includes/default-widgets.php */
        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

        /**
         * Filter the content of the Text widget.
         *
         * @since 2.3.0
         *
         * @param string    $widget_text The widget content.
         * @param WP_Widget $instance    WP_Widget instance.
         */
        $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
        echo $args['before_widget'];
        if ( ! empty( $title ) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        } ?>
            // lots and lots of custom markup here…
            <div class="my-custom-textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
        <?php
        echo $args['after_widget'];
    }
}

Unfortunately, my class does nothing, the core WordPress widget is still used. But I can't be completely wrong either since there is no error, but where am I going wrong? Is there a way around to avoid introducing a new widget?

1

Think about it, why would WordPress do anything just because you have defined additional class?

It's not magic (neither magical magic or code driven magic). The text widget doesn't appear simply because class exists. When WordPress loads it runs wp_widgets_init(), which executes register_widget('WP_Widget_Text').

So technically you cannot tell it to use different class. What you can do is unregister the native class and register your own. As long as your class outputs same name and settings it will be practically indistinguishable from native widget.

1
  • 1
    Ok sure, that makes sense. I got it working now, thanks for your answer! – Sven Jan 3 '15 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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