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 recently been trying to create a 'Popular Products' widget for for WP e-Commerce. Since there isn't built-in functionality, I referred to Dave Mackintosh's post here and gave it a shot.

Still no success. But since there's a shortcode, I decided to use the 'Shortcodes in Widgets' plugin and place the shortcode in a text widget. It works now, but the issue now is the product list appears outside the widget area, just floating in the sidebar.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

This behavior is being caused most likely because something in the shortcode function that is causing unstable behavior. I recently just had the same issue with something I was working on.

From the WordPress Codex for "Add ShortCode" there's a note tucked down near the bottom of the page:

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.

From the example hack that you referenced there are echo calls within the shortcode function. Basically what's happening is that those echo commands are being called before the Widget header script.

The way to fix this is to pass all echo calls in the function to a variable and then add a return statement at the very end of the function.

return $html;

share|improve this answer
    
Wonderful! Thanks Brent! One line was all it took, overlooked it I guess. It works now. –  nouptime Aug 28 '12 at 1:42
add comment

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.