2

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

1 Answer 1

3

It 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) {
  }
}
1
  • 1
    may whatever deity you believe in give you bountiful gifts good sir, thank you. Commented Jun 20, 2020 at 1:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.