thoughts of the driverSteinn Eldjarn Sigurdarson on tech, tel, digital freedom and possibly his life..

April 15, 2009

Fatal error: Cannot use object of type WP_Error as array

Filed under: General, Technology — Steinn E. Sigurðarson @ 2:45 pm

Quick post, since others face the same problem. When I updated I added a captcha plugin to my site, since I was getting about 10 spammers registering per day. This means that you need to enter a captcha phrase to register as a commenter here. If you however entered the wrong phrase, you were confronted with the lovely error:

Fatal error:  Cannot use object of type WP_Error as array...

A look at the code revealed the plugin to be checking if the captcha entered was correct, and then doing something like this:

$errors["error"] = "ERROR: You must enter the correct letters displayed in the image.";

(I’m not printing it verbatim because I’m too lazy to open the original file)

Obviously $errors (a global) was not an array, but an object of the type WP_Error. Solution: To read up on how WP_Error works, and use it? Easy peasy?

Not quite.

Simply using the preferred $errors->add(’errorcode’,'error message’); didn’t do the trick, in fact changing the code to that caused an even worse behaviour — instead of there being a fatal PHP error, the captcha simply didn’t complain at all, and new users were able to register at will — oh the humanity!

After checking wp-login.php, where they call the hook “register_post”, which the captcha user registration plugin was hooking into, I saw that it calls it like this:

do_action('register_post', $user_login, $user_email, $errors);

Eureka! I can’t simply use the global $errors array, I must use the one supplied to me by the do_action! Immediately I modify the capcc_validateregister function so that it’s declaration takes in 3 parameters, $user_login, $user_email, and the coveted $errors I so hoped to use. Errrrrr, now I got another wonderful error, that arguments 2 and 3 were missing! Oh lord, I was only getting the desired username!

After digging through into the definition of do_action in wp-includes/plugin.php, I found out that it actually loops through all it’s supplied arguments and appends them to a list of arguments which it then uses for all functions which are hooked in to this hook, but each of them has a little configuration variable called “accepted_args”, and upon freshing up on the wordpress plugin docs for hooks, I finally found the answer to my problem.

add_action('register_post', 'capcc_validateregister', 10, 3);

Where 10 is the priority level (10 is default, which I assumed was good enough), and 3 is the accepted_args parameter.

So hooray, captcha works, and now “all” my readers will know how to fix their “Cannot use object of type WP_Error as array” problems:
1. Make sure you are using the right $errors (in register_post case it is supplied as arg #3)
2. Use $errors->add(’errorcode’, ‘message’); instead of $errors['errorcode']= ‘message’;

Hopefully getting point 1 down will be quicker than it was for me, as including the writing of this post, it’s taken an hour of my time! And I really should be debugging a cool app which I’ll blog about later!

digg this

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

gin 2.840 & tonic. | Powered by WordPress