Recently, while working on the next upcoming release of Easy Digital Downloads, we were challenged with a unique request. The goal, was to hide our Cart Widget when the cart was empty, but show it when the cart had items added to it, then re-hide the widget if it were emptied again. All well and good, but it had to be something that was on a per-instance basis, and happen without a page refresh. Meaning, you could have multiple cart widgets on a page, and they could behave independently.

Easy Peasy….ish

If you’ve ever dealt with Widgets in WordPress, then you know as well as I do, no problem, we’ll just do some logic within the `widget` method of the PHP Class we built, and determine if the cart is empty, add a few CSS classes to a new HTML element that wraps the content, and we’re good.

Or so I thought…

It was quickly pointed out that the `widget` method doesn’t actually contain the ‘wrapper’ element that WordPress renders your widget within, just the internal content. Consequently, by hiding the content in the widget, we left the wrapper elements visible, and those are what Theme Developers use sometimes, causing a problem. So, back to the drawing board.

Digging a little deeper

The original request mentioned a filter named dynamic_sidebar_params, and this filter was a great idea, except one thing, I couldn’t figure out how to access the specific widget instance’s settings, to know if we should hide this instance when empty (and do so by adding CSS classes to the wrapper).

Or so I thought….again…

The solution

It turns out, there are a few methods of the base `WP_Widget` class that you can use to get some instance settings, that I wasn’t aware of. Below is the basic outline of a widget class that includes a title option as well as an option to add a CSS Class to the widget wrapper, using the settings:

Breaking it all down

Most of this will look familiar if you’ve written a widget before, but the key is the inclusion of the widget_class method, and the filter hooking into dynamic_sidebar_params. What this does, is lets us access part of the Widget that wraps it within the theme, before the output of the widget contents.

This filter will run on each widget loaded on the page, so we first need to make sure that we’re looking at an instance of our own widget:
if ( strpos( $params[0]['widget_id'], 'ck_example_widget' ) !== false ) {

After that we get all the settings related to this specific instance of the Widget (since you can have multiple instances of the same widget in some cases).
$instance_settings = $all_settings[ $instance_id ];

Once there, we see if the option to add the class was checked:
if ( ! empty( $instance_settings['add_wrapper_class'] ) ) {

And if we’ve checked it, perform a regular expression search to add in our new class we want to add to the wrapper.

Hope this helps out!

Posted by Chris Klosowski

Chris Klosowski is the Director of Technology at Sandhills Development, LLC, creators of fine WordPress plugins, and ecommerce solutions.


  1. I am glad to be one of several visitors on this outstanding web
    site (:, thanks for putting up.


  2. Udzieⅼone staѡki poᴡodują 10% rabatek
    także dot.


  3. Hey there! Iam aware this kind of is kind of off-topic, but
    I’d figuresd I had created ask. Would you become interested in exchanging back links or maybe guest writing a blog post or perhaps vice-versa?
    My blog will go over a lot associated with the same topics since yours, and i also believe many of us could greatly take advantage of every single other.

    If you take place to be interested, twenty-four hours a day shoot me an e mail.
    I look forward to be able tto hearing from you!
    Wonderful blog by the method!


  4. Hello to every one, as I am truly eager of reading
    this website’s post to be updated on a regular basis.
    It contains good data.


  5. UnqᥙestiobaЬly believe that which you stated.
    Υour favorite reaѕon seemed to bе oon the intrnet the simplest thing to Ƅe aware of.
    I say to you, I definitely geet iгked while people consider wоrries that
    they jujst do not know abⲟut. Yoս managed to hit tһe
    nail upon the top and also defined out the whole thiong without
    having side-effects , people ccan take a signal. Wilⅼ probably be back to geet
    more. Thanks


  6. Asking questions are genuinely pleasant thing if you are not understanding something entirely, except this paragraph gives nice understanding


  7. Hi there, always i used to check blog posts here early in the dawn, for the reason that i like to gain knowledge of more and more.


  8. This site truly has all of the info I wanted concerning this subject and
    didn’t know who to ask.

    Feel free to surf to my webpage;


  9. This info is w᧐rth everyone’s attention. How can I find
    out more?


  10. Hi, There’s no doubt that your site could possibly be having web browser compatibility issues.
    When I look at your web site in Safari, it looks fine but
    when opening in I.E., it’s got some overlapping issues.

    I simply wanted to provide you with a quick heads up!
    Other than that, fantastic site!


  11. Thanks , I’ve just been searching for info about this subject for a long time and yours is the greatest
    I have found out so far. But, what concerning the bottom
    line? Are you positive about the supply?

    Also visit my website … gema mega888 (Laurene)


  12. Piece of writing writing is also a excitement, if you be acquainted with afterward you can write
    or else it is complex to write.

    My blog – Keto Premium Shot Pills


  13. Appreciating the hard work you put into your blog and in depth information you offer.
    It’s nice to come across a blog every once in a while that isn’t
    the same out of date rehashed information. Great read!
    I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.


  14. I loved as much as you will receive carried out right here.
    The sketch is tasteful, your authored material stylish.

    nonetheless, you command get bought an shakiness over that
    you wish be delivering the following. unwell unquestionably
    come more formerly again as exactly the same nearly very often inside case you shield this increase.


  15. Hi there! This blog post could not be written any better!
    Looking at this post reminds me of my previous roommate!
    He always kept talking about this. I’ll forward this article
    to him. Fairly certain he will have a very good read.

    Many thanks for sharing!


Leave a reply

Your email address will not be published. Required fields are marked *