How to display error messages using WP_Error class?

With that in functions.php you’d probably have to declare $error to be global like so :

if ('POST' == $_SERVER['REQUEST_METHOD'] && !empty($_POST['action']) && $_POST['action'] == 'registration') {
    global $error;
    $error = new WP_Error();
    // the rest of your code

And then do global $error; again on your registration page before trying to use it.

But I don’t understand why you have that code in functions.php. That seems like bad design to me. You are running that if conditional every time any page loads and it sounds like you only need it on your registration page, which I am assuming is something you’ve written yourself and that you are not talking about the built in registration/login page at wp-login.php. Given that assumption, just move that code to the registration page and it will be available with no hassle. WP_Error has methods will let you get at the data.

Leave a Comment