Plugin Support
Laszlo
(@laszloszalvak)
Hi @auther61
500 errors usually indicated server related problems or PHP errors. In your case the 500 error occurs because the way our filter was used.
So you are trying to work with $errors however it is undefined. So when you are trying to add an error to the $errors object, you need to use the 3rd parameter of the filter, which represents this error object.
So your code should look more like:
add_filter('nsl_registration_user_data', function ($userData, $provider, $errors) {
...
if ($userData['username'] == 'xxx') {
$errors->add('invalid_username', '' . __('ERROR') . ':' . __('Registration failed: error description here.'));
}
return $userData;
}, 10, 3);
Once you an error is added to the error object, the registration will fail and will redirect the user back to your /wp-login.php page where this error will be indicated a similarly to errors occurring during the default WordPress registration.
So don’t worry if your PHP code is fine, the users won’t see this page with the 500 error.
Best regards,
Laszlo.
Seems like I got it working now, thanks!
Though when I’m redirected to the login page after preventing the registration, it doesn’t show any error, as I have a custom login page.
How is the error transfered to that page so that I can present it?
Thank you!
-
This reply was modified 6 years, 4 months ago by
auther61.
Plugin Support
Laszlo
(@laszloszalvak)
Hi @auther61
Sorry I forgot to mention in my previous reply that you also need to modify the accepted argument count at the end of the add_filter():
https://developer.wordpress.org/reference/functions/add_filter/
to 3, like you see in the code sample in my previous reply.
The error is stored in the $errors object which comes from the WP_Error class:
https://developer.wordpress.org/reference/classes/wp_error/
and the wp-login.php page prints the error message what this object contains.
So if you would like to display an error on a custom page, you need to display it on a custom way.
I am not really sure the way you are redirect the users to another page, but what Nextend Social Login can help you with, is displaying some kind of error on our own way. If that is fine for you, then you could add the error message like:
NSL\Notices::addError('Custom error message here.');
in this same function what you hooked to the nsl_registration_user_data filter.
Ps.:
If you inspect this file of ours:
wp-content/plugins/social-login/nextend-facebook-connect/includes/userData.php
what we actually do when the “nsl_registration_user_data” filter returns an error is the following:
-we set our own error notification, what I mentioned above
-we redirect with an exit
so actually in the function what you hooked to the filter, you could implement a logic for your error handling and break the flow with a redirect and exit.
Best regards,
Laszlo.
Thank you, this is very valuable information, I appreciate it.
I’ll follow up in case I’ll have any further questions. Thanks again for your great support!