0

When I insert multiple instances of widget in a particular sidebar, they all appear by default collapsed and with the same title. E.g.:

Multiple Widgets with the same title

In this case, the user can upload a different pic to each widget, and reorder them as they want. It's not very user friendly having to click to open each one to see if its the one that they want to remove/edit/change of place.

With the form() method of the widget we can change whatever is shown once opened, but the "<div class="widget-title">" and its corresponding "<h4>" are out of bounds.

Is there any filter or action that allows to edit what happens there and insert some instance specific information? E.g.: "Graphic Ad: Foobar".

1

I believe if you add 'title' field to the form() (and update()) method this will be displayed in the admin interface. Traditionally this would also be used in the widget() method, but is not required.

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title']);
        return $instance;
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
        $title = strip_tags($instance['title']);
        ?><p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p><?php
    }
  • Thanks! As far as I can see it's not enough to have an $instance['title'] field, right? You need to actually have an actual html input field with the widget title as id attribute, isn't it? – yivi Dec 17 '14 at 16:09
  • Correct. But you could dynamically populate a hidden field. – karpstrucking Dec 17 '14 at 16:10
  • Yes, that was my thinking. Still, weird way of working. :P Many thanks! – yivi Dec 17 '14 at 16:10

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.