WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I took a code straight out of one of my themes I created, and it's a list of all 50 states in an unordered list packed into a widget you can just drag and drop on the sidebar.

The problem is, when I try using this code in a PLUGIN file, I get the following error:

Fatal error: Call to a member function register() on a non-object in C:\xampp\htdocs\wordpress\wp-includes\widgets.php on line 431

Why would it work in the theme, but not in the plugin? By the way, the active theme is NOT the theme I took the code out of.

Here's my code: http://pastebin.com/ZeRWW3yb


share|improve this question
up vote 14 down vote accepted

try replacing :



add_action('widgets_init', 'register_states_widget');
function register_states_widget() {
share|improve this answer
Perfect, thank you. – Jared Feb 24 '11 at 22:04
Glad i could help – Bainternet Feb 24 '11 at 22:21
One-liner: add_action('widgets_init', create_function('', 'return register_widget("States_Widget");')); – Zachary Schuessler Aug 13 '12 at 13:23
Those one-liners have the severe disadvantage that if users want to issue remove_action, they can't, because there is no hooked function that can be removed. – Elliot Oct 7 '14 at 22:20

Your Answer


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.