Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

http://pastebin.com/3uWpeaFx

(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!

share|improve this question
    
Do you not have to define the widget class as parent:: for multi widget use? Not sure but have a look stackoverflow.com/questions/5247302/… –  Wyck May 22 '11 at 2:02

1 Answer 1

up vote 1 down vote accepted

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. :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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