Custom random quote widget breaks when used in multiple sidebars

I’ve created a custom widget and placed it in functions.php. You can see the widget here:

http://pastebin.com/3uWpeaFx

Read More

(I’ve also created a similar widget that grabs a random image; it’s included with the pastebin.)

The widget allows users to plug in up to ten blocks of text/HTML, stores them in an array in JS, and displays one at random using JS.

These are super simple widgets that are based on the default text widget that ships with WP. (I’m no PHP expert, so there’s probably a more elegant way to create them, but they’re simple to use, which is what the client needed.)

The problem I’m having is this:

Using more than one Random Text widget, even on multiple different sidebars displayed on different pages, causes all but the original to return ‘undefined’.

The text that you put in the box seems to store properly, and is editable through the WP admin interface; but when it comes to displaying it on the front-end, it doesn’t echo the text properly.

I’m sure it’s something simple that I’m missing in the widget…any clues?

Thanks!

Related posts

Leave a Reply

1 comment

  1. No parent necessary: the difference between a not-working widget and a working widget was this:

    $text1 = apply_filters( 'widget_text1', $instance['text1'], $instance );
    

    vs:

    $text1 = apply_filters( 'widget_text1', empty($instance['text1']) ? '' : $instance['text1'], $instance, $this->id_base);
    

    I realized putting multiple Random Text widgets on the same page, their titles worked, but their text content did not. So I copied the properties (empty($instance…etc.) from the title to each of the text instances.

    The widgets themselves were instantiated, but the text areas weren’t. At least, that’s what I think was happening. I don’t know enough PHP to know for sure.

    If anybody has insight as to why the second version works and the first doesn’t, I’d be happy to hear it. 🙂