A question came across the Genesis WordPress Facebook group recently:

Simple Social Icons – how can I disable the CSS that is added to ``? The code calls add_action() with $this->css. The widget class is invoked with register_widget() so I don’t think that there is a global variable created that I can pass to remove_action(). Any thoughts?

It’s an interesting question because, beyond this particular case, it’s pretty common to run into situations where a WordPress action is registered within a class and you need to remove it.

The problem is that to remove an action in WordPress you need to be able to reference the exact callback function (in this case $this->css) that was added. It’s not always obvious where to find the original, and even if you can find it, getting access to it outside the class that put it there can be anywhere between tricky and downright impossible.

In this case, however there is a global variable that can give us access to the original widget class. When a widget gets registered and created, it is stored in the WordPress $wp_registered_widgets global variable. The variable is an array where each element has a key for the unique widget (usually the widget name with a serial number appended). The element itself is an array holding other meta data concerning the widget and the widget object itself.

So by checking the keys for the widget name, we can find all the widgets of the “Simple Social Icons” variety. Then for each one, we can grab the widget object and run remove_action() on that original object.

Here’s a quick snippet that does the trick for this particular widget: