title: wordpress registration errors with custom code
body: <p>Anyone know why my following code would be causing a fatal error with the following core wordpress code in users.php line 2610. The code below is in functions.php and when the form is processed the server pops a fatel error for wp-includes/users.php line 2610.</p>
  
  <pre><code>// check register form for errors
  add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
  function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
      global $errors;
      if (! is_wp_error($errors)) $errors = new WP_Error();
      if ( empty( $sanitized_user_login )) {
          $errors-&gt;add( 'user_login_error', __('&lt;strong&gt;ERROR&lt;/strong&gt;: The username field is empty' ) );
              return $errors;
      }
  
      if ( empty( $user_email )) {
          $errors-&gt;add( 'user_login_error', __('&lt;strong&gt;ERROR&lt;/strong&gt;: Email field is empty' ) );
              return $errors;
      }
  
      if (username_exists( $sanitized_user_login )) {
          $errors-&gt;add( 'user_login_error', __( '&lt;strong&gt;ERROR&lt;/strong&gt;: The username you entered is already being used', 'mydomain' ) );
              return $errors;
      }
  
      if (email_exists($user_email)) {
          $errors-&gt;add( 'user_login_error', __( '&lt;strong&gt;ERROR&lt;/strong&gt;: The email address you entered is already being used', 'mydomain' ) );
          //'&lt;strong&gt;ERROR&lt;/strong&gt;: A user with that email already exists. &lt;a href="' . wp_lostpassword_url() . '"&gt;Recover your password&lt;/a&gt;', 'mydomain'
              return $errors;
      }
  
      include("devteamfiles/inc/bannedwordlist.php");
      if (!checkbannedwordlist($sanitized_user_login )) {
          $errors-&gt;add( 'user_login_error', __( '&lt;strong&gt;ERROR&lt;/strong&gt;: That username can not be used', 'mydomain' ) );
              return $errors;
      }
  
      if ( ! isset( $_POST['chkagreetos'] )  ) {
          $errors-&gt;add( 'tos_aggree_error', __( '&lt;strong&gt;ERROR&lt;/strong&gt;: You must agree to site terms of service and disclaimers.', 'mydomain' ) );
              return $errors;
      }
  }
  </code></pre>
  
  <p>Users.php line 2610</p>
  
  <pre><code>$errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
  
      if ( $errors-&gt;get_error_code() )
          return $errors;
  
      $user_pass = wp_generate_password( 12, false );
      $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );
      if ( ! $user_id || is_wp_error( $user_id ) ) {
          $errors-&gt;add( 'registerfail', sprintf( __( '&lt;strong&gt;ERROR&lt;/strong&gt;: Couldn&amp;#8217;t register you&amp;hellip; please contact the &lt;a href="mailto:%s"&gt;webmaster&lt;/a&gt; !' ), get_option( 'admin_email' ) ) );
          return $errors;
      }
  </code></pre>
  
  <hr>
  
  <p>Edited with the code from the answer provided but still popping the fatal error <code>Fatal error: Call to a member function get_error_code() on a non-object in /wp-includes/user.php on line 2610</code></p>
  
  <pre><code>// check register form for errors
  add_filter( 'registration_errors', 'myplugin_registration_errors', 10, 3 );
  function myplugin_registration_errors( $errors, $sanitized_user_login, $user_email ) {
      if (! is_wp_error($errors)) $errors = new WP_Error();
  
      if (username_exists( $sanitized_user_login )) {
          $errors-&gt;add( 'user_login_error', __( '&lt;strong&gt;ERROR&lt;/strong&gt;: STATEMENT 3', 'mydomain' ) );
              return $errors;
      }
  
      if (email_exists($user_email)) {
          $errors-&gt;add( 'user_login_error', __( '&lt;strong&gt;ERROR&lt;/strong&gt;:  STATEMENT 4', 'mydomain' ) );
          //'&lt;strong&gt;ERROR&lt;/strong&gt;: A user with that email already exists. &lt;a href="' . wp_lostpassword_url() . '"&gt;Recover your password&lt;/a&gt;', 'mydomain'
              return $errors;
      }
  
      include("devteamfiles/inc/bannedwordlist.php");
      if (!checkbannedwordlist($sanitized_user_login )) {
          $errors-&gt;add( 'user_login_error', __( '&lt;strong&gt;ERROR&lt;/strong&gt;: STATEMENT 5', 'mydomain' ) );
              return $errors;
      }
  
      if ( ! isset( $_POST['chkagreetos'] )  ) {
          $errors-&gt;add( 'tos_aggree_error', __( '&lt;strong&gt;ERROR&lt;/strong&gt;: STATEMENT 6', 'mydomain' ) );
              return $errors;
      }
  }
  </code></pre>