admin管理员组文章数量:1336331
I would really, really, appreciate any insight I can get here.
I'm working on a WordPress plugin that registers users in WordPress and want to validate data submitted through the form. I have a class for this, here's a condensed version of it:
namespace PluginName\Includes\Forms\Classes;
class Validation {
public function validate_fields($field, $form_type){
$errors = new WP_Error();
$validated_fields = [];
if(!is_email($field)):
$errors->add('not_email', __('Entered an invalid email', 'plugin-name'));
else:
$validated_fields['user_email'] = $field;
endif;
//other validations
$error_code = $errors->get_error_code();
if(!empty($error_code)):
return $errors;
endif;
return $validated_fields;
}
}
In my user registration class I instantiate the class above to get whatever it returns either and array of field data or errors.
Here's my Register_User class condensed:
namespace PluginName\Includes\Forms\Classes;
class Register_User{
public function __construct(){
add_action('wp_ajax_pn_register_user', [$this, 'pn_register_user']);
add_action('wp_ajax_nopriv_pn_register_user', [$this, 'pn_register_user']);
}
public function pn_register_user(){
if (!check_ajax_referer( 'reg_nonce' )):
wp_die();
endif;
$form_validation = new Validation();
$form_validation_results = $form_validation->validate_fields($_POST, 'registration');
if(is_wp_error($form_validation_results)):
error_log('Some error happened');
else:
error_log('No Errors to report');
$userdata = array(
'user_email' => $form_validation_results['user_email']
//other fields
);
wp_insert_user($userdata);
endif;
wp_die();
}
}
This does not work if I have this line in the validation class: $errors = new WP_Error();
- the form is submitted but no user is registered as soon as I comment out everything related to WP_Error the user is registered normally. And it's not due to any validation, even with no validation and just the WP_Error object instantiated it does not work.
Again, I would really appreciate any help that I can get, my head is about to explode. I couldn't find anything on the site or elsewhere. I'm not sure if it's a scoping issue or an ajax issue? regarding the former, I tried making $errors global but still no results.
Please help
I would really, really, appreciate any insight I can get here.
I'm working on a WordPress plugin that registers users in WordPress and want to validate data submitted through the form. I have a class for this, here's a condensed version of it:
namespace PluginName\Includes\Forms\Classes;
class Validation {
public function validate_fields($field, $form_type){
$errors = new WP_Error();
$validated_fields = [];
if(!is_email($field)):
$errors->add('not_email', __('Entered an invalid email', 'plugin-name'));
else:
$validated_fields['user_email'] = $field;
endif;
//other validations
$error_code = $errors->get_error_code();
if(!empty($error_code)):
return $errors;
endif;
return $validated_fields;
}
}
In my user registration class I instantiate the class above to get whatever it returns either and array of field data or errors.
Here's my Register_User class condensed:
namespace PluginName\Includes\Forms\Classes;
class Register_User{
public function __construct(){
add_action('wp_ajax_pn_register_user', [$this, 'pn_register_user']);
add_action('wp_ajax_nopriv_pn_register_user', [$this, 'pn_register_user']);
}
public function pn_register_user(){
if (!check_ajax_referer( 'reg_nonce' )):
wp_die();
endif;
$form_validation = new Validation();
$form_validation_results = $form_validation->validate_fields($_POST, 'registration');
if(is_wp_error($form_validation_results)):
error_log('Some error happened');
else:
error_log('No Errors to report');
$userdata = array(
'user_email' => $form_validation_results['user_email']
//other fields
);
wp_insert_user($userdata);
endif;
wp_die();
}
}
This does not work if I have this line in the validation class: $errors = new WP_Error();
- the form is submitted but no user is registered as soon as I comment out everything related to WP_Error the user is registered normally. And it's not due to any validation, even with no validation and just the WP_Error object instantiated it does not work.
Again, I would really appreciate any help that I can get, my head is about to explode. I couldn't find anything on the site or elsewhere. I'm not sure if it's a scoping issue or an ajax issue? regarding the former, I tried making $errors global but still no results.
Please help
Share Improve this question asked Jun 19, 2020 at 18:19 ryanaindimerryanaindimer 233 bronze badges1 Answer
Reset to default 3It seems you need to use $errors = new \WP_Error();
in your code, since WP_Error
isn't part of your namespace.
I ran a quick test on my machine, using wp-cli commands:
Using new WP_Error()
:
% wp eval 'namespace PJ\NS\Test; $x = new WP_Error(); var_dump( $x );'
Fatal error: Uncaught Error: Class 'PJ\NS\Test\WP_Error' not found in phar:///usr/local/bin/wp/vendor/wp-cli/eval-command/src/Eval_Command.php(37) : eval()'d code:1
Stack trace:
#0 phar:///usr/local/bin/wp/vendor/wp-cli/eval-command/src/Eval_Command.php(37): eval()
#1 [internal function]: Eval_Command->__invoke(Array, Array)
#2 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/CommandFactory.php(98): call_user_func(Array, Array, Array)
#3 [internal function]: WP_CLI\Dispatcher\CommandFactory::WP_CLI\Dispatcher\{closure}(Array, Array)
#4 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Dispatcher/Subcommand.php(451): call_user_func(Object(Closure), Array, Array)
#5 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(371): WP_CLI\Dispatcher\Subcommand->invoke(Array, Array, Array)
#6 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(394): WP_CLI\Runner->run_command(Array, Array)
#7 phar:///usr/local/bin/wp/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(87): WP_CLI\Runne in phar:///usr/local/bin/wp/vendor/wp-cli/eval-command/src/Eval_Command.php(37) : eval()'d code on line 1
Error: There has been a critical error on your website.Learn more about debugging in WordPress. There has been a critical error on your website.
Using new \WP_Error()
:
% wp eval 'namespace PJ\NS\Test; $x = new \WP_Error(); var_dump( $x );'
object(WP_Error)#1516 (2) {
["errors"]=>
array(0) {
}
["error_data"]=>
array(0) {
}
}
本文标签: phpWhy when I instantiate wperror in a validation method my user registration method stops working
版权声明:本文标题:php - Why when I instantiate wp_error in a validation method my user registration method stops working? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742332639a2454999.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论