Ever run into a situation where your Widget isn’t saving? I did and it sucked.

I figured out what my problem was. NAMESPACING. Yes, super important folks. I wrote my own twitter widget since I didn’t like some of the plugins I found. Here is a quick glance at the code:

class XY_Twitter_Widget extends WP_Widget{  
function __construct(){  
$widget_ops = array("classname" => "xy_twitter_widget", "description" => __("Displays Latest Twitter Feeds"));

parent::__construct("twitter", __('Providence Twitter Widget'), $widget_ops);  
}

function widget($args, $instance){  
//.... layout stuff
}

Easy enough. PROBLEM. It wouldn’t save. I would enter my twitter handle and it would always be blank. Code checked out fine, everything rendered fine, id’s and names were in place.

I began to debug the code, in my case did a var_dump on the $instance variable. And noticed some arbitrary key values. They didn’t belong to my Widget object.

Yes, I was inheriting another twitter object, in my case from the wonderful Jetpack plugin. Bah! All I had to do was follow proper name spacing conventions. So I changed the namespace in the parent::construct method.

Here is the new code:

class XY_Twitter_Widget extends WP_Widget{  
function __construct(){  
...
//NAMESPACE - xy_twitter_wiget <-- DOIT
parent::__construct("xy_twitter_widget", __('Providence Twitter Widget'), $widget_ops);  
}

That made saving work.